iphone Core Data - 为什么核心数据这么慢?
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您没有在列上使用任何索引,这可能有助于加快速度,但如果您搜索长度为 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.