聚合框架不能使用索引 [英] Aggregate framework can't use indexes

查看:53
本文介绍了聚合框架不能使用索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行以下命令:

db.ads_view.aggregate({$ group:{_id:"$ campaign","action":{$ sum:1}}});

db.ads_view.aggregate({$group: {_id : "$campaign", "action" : {$sum: 1} }});

ads_view:500 000个文档.

ads_view : 500 000 documents.

此查询需要1.8秒.这是它的配置文件: https://gist.github.com/afecec63a994f8f7fd8a

this queries take 1.8s . this is its profile : https://gist.github.com/afecec63a994f8f7fd8a

已编制索引:db.ads_view.ensureIndex({campaign:1});

indexed : db.ads_view.ensureIndex({campaign: 1});

但是mongodb不使用索引.任何人都知道聚合框架是否可以使用索引,以及如何为该查询建立索引.

But mongodb don't use index. Anyone know if can aggregate framework use indexes, how to index this query.

推荐答案

$group运算符不是当前将使用索引的运算符之一. (自2.2起)的运算符列表为:

The $group operator is not one of the ones that will use an index currently. The list of operators that do (as of 2.2) are:

$match
$sort
$limit
$skip

从这里:

http://docs.mongodb.org/manual/applications/aggregation/#pipeline-operators-and-indexes

根据要点上发生的产量的数量,我假设您要么有一个非常活跃的实例,要么在进行组时内存中没有很多此类数据(通常会出现页面错误的产量)也是如此),因此是1.8秒

Based on the number of yields going on in the gist, I would assume you either have a very active instance or that a lot of this data is not in memory when you are doing the group (it will yield on page fault usually too), hence the 1.8s

请注意,即使$group可以使用索引,并且您的索引涵盖了所有要分组的内容,它仍将涉及对索引的完整扫描以进行分组,而且无论如何都不会很快.

Note that even if $group could use an index, and your index covered everything being grouped, it would still involve a full scan of the index to do the group, and would likely not be terrible fast anyway.

这篇关于聚合框架不能使用索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆