如果我的Author NSManagedObject模型有一个authorID属性(由服务器决定),那么如果NSFetchRequest按照authorID而不是完整的Author对象进行过滤,NSPredicate的性能会更好吗?假设我正在通过某个author获取所有的Book NSManagedObject。哪种predicateFormat更好?
[NSPredicate predicateWithFormat:@"author = %@", anAuthor]或
[NSPredicate predicateWithFormat:@"author.authorID = %@", anAuthor.authorID]描述这个的最好方法是什么?我使用OCUnit (SenTestingKit)进行核心数据测试。iOS有像Ruby's Benchmark module这样的东西吗?
发布于 2011-12-23 22:42:32
第一种方法(只使用Author)可能会更快,但只有测试才能确定。
如果您获取(例如Book)与Author有关系的对象,并且您使用谓词
[NSPredicate predicateWithFormat:@"author = %@", anAuthor]SQLite可以查看从Book到Author的合并表是否具有该给定author的正确主键。换句话说,谓词变成了对Author实体主键的检查。尽管如此,CoreData仍然需要查询合并表。
如果您使用
[NSPredicate predicateWithFormat:@"author.authorID = %@", anAuthor.authorID]然后,SQLite必须将合并表与实际的Author表进行join,然后匹配得到的authorID列。这将是更多的工作。如果authorID没有索引,它就会崩溃。
https://stackoverflow.com/questions/8610687
复制相似问题