核心数据性能:NSPredicate 比较对象

发布于 2024-12-22 22:36:17 字数 756 浏览 6 评论 0原文

如果我的 Author NSManagedObject 模型具有 authorID 属性(由服务器确定),那么 NSFetchRequest 的性能会更好吗? NSPredicateauthorID 过滤,而不是完整的 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 NSManagedObjects 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深者入戏 2024-12-29 22:36:17

可能值得使用 -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).

谁的新欢旧爱 2024-12-29 22:36:17

第一个(仅使用 Author)可能会更快,但只有测试才能确定。

如果您获取(例如 Book)与 Author 有关系的对象并且使用谓词,

[NSPredicate predicateWithFormat:@"author = %@", anAuthor]

SQLite 可以查看合并表是否从 BookAuthor 具有该给定 author 的正确主键。换句话说,谓词变成对 Author 实体主键的检查。不过,CoreData 仍然需要查阅合并表。

如果您使用

[NSPredicate predicateWithFormat:@"author.authorID = %@", anAuthor.authorID]

,则 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 to Author and you use a predicate

[NSPredicate predicateWithFormat:@"author = %@", anAuthor]

SQLite can see if the merge table from Book to Author has the right primary key for that given author. In other words, the predicate turns into a check for the Author entities primary key. CoreData still has to consult the merge table, though.

If you use

[NSPredicate predicateWithFormat:@"author.authorID = %@", anAuthor.authorID]

then SQLite will have to join the merge table with the actual Author table and then match the resulting authorID column. That would be more work. And it would break down if authorID isn't indexed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文