Xcode Core-Data 数据模型检查器中的索引是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8562711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are Indexes in the Xcode Core-Data data model inspector
提问by Matthias Bauch
In Xcode you can add "Indexes" for an entity in the data model inspector.
在 Xcode 中,您可以在数据模型检查器中为实体添加“索引”。
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?
这些索引究竟是什么?
它们与索引属性有什么关系吗?如果他们有在此检查器中指定索引和为单个属性选择“索引”之间有什么区别?
回答by Bart?omiej Semańczyk
Optimizing Core Data searches and sorts
As the title says, indexing is to speed up searching and sortingyour database. However it slows down saving changesto persistant store. It matters when you are using NSPredicate
and NSSortDescriptor
objects within your query.
正如标题所说,索引是为了加快对数据库的搜索和排序。但是,它会减慢将更改保存到持久存储的速度。当您在查询中使用NSPredicate
和NSSortDescriptor
对象时,这很重要。
Let's say you have two entities: PBOUser
and PBOLocation
(many to many). You can see its properties at the image below:
假设您有两个实体:PBOUser
and PBOLocation
(many to many)。您可以在下图中看到它的属性:
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 everyrecord (basically 10,000).
假设数据库中有 10,000 个用户和 50,000 个位置。现在我们需要找到电子邮件以 开头的每个用户a
。如果我们在没有索引的情况下提供这样的查询,Core Data 必须检查每条记录(基本上是 10,000)。
But what if it is indexed (in other words sorted by emaildescending)? --> 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.
但是如果它被索引(换句话说,按电子邮件降序排序)呢?--> 然后 Core Data 只检查那些以a
. 如果 Core Data 到达b
,它将停止搜索,因为很明显,a
自从它被索引以来,没有更多的电子邮件开头的记录。
How to enable indexing on a Core Data model from within Xcode:
如何在 Xcode 中对 Core Data 模型启用索引:
or:
或者:
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:
但是如果你想a
b
怎么办:电子邮件开头和名称开头你可以检查实体的name
属性的索引PBOUser
,或者:
This is how you can optimise your database:-)
这是优化数据库的方法:-)
回答by Dirk
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.
将具有单个属性的行添加到Indexes
列表中等同于选择Indexed
该属性:它为该属性创建索引以加快查询语句中的搜索速度。
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:
该Indexes
列表用于复合索引。当您知道将搜索组合在WHERE
查询子句中的这些属性的值时,复合索引很有用:
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.
该语句可以使用复合索引surname, firstname
。如果您只搜索surname
,该索引也很有用,但如果您只搜索firstname
. 将该索引视为电话簿:它先按姓氏排序,然后按名字排序。所以属性的顺序很重要。
回答by Mark Szymczyk
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.
使用索引列表向实体添加复合索引。复合索引是跨越多个属性或关系的索引。复合索引可以使搜索速度更快。数据模型中属性和关系的名称是最常见的索引。您必须使用 SQLite 存储才能使用复合索引。