Reputation: 201
I have following Core Data fetch request running on iPad using iOS 4.3.2.
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];
I have approx 688 rows of data in the SQLite database for RssFeedItem table, and less than 100 rows in all other tables. That query is logged using debug logging as:
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.
And it runs very slow (more than 3 seconds). I have indexes on all the required search fields and sort fields. I am assuming maybe the 2 way join is making it slow, but not sure why it would be that slow. Is there any way to optimize this code or query, or is there something else I should look at?
Upvotes: 2
Views: 1597
Reputation: 4215
That seems a bit slow. I don't know if this will make a huge speed difference, but it should help a little, rework your predicate as:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"feed.feedType == %@ AND feed.account.name == %@", feedType, accountName];
You want to put the cheapest condition first so that the more expensive condition won't have to be checked in all cases. The join that happens with checking the account name is a lot more expensive. Also, depending on what the feed type value looks like it, it might be quicker to switch it to be an integer (I'm assuming it's currently a string since it's an object).
Upvotes: 1
Reputation: 3355
I guess the lines that are slowing your query down are
[fetchedResultsController.fetchRequest setPropertiesToFetch:[NSArray
arrayWithObjects:@"url",@"headline",@"isRead",@"origin",@"originId",@"date",nil]];
and
[fetchedResultsController.fetchRequest setFetchLimit:500];
I'm curious, why do you need to fetch all those properties (and 500 items) at once?
Upvotes: 2