核心数据:子查询性能问题
我一直在尝试查看是否有任何方法可以提高以下谓词的性能:
[NSPredicate predicateWithFormat:@"A=%@ and SUBQUERY($self.words,$k,$k.keyword IN %@).@count==%d",
<value for A>,
keywordList,
[keywordList count]];
我想做的是返回 A 的所有记录,这些记录的关键字全部包含在提供的数组 (keywordList )。我有一个大约有 2000 条记录的小型数据库。但是,每个实体的关键字范围为 40-300 个关键字。关键字表示为从 A 到称为关键字的实体的多对关系。上面的查询有效,但在我的 iPhone4 上运行大约需要 7 秒。我想看看我能做些什么来将其缩小到亚秒级响应。
谢谢, 迈克尔
I've been trying to see if there is any way I can improve on the performance of the following Predicate:
[NSPredicate predicateWithFormat:@"A=%@ and SUBQUERY($self.words,$k,$k.keyword IN %@).@count==%d",
<value for A>,
keywordList,
[keywordList count]];
What I'm trying to do is return all the records of A that have keywords that are ALL contained in the supplied array (keywordList). I have a small database of about 2000 records. However, the keywords for each Entity ranges from 40-300 keywords. The keywords are represented as a to-Many relationship from A to an entity called Keywords. The above query works but takes about 7 seconds to run on my iPhone4. I want to see what I can do to shrink that down to a sub-second response.
Thanks,
Michael
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这并不能完全解决问题,因为从 fetchedObjs 的交集返回的结果不正确。它不保证所有“A”对象都包含所有提供的关键字。事实上,返回的是一个空列表,整个过程实际上需要更长的时间。
我的模型或之前的答案一定有问题。我没有得到我期望的结果。返回“A”列表的一种替代方法是返回“A”的 ManagedObjectID 列表。 [mo valueForKey: (NSString *)] 返回关系的对象。
注意:我最初匿名发布这个问题,以为我已经登录,所以我似乎无法评论任何人的答案。
This doesn't quite solve the problem as the results that come back from intersection of the fetchedObjs is not correct. It doesn't guarantee that all the 'A' objects contain all the supplied keywords. In fact, what comes back is an empty list and the entire process actually takes longer.
Something must be wrong with my model or the previous answer. I'm not getting the results I'm expecting. One alternative to returning a list of 'A' would be to return a list of ManagedObjectIDs of 'A'. [mo valueForKey: (NSString *)] returns the object of the relationship.
Note: I originally posted this question anonymously thinking I was logged in, so i can't seem to comment on anybody's answer.
我认为你正在倒退地处理这个问题。如果您有关键字,则应搜索
Keyword
对象,然后遍历它们的A
关系以查找相关的A
对象。然后检查关系集中是否有重叠。因此,类似:
...
inCommon
将包含一组共享所有关键字的所有唯一A
对象。更新:
来自OP作者:
好吧,我们采取另一种策略。假设您有一个看起来像这样的数据模型:
那么这样的东西应该可以工作:
I think you are approaching the problem backwards. If you have the keywords, you should search for the
Keyword
objects and then walk theirA
relationships to find the relatedA
objects.Then check for overlap in the relationship sets.So something like:
...
inCommon
would then contain a set of all uniqueA
objects that shared all the keywords.Update:
From OP author:
Okay, lets take another tack. Assuming you have a data model that looks something like this:
Then something like this should work: