核心数据性能:NSPredicate 比较对象
如果我的 Author
NSManagedObject
模型具有 authorID
属性(由服务器确定),那么 NSFetchRequest
的性能会更好吗? NSPredicate
按 authorID
过滤,而不是完整的 Author
对象?假设我正在获取某个 author
的所有 Book
NSManagedObject
。哪种 predicateFormat
更好?
[NSPredicate predicateWithFormat:@"author = %@", anAuthor]
或
[NSPredicate predicateWithFormat:@"author.authorID = %@", anAuthor.authorID]
分析此问题的最佳方法是什么?我使用 OCUnit
(SenTestingKit
) 进行核心数据测试。 iOS 是否有类似 Ruby 的 Benchmark 模块?
If my Author
NSManagedObject
model has a authorID
attribute (determined by the server), will an NSFetchRequest
perform better if the NSPredicate
filters by authorID
rather than the complete Author
object? Let's say I'm fetching all Book
NSManagedObject
s by a certain author
. Which predicateFormat
is better?
[NSPredicate predicateWithFormat:@"author = %@", anAuthor]
or
[NSPredicate predicateWithFormat:@"author.authorID = %@", anAuthor.authorID]
What's the best way to profile this? I have Core Data testing working with OCUnit
(SenTestingKit
). Does iOS have something like Ruby's Benchmark module?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能值得使用
-com.apple.CoreData.SQLDebug 1 参数运行您的应用程序,详细信息 此处。
然后,您可以查看 Core Data 是否在两种情况下执行相同的 SQL(假设您使用的是 SQLite 存储)。
It might be worth running your app with an argument of
-com.apple.CoreData.SQLDebug 1
, as detailed here.You could then see if Core Data was executing the same SQL in both circumstances (assuming you're using a SQLite store).
第一个(仅使用
Author
)可能会更快,但只有测试才能确定。如果您获取(例如
Book
)与Author
有关系的对象并且使用谓词,SQLite 可以查看合并表是否从
Book
到Author
具有该给定author
的正确主键。换句话说,谓词变成对Author
实体主键的检查。不过,CoreData 仍然需要查阅合并表。如果您使用
,则 SQLite 必须将合并表与实际的 AuthorID 表进行连接,然后匹配生成的authorID 列。那将是更多的工作。如果
authorID
没有被索引,它就会崩溃。The first one (using just
Author
) would probably be faster, but only testing will tell you for sure.If you fetch (e.g.
Book
) objects that have an relationship toAuthor
and you use a predicateSQLite can see if the merge table from
Book
toAuthor
has the right primary key for that givenauthor
. In other words, the predicate turns into a check for theAuthor
entities primary key. CoreData still has to consult the merge table, though.If you use
then SQLite will have to
join
the merge table with the actualAuthor
table and then match the resultingauthorID
column. That would be more work. And it would break down ifauthorID
isn't indexed.