I have a Core Data database running on an iPad. It has 30,000 contacts in it with attributes such as firstname, lastname etc.
The search performance isn't great using an NSCompoundPredicate. I am or'ing two LIKE Predicates (on firstname and lastname) and it is taking around 1500ms to search the 30,000 contacts. I then tried adding the 'indexed' property to the firstname and lastname fields (using the XCode Data Modeller UI) but the performance seems exactly the same.
Since adding the Indexed property to the two fields I have removed all objects from the database and re-populated it. Do I need to do anything more in order for the indexes to start to be used, and should I expect to see a performance improvement by indexing the two fields being used within the compound predicate?
I am running on a real device (an iPad3). 1500ms to search through 30,000 contact records doesn't seem that great - is it par for the course?
Many thanks.
LIKE
searches are expensive, regardless of whether the attribute(s) you're searching are indexed.
A lot of apps -- including Apple's own -- avoid this problem by restricting the search in ways the user won't notice. For example, searching while typing in an app like Contacts doesn't really want to match the query against all possible substrings of people's names; instead it matches people whose first or last name starts with the query.
For further optimization, consider also that you probably can get away without the full Unicode-savvy, case- and diacritic-aware matching that LIKE
and BEGINSWITH
get you. Apple's DerivedProperty example shows how you can get much better performance by storing a "normalized" (i.e. all same case, no diacritics, other extraneous stuff gone) version of your search property and using predicates that rely on lexicographic ordering (e.g. normalizedLastName >= "foo" && normalizedLastName < "fop"
).
More details on these and related tricks can be found in some of the Core Data sessions from past WWDCs, especially 2010 Session 137, "Optimizing Core Data Performance on iPhone OS".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With