Note that there are some explanatory texts on larger screens.

plurals
  1. POFetch from CoreData and Group By categories is extremely slow
    text
    copied!<p>I have to fetch about few hundreds records from CoreData and group them by category_id to put into sections. Below is the code for the fetchedResultController</p> <pre><code>- (NSFetchedResultsController *)fetchedResultsController { if (_fetchedResultsController != nil) { return _fetchedResultsController; } NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%d == mall_id",self.mallId.intValue]; NSFetchRequest *fetch = [[NSFetchRequest alloc] initWithEntityName:@"Shop"]; NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"category_id" ascending:YES]; NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; [fetch setPredicate:predicate]; [fetch setFetchBatchSize:20]; [fetch setSortDescriptors:sortDescriptors]; self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetch managedObjectContext:self.contextForDb sectionNameKeyPath:@"category_id" cacheName:@"root"]; return self.fetchedResultsController; } </code></pre> <p>the fetching takes around 1-1.5 seconds which is unacceptable slow for few hundreds of records. I've tried to search on the Internet for solution for days but haven't managed to solve this performance problem.</p> <p>I think the bottle neck is the grouping on the database.</p> <p>Could somebody please help ?</p> <p><strong>UPDATE</strong></p> <pre><code>2012-04-02 17:57:08.872 SGMalls[186:707] CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZADDRESS, t0.ZID, t0.ZLATITUDE, t0.ZLONGITUDE, t0.ZNAME, t0.ZPHONE, t0.ZUPDATED_TIME, t0.ZURL, t0.ZZIP FROM ZMALL t0 WHERE t0.ZID = ? 2012-04-02 17:57:08.875 SGMalls[186:707] CoreData: annotation: sql connection fetch time: 0.0030s 2012-04-02 17:57:08.877 SGMalls[186:707] CoreData: annotation: total fetch execution time: 0.0047s for 1 rows. 2012-04-02 17:57:09.040 SGMalls[186:707] CoreData: sql: SELECT 0, t0.Z_PK FROM ZSHOP t0 WHERE ? = t0.ZMALL_ID ORDER BY t0.ZCATEGORY_ID 2012-04-02 17:57:09.390 SGMalls[186:707] CoreData: annotation: sql connection fetch time: 0.3496s 2012-04-02 17:57:09.391 SGMalls[186:707] CoreData: annotation: total fetch execution time: 0.3514s for 16 rows. 2012-04-02 17:57:09.395 SGMalls[186:707] CoreData: sql: SELECT t0.ZCATEGORY_ID, COUNT (DISTINCT t0.Z_PK) FROM ZSHOP t0 WHERE ? = t0.ZMALL_ID GROUP BY t0.ZCATEGORY_ID ORDER BY t0.ZCATEGORY_ID 2012-04-02 17:57:09.741 SGMalls[186:707] CoreData: annotation: sql connection fetch time: 0.3458s 2012-04-02 17:57:09.743 SGMalls[186:707] CoreData: annotation: total fetch execution time: 0.3475s for 3 rows. 2012-04-02 17:57:09.748 SGMalls[186:707] CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZDETAIL, t0.ZID, t0.ZNAME FROM ZCATEG t0 WHERE t0.ZID = ? 2012-04-02 17:57:09.751 SGMalls[186:707] CoreData: annotation: sql connection fetch time: 0.0028s 2012-04-02 17:57:09.754 SGMalls[186:707] CoreData: annotation: total fetch execution time: 0.0061s for 1 rows. 2012-04-02 17:57:09.757 SGMalls[186:707] Title: icon_foodspecialties 2012-04-02 17:57:09.765 SGMalls[186:707] CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZDETAIL, t0.ZID, t0.ZNAME FROM ZCATEG t0 WHERE t0.ZID = ? 2012-04-02 17:57:09.767 SGMalls[186:707] CoreData: annotation: sql connection fetch time: 0.0022s 2012-04-02 17:57:09.769 SGMalls[186:707] CoreData: annotation: total fetch execution time: 0.0042s for 1 rows. 2012-04-02 17:57:09.773 SGMalls[186:707] Title: icon_supermarket 2012-04-02 17:57:09.782 SGMalls[186:707] CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZDETAIL, t0.ZID, t0.ZNAME FROM ZCATEG t0 WHERE t0.ZID = ? 2012-04-02 17:57:09.784 SGMalls[186:707] CoreData: annotation: sql connection fetch time: 0.0022s 2012-04-02 17:57:09.787 SGMalls[186:707] CoreData: annotation: total fetch execution time: 0.0046s for 1 rows. </code></pre> <p>Enable SQL Debugging returns this in the console</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload