现在,我明白(主要是从阅读其他stackoverflow问题),sqlite和核心数据是两个不同的基本正交的东西,不应该被混淆.但是我也理解,你应该通过核心数据来做任何数据库的工作和调整;在优化或设计应用程序中的对象永久性时,不应尝试绕过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 themelse { 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;
所以本质上这个提取是什么排序在下一个(给定的项目到期的时间)和筛选用户名,正在研究的语言,正在研究的风格(中文有简化和传统)和被研究的部分(写作,音调,阅读或定义),并且仅在“下一个”范围内提取.以下是我从调整中学到的简短列表:
>它总是扫描整个表,或者似乎.虽然下一个被索引,即使我迫使它搜索一个范围,我知道将不会返回任何东西,仍然需要几秒钟的抓取完成.
>谓词,任意数量的谓词使得这个慢.如果我删除一些但不是全部,它的速度很慢.如果我删除所有谓词(从而打破了应用程序),那么它要快得多.
>速度很大程度上取决于表中总共有多少个UserItem.更多的项目,这是越慢.有些人可以拥有成千上万的物品,那就是这个抓取时间可能需要10秒才能完成.这在我的应用程序中导致尴尬的停顿.
>下一个值的上限不是因为我们需要添加的,而是因为它加速了一点.
>让查询返回字典中的属性的子集(而不是整个受管对象),并且其余的延迟更快,但仍然不够快.
我来自Google App Engine,所以我习惯了他们在那里提供的索引.基本上我想要这种索引,但是通过核心数据应用于sqlite.我发现关于在sqlite中添加索引的信息,我想要的那种,但是通过Core Data进行这种索引,我找不到任何信息.
解决方法 您想要的是核心数据在iOS 5.0及更高版本中支持的复合索引.您可以在Xcode中进行设置:实体检查器具有索引部分,或者如果要在代码中创建NSEntityDescription,请使用-setCompoundindexes :.
如果您使用Xcode,您可以在“索引”部分中添加一行
next,user,langraw
这样sql可以为您的查询使用索引.
总结以上是内存溢出为你收集整理的objective-c – 使用Core Data可以使用复杂的索引吗?全部内容,希望文章能够帮你解决objective-c – 使用Core Data可以使用复杂的索引吗?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)