20

I'm looking to speed up queries to my SQL backed CoreData instance (displaying records sorted by date). I know that indexing can help decrease query time, but what's the difference between:

Highlighting the entity that an attribute belongs to, then adding a comma separated list of attributes into the indexes field as seen here:

enter image description here

Or highlighting the attribute, then checking the indexed box as seen here:

enter image description here

1

2 Answers 2

15

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.

In your case you should go for the single indexes first (that is, select Indexed for the attributes you like to search for). The compound index you showed could never be used if you just search for babyId, for example.

5
  • For setting compound indexes from code, see also (overlapping) answer stackoverflow.com/a/8657785/3419541
    – PDK
    Commented May 19, 2017 at 12:20
  • Hi, How can i add index for table When I can't find the index property in newest version Xcode. Commented Oct 27, 2017 at 8:42
  • 3
    @taitanxiami That is a good question. Just found the answer: Xcode 9 now has a command "Add Fetch Index" in the Editor menu. It will be enabled if you select an entity. You can define compound indices by adding more than one attribute to that index.
    – Dirk
    Commented Oct 27, 2017 at 11:28
  • @Dirk Do you mean long press the "Add Entity" button , then choose the "Add Fetch Index ", at last to select the corresponding property? Commented Oct 28, 2017 at 0:56
  • @taitanxiami No, I was not aware of that option. I simply mean the "Editor" menu in the menu bar of Xcode 9.0.1. It changes according to context and provides this command when you edit an entity.
    – Dirk
    Commented Oct 29, 2017 at 1:45
9

At WWDC 2017, apple updated this to instead be done by using a Fetch Index(see: https://developer.apple.com/videos/play/wwdc2017/210/?time=997) To add it, select the entity and then go to Editor -> Add Fetch Index

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.