我使用iOS 4.3.2在iPad上運行以下核心數據讀取請求。在iPad上核心數據讀取速度非常慢
NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"feed.account.name == %@ AND feed.feedType == %@", accountName,feedType];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date"
ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
fetchedResultsController=[self createFetchedResultsController:@"RssFeedItem" predicate:predicate sortDescriptors:sortDescriptors];
[fetchedResultsController.fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:@"url",@"headline",@"isRead",@"origin",@"originId",@"date",nil]];
[fetchedResultsController.fetchRequest setFetchLimit:500];
[fetchedResultsController setDelegate:self];
[fetchedResultsController performFetch:&error];
return [fetchedResultsController fetchedObjects];
我有大約688排在RssFeedItem表SQLite數據庫的數據,並在所有其他表小於100行。該查詢使用調試日誌記錄爲:
CoreData: sql: SELECT t0.Z_ENT, t0.Z_PK, t0.ZURL, t0.ZHEADLINE, t0.ZISREAD, t0.ZORIGIN,
CoreData: annotation: sql connection fetch time: 3.3854s
CoreData: annotation: total fetch execution time: 3.4178s for 688 rows.
而且運行速度很慢(超過3秒)。我有所有必需的搜索字段和排序字段的索引。我假設也許2路連接使它變慢,但不知道爲什麼會這麼慢。有沒有什麼辦法來優化這段代碼或查詢,還是有什麼我應該看看?
非常感謝,所以我會先試一試。 – 2011-05-06 14:19:34
我試着重新安排上面指定的謂詞。但仍然很慢: 'CoreData:sql:SELECT t0.Z_ENT,t0.Z_PK,t0.ZURL,t0.ZHEADLINE,t0.ZISREAD,t0.ZORIGIN,t0.ZORIGINID,t0.ZDATE FROM ZFEEDITEM t0 JOIN ZFEED t1 ON t0.ZFEED = t1.Z_PK JOIN ZFEEDACCOUNT t2 ON t1.ZACCOUNT = t2.Z_PK WHERE((t1.ZFEEDTYPE =?AND t2.ZNAME =?)AND t0.Z_ENT =?)ORDER BY t0.ZDATE DESC LIMIT 500' CoreData:annotation:sql connection fetch time:4.0500s' 'CoreData:annotation:total fetch execution time:4.0797s for 500 rows.' – 2011-05-06 14:36:13
您的謂詞仍然會很慢,因爲其中有2個連接發生。您可能需要重構您的模型以獲得重大改進。發佈您的模型,我們可能會提出一些改進建議。基本上,你希望儘可能地在你要獲取的實體中擁有你在謂詞中檢查的值,而不是在關係中。 – McCygnus 2011-05-07 05:46:10