iPad 上的核心数据获取速度非常慢

发布于 2024-11-04 23:46:29 字数 1417 浏览 0 评论 0原文

我使用 iOS 4.3.2 在 iPad 上运行以下核心数据获取请求。

NSPredicate *predicate = [NSPredicate predicateWithFormat:
                          @"feed.account.name == %@ AND feed.feedType == %@", accountName,feedType];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date" 
                                                               ascending:NO];

NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

fetchedResultsController=[self createFetchedResultsController:@"RssFeedItem"  predicate:predicate sortDescriptors:sortDescriptors];

[fetchedResultsController.fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:@"url",@"headline",@"isRead",@"origin",@"originId",@"date",nil]];


[fetchedResultsController.fetchRequest setFetchLimit:500];

[fetchedResultsController setDelegate:self];

[fetchedResultsController performFetch:&error];

return [fetchedResultsController fetchedObjects];

我在 SQLite 数据库中的 RssFeedItem 表中有大约 688 行数据,而在所有其他表中只有不到 100 行数据。该查询使用调试日志记录为:

CoreData: sql: SELECT t0.Z_ENT, t0.Z_PK, t0.ZURL, t0.ZHEADLINE, t0.ZISREAD, t0.ZORIGIN,           
CoreData: annotation: sql connection fetch time: 3.3854s
CoreData: annotation: total fetch execution time: 3.4178s for 688 rows.

并且运行速度非常慢(超过 3 秒)。我在所有必需的搜索字段和排序字段上都有索引。我假设 2 路连接可能会使其变慢,但不确定为什么会这么慢。有什么方法可以优化此代码或查询,或者还有其他我应该看的东西吗?

I have following Core Data fetch request running on iPad using iOS 4.3.2.

NSPredicate *predicate = [NSPredicate predicateWithFormat:
                          @"feed.account.name == %@ AND feed.feedType == %@", accountName,feedType];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date" 
                                                               ascending:NO];

NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

fetchedResultsController=[self createFetchedResultsController:@"RssFeedItem"  predicate:predicate sortDescriptors:sortDescriptors];

[fetchedResultsController.fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:@"url",@"headline",@"isRead",@"origin",@"originId",@"date",nil]];


[fetchedResultsController.fetchRequest setFetchLimit:500];

[fetchedResultsController setDelegate:self];

[fetchedResultsController performFetch:&error];

return [fetchedResultsController fetchedObjects];

I have approx 688 rows of data in the SQLite database for RssFeedItem table, and less than 100 rows in all other tables. That query is logged using debug logging as:

CoreData: sql: SELECT t0.Z_ENT, t0.Z_PK, t0.ZURL, t0.ZHEADLINE, t0.ZISREAD, t0.ZORIGIN,           
CoreData: annotation: sql connection fetch time: 3.3854s
CoreData: annotation: total fetch execution time: 3.4178s for 688 rows.

And it runs very slow (more than 3 seconds). I have indexes on all the required search fields and sort fields. I am assuming maybe the 2 way join is making it slow, but not sure why it would be that slow. Is there any way to optimize this code or query, or is there something else I should look at?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

辞别 2024-11-11 23:46:29

我猜想减慢查询速度的行是

 [fetchedResultsController.fetchRequest setPropertiesToFetch:[NSArray 
 arrayWithObjects:@"url",@"headline",@"isRead",@"origin",@"originId",@"date",nil]];

[fetchedResultsController.fetchRequest setFetchLimit:500];

我很好奇,为什么需要一次获取所有这些属性(和 500 个项目)?

I guess the lines that are slowing your query down are

 [fetchedResultsController.fetchRequest setPropertiesToFetch:[NSArray 
 arrayWithObjects:@"url",@"headline",@"isRead",@"origin",@"originId",@"date",nil]];

and

[fetchedResultsController.fetchRequest setFetchLimit:500];

I'm curious, why do you need to fetch all those properties (and 500 items) at once?

我喜欢麦丽素 2024-11-11 23:46:29

这似乎有点慢。我不知道这是否会产生巨大的速度差异,但它应该会有所帮助,将您的谓词重新修改为:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"feed.feedType == %@ AND feed.account.name == %@", feedType, accountName];

您希望首先放置最便宜的条件,以便不必在所有情况下都检查更昂贵的条件。通过检查帐户名进行的加入要昂贵得多。另外,根据提要类型值的外观,将其切换为整数可能会更快(我假设它当前是字符串,因为它是对象)。

That seems a bit slow. I don't know if this will make a huge speed difference, but it should help a little, rework your predicate as:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"feed.feedType == %@ AND feed.account.name == %@", feedType, accountName];

You want to put the cheapest condition first so that the more expensive condition won't have to be checked in all cases. The join that happens with checking the account name is a lot more expensive. Also, depending on what the feed type value looks like it, it might be quicker to switch it to be an integer (I'm assuming it's currently a string since it's an object).

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