iphone Core Data - 为什么核心数据这么慢?

发布于 2024-11-05 19:45:55 字数 1858 浏览 0 评论 0原文

我在 CoreData 中的表有大约 8 列,全部都是字符串。最大字符串长度小于10K。

首先,我使用以下代码来检索行:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"Item" inManagedObjectContext:context]];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"(URL ==[c] %@)", url]];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Time" ascending:NO selector:nil];
NSArray *descriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:descriptors];
[sortDescriptor release];

NSError *error = nil;
NSArray *array = [context executeFetchRequest:fetchRequest error:&error];

这是性能日志:

2011-05-10 11:47:18.743 Test2[2176:5907] CoreData: 注释: sql 连接获取时间: 2.6172s

2011-05-10 11:47:21.600 Test2[2176:5907] CoreData:注释:总获取执行时间:144 行 2.8577 秒。

144 行超过 2 秒?

然后我为每一行构造一个非 coredata 对象:

if ([array count] > 0) {
    for (Item *item in array) {
        SimpleItem *sitem = [[SimpleItem alloc] init];
        sitem.Title = item.Title;
        sitem.Summary = item.Summary;
        sitem.URL = item.URL;
        [dataDict setObject:sitem forKey:item.URL];
        [sitem release];
    }
    [dataDict release];
}

上述代码的性能日志(我认为是一行读取)的一部分是:

2011-05-10 11:47:21.617 Test2[2176:5907] CoreData: sql: 选择 0、t0.Z_PK、t0.Z_OPT、t0.ZURL、t0.ZSUMMARY、、t0.ZIMAGEURL、t0.ZTITLE , t0.ZURL, t0.ZTIME从 ZITEM t0 到 t0.Z_PK = ?

2011-05-10 11:47:21.629 Test2[2176:5907] CoreData: 注释: sql 连接获取时间: 0.0098s

2011-05-10 11:47:21.633 Test2[2176:5907] CoreData:注释:总获取执行时间:1 行 0.0162 秒。

看来故障还是有的。

任何人都可以帮助我提高性能吗?

另外,当我在 xcode 中编辑数据模型时,我检查了 URL 的“索引”选项。这足以建立索引吗?

My table in the CoreData has like 8 columns, all are strings. The largest string's length is less than 10K.

First, I use the following codes to retrieve the rows:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"Item" inManagedObjectContext:context]];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"(URL ==[c] %@)", url]];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Time" ascending:NO selector:nil];
NSArray *descriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:descriptors];
[sortDescriptor release];

NSError *error = nil;
NSArray *array = [context executeFetchRequest:fetchRequest error:&error];

Here is the performance log:

2011-05-10 11:47:18.743 Test2[2176:5907] CoreData: annotation: sql connection fetch time: 2.6172s

2011-05-10 11:47:21.600 Test2[2176:5907] CoreData: annotation: total fetch execution time: 2.8577s for 144 rows.

144 rows for more than 2 secs?

Then I construct an non-coredata object for each row:

if ([array count] > 0) {
    for (Item *item in array) {
        SimpleItem *sitem = [[SimpleItem alloc] init];
        sitem.Title = item.Title;
        sitem.Summary = item.Summary;
        sitem.URL = item.URL;
        [dataDict setObject:sitem forKey:item.URL];
        [sitem release];
    }
    [dataDict release];
}

part of The performance log ( i think for one row read) for the above codes is:

2011-05-10 11:47:21.617 Test2[2176:5907] CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZURL, t0.ZSUMMARY, , t0.ZIMAGEURL, t0.ZTITLE, t0.ZURL, t0.ZTIME FROM ZITEM t0 WHERE t0.Z_PK = ?

2011-05-10 11:47:21.629 Test2[2176:5907] CoreData: annotation: sql connection fetch time: 0.0098s

2011-05-10 11:47:21.633 Test2[2176:5907] CoreData: annotation: total fetch execution time: 0.0162s for 1 rows.

It seems faults are there.

Anyone can help me to improve the performance?

Also, I have checked "Index" option for URL when I edit the data model in xcode. Is that enough for setting up an index?

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

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

发布评论

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

评论(1

何以笙箫默 2024-11-12 19:45:55

听起来您没有在列上使用任何索引,这可能有助于加快速度,但如果您搜索长度为 10K 的字符串,CoreData 可能不是最佳选择。

CoreData 不是数据库 - 它处理某些事情的效率不如仅使用 sqlite。在 CoreData 中,某些操作的对象完全加载到内存中 - 将 10K * 144 条记录加载到内存中可能不是世界上最快的事情:)

尝试在 sqlite 中编写数据库代码,看看它是否更快。

It sounds like you haven't used any indexes on your columns, that might help speed things up a little but if you're searching strings 10K in length, CoreData probably isn't the way to go.

And CoreData isn't a database - it deals with some things less efficiently that just using sqlite. In CoreData, objects are completely loaded into memory for some operations - loading 10K * 144 records into memory probably won't be the fastest thing in the world :)

Try writing your database code in sqlite and see if it's any faster.

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