Matthias Bauch
Matthias Bauch

Reputation: 90127

What are Indexes in the Xcode Core-Data data model inspector

In Xcode you can add "Indexes" for an entity in the data model inspector.

Xcode sidebar showing the Indexes view

For the screenshot I did hit "add" twice so "comma,separated,properties" is just the default value.

What exactly are those indexes?
Do they have anything to do with indexed attributes? And if they have what is the difference between specifying the Indexes in this inspector and selecting "Indexed" for the individual attribute?

Upvotes: 23

Views: 6048

Answers (3)

Bartłomiej Semańczyk
Bartłomiej Semańczyk

Reputation: 61880

Optimizing Core Data searches and sorts

As the title says, indexing is to speed up searching and sorting your database. However it slows down saving changes to persistant store. It matters when you are using NSPredicate and NSSortDescriptor objects within your query.

Let's say you have two entities: PBOUser and PBOLocation (many to many). You can see its properties at the image below:

enter image description here

Suppose that in database there is 10,000 users, and 50,000 locations. Now we need to find every user with email starting on a. If we provide such query without indexing, Core Data must check every record (basically 10,000).

But what if it is indexed (in other words sorted by email descending)? --> Then Core Data checks only those records started with a. If Core Data reaches b then it will stop searching because it is obvious that there are no more records whose email starts with a since it is indexed.

How to enable indexing on a Core Data model from within Xcode:

enter image description here
or:
enter image description here

Hopefully they are equivalent:-)

But what if you wanted: Emails started with a and name starts with b You can do this checking INDEXED for name property for PBOUser entity, or:

enter image description here

This is how you can optimise your database:-)

Upvotes: 19

Dirk
Dirk

Reputation: 2425

Adding a row with a single attribute to the Indexes list is equivalent to selecting Indexed for that attribute: It creates an index for the attribute to speed up searches in query statements.

The Indexes list is meant for compound indexes. Compound indexes are useful when you know that you will be searching for values of these attributes combined in the WHERE clause of a query:

SELECT * FROM customer WHERE surname = "Doe" AND firstname = "Joe";

This statement could make use of a compound index surname, firstname. That index would also be useful if you just search for surname, but not if you only search for firstname. Think of the index as if it were a phone book: It is sorted by surname first, then by first name. So the order of attributes is important.

Upvotes: 7

Swift Dev Journal
Swift Dev Journal

Reputation: 20088

Use the Indexes list to add compound indexes to the entity. A compound index is an index that spans multiple attributes or relationships. A compound index can make searching faster. The names of attributes and relationships in your data model are the most common indexes. You must use the SQLite store to use compound indexes.

Upvotes: 7

Related Questions