• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: CoreData fetch is way too slow
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: CoreData fetch is way too slow


  • Subject: Re: CoreData fetch is way too slow
  • From: Bill Dudney <email@hidden>
  • Date: Tue, 5 Feb 2008 12:58:02 -0700

Hi James,

Though I don't know for sure it sounds to me like you don't have an index on the 'input' field. If I remember correctly there is checkbox on the field in the model editor that says 'indexed' and if you turn that on the SQLite store will put an index on that field which will make your fetches much much faster.

There could be issues with the 'like' part of your query even after you make an index, but i'm not hip enough on SQLite to say one way or the other.

http://www.sqlite.org/optoverview.html

has some guidelines to optimizing things.

HTH,

-bd-
http://bill.dudney.net/roller/objc

On Feb 5, 2008, at 12:31 PM, James Hober wrote:

I have a CoreData app that has a SQLite store holding 157,273 managed objects. Each managed object has 2 attributes, input and answer, which are NSStrings and that's all. No other attributes. No relationships. No fetched properties.

A simple fetch for a single managed object takes 7 seconds!

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K like %@",
@"input", input]; //assume NSString *input exists
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"MyEntity"
inManagedObjectContext: [self managedObjectContext]];
[fetchRequest setEntity:entity];
[fetchRequest setPredicate:predicate];
[fetchRequest setFetchLimit:1];
NSError *error = nil;


//The following line takes 7 seconds to execute:
NSArray *results = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error];


if ([results count]) {return [[results objectAtIndex:0] valueForKey:@"answer"];}
return @"";


How can I speed this up?

The 157,273 inputs are unique. I believe CoreData searches them linearly in no particular order, instead of sorting them so they can be binary searched in the store. Is this aspect of CoreData opaque? Is there no way to get CoreData to efficiently search on a unique string attribute?

If my experiments with CoreData continue to prove unfruitful, I may remain with my current implementation which uses Objective-C++ and an STL map. I can load all the data into the map in about 3.6 seconds and then retrieval from memory is near instantaneous.

Thank you for any help speeding up this CoreData implementation or any other suggestions.
_______________________________________________


Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >CoreData fetch is way too slow (From: James Hober <email@hidden>)

  • Prev by Date: Re: CoreData fetch is way too slow
  • Next by Date: [[NSGarbageCollector defaultCollector] isEnabled] wrong?
  • Previous by thread: Re: CoreData fetch is way too slow
  • Next by thread: re: CoreData fetch is way too slow
  • Index(es):
    • Date
    • Thread