核心数据获取速度很慢

时间:2014-08-11 22:40:32

标签: ios core-data nsfetchrequest

我正从大型数据库中检索一系列对象。每次查找都有1-20个结果。使用获取请求如下:

NSPredicate *indexPredicate = [NSPredicate predicateWithFormat:
                          @"index == %@",indexNumber];

我可以在相对较短的时间内获得结果。

CoreData: annotation: total fetch execution time: 0.0623s for 9 rows.

然而,当将所有谓词与[NSCompoundPredicate或PredredWithSubpredicates合并:在一次操作中进行提取时,速度会下降。

CoreData: annotation: total fetch execution time: 0.3890s for 195 rows.

我尝试过索引,设置提取和批量限制,但无法减少总提取时间。我还尝试使用GDC一个接一个地同时执行获取请求,但是无法设置它。如何减少获取此数据的时间?

1 个答案:

答案 0 :(得分:3)

将20个索引查找与OR组合在fetch中进行了大量比较。您可以通过将索引值收集到数组中然后使用与IN的单个比较来改进问题。像

这样的东西
NSArray *indexesToFetch = // A bunch of NSNumbers with index numbers of interest
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"indexNumber in %@", indexesToFetch];

如果您要搜索大量数据,这仍然需要做很多工作。但是使用IN这样的事情通常会比使用一大堆OR快得多。

相关问题