使用Core Data时是否可以使用复杂索引?

sco*_*ter 9 database indexing core-data objective-c ios

我正在开发一款iOS闪存卡式学习应用程序,在加载时需要从Core Data中获取大量数据.但是我需要的数据是实体的一个相当特定的子集,基于用户设置,所以有多个谓词涉及测试等价.我发现这些提取速度非常慢,根据对SQLite的研究,我认为索引在这里是一个不错的选择.

现在,我理解(主要是通过阅读其他stackoverflow问题)SQLite和Core Data是两个不同的,基本上是正交的东西,不应该混淆.但是我的理解是你应该通过Core Data来完成任何类型的数据库工作和调整; 在优化或设计应用程序中的对象持久性时,不应试图绕过并直接使用SQLite.

但我唯一可以在Core Data中找到索引的是模型中每个属性的一个"索引"复选框.而这只是没有做我正在寻找的那种优化.

这是获取请求,目前:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"SKUserItem" inManagedObjectContext:context];
fetchRequest.entity = entity;

NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"next" ascending:YES] autorelease];
fetchRequest.sortDescriptors = [NSArray arrayWithObject:sortDescriptor];

NSMutableArray *predicates = [NSMutableArray arrayWithCapacity:6];
[predicates addObject:[NSPredicate predicateWithFormat:@"next < %f", now() + (60.0*60.0*24.0)]];
[predicates addObject:[NSPredicate predicateWithFormat:@"next > %f", nextOffset]];
[predicates addObject:[NSPredicate predicateWithFormat:@"user == %@", user]];
[predicates addObject:[NSPredicate predicateWithFormat:@"langRaw == %d", lang]];

NSArray *stylePredicates = [NSArray arrayWithObjects:[NSPredicate predicateWithFormat:@"styleRaw == %d", SK_SIMP_AND_TRAD], [NSPredicate predicateWithFormat:@"styleRaw == %d", self.style], nil];
[predicates addObject:[NSCompoundPredicate orPredicateWithSubpredicates:stylePredicates]];

if([self.parts count] == 4 || (self.lang == SK_JA && [self.parts count] == 3))
    ;  // don't have to filter by parts; they're studying all of them
else {
    NSMutableArray *partPredicates = [NSMutableArray arrayWithCapacity:[self.parts count]];
    for(NSString *part in self.parts)
        [partPredicates addObject:[NSPredicate predicateWithFormat:@"partRaw == %d", partCode(part)]];
    [predicates addObject:[NSCompoundPredicate orPredicateWithSubpredicates:partPredicates]];
}

NSPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:predicates];
fetchRequest.predicate = compoundPredicate;
Run Code Online (Sandbox Code Playgroud)

所以这个fetch所做的基本上是下一个(给定项目到期的时间)和用户名,正在研究的语言,正在研究的样式(中文有简化和传统)和正在研究的部分(写作,音调)的过滤器,阅读或定义),仅在"下一个"范围内获取.以下是我从调整和摆弄这些事情中学到的一些简短列表:

  1. 它总是扫描整个表,或似乎.虽然next是索引的,但即使我强制它搜索我知道也不会返回任何内容的范围,它仍然需要几秒钟才能完成获取.
  2. 谓词,任意数量的谓词,都会让这个变慢.如果我删除了一些但不是全部,那就慢了.如果我删除所有谓词(从而打破了应用程序),那么它会更快.
  3. 速度在很大程度上取决于表中总共有多少UserItem.物品越多,越慢.有些人可以拥有成千上万的物品,而且这个物品的获取时间可能需要10秒才能完成.这导致我的应用程序出现尴尬的停顿.
  4. 添加下一个值的上限不是因为我们需要它,而是因为它加速了一点点提取.
  5. 让查询返回字典中的属性的子集(而不是整个托管对象)并且懒惰地获取其余属性更快,但仍然不够快.

我来自Google App Engine,所以我习惯了他们在那里提供的索引.基本上我想要那种索引,但是通过Core Data应用于SQLite.我找到了有关在SQLite中添加索引的信息,我想要的那种,但是通过Core Data进行这种索引,我找不到任何关于它的信息.

Dan*_*ert 18

你想要的是Core Data在iOS 5.0及更高版本中支持的复合索引.

您可以在Xcode中进行设置:实体检查器具有索引部分,或者如果您要创建输入NSEntityDescription代码,请使用-setCompoundIndexes:.

如果你使用Xcode,你可以在索引部分添加一行说明

next,user,langRaw
Run Code Online (Sandbox Code Playgroud)

这样SQL就可以为您的查询使用索引.