MongoDB {aggregation $ match} vs {find}速度 [英] MongoDB {aggregation $match} vs {find} speed

查看:113
本文介绍了MongoDB {aggregation $ match} vs {find}速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有数百万行的mongoDB集合,我正在尝试优化查询.我目前正在使用聚合框架来检索数据并根据需要对其进行分组.我典型的聚合查询类似于:$match > $group > $ group > $project

但是,我注意到最后的部分只花了几毫秒,而开始的时间最慢.

我尝试仅使用$ match过滤器执行查询,然后使用collection.find执行相同的查询.聚合查询大约需要80毫秒,而查找查询则需要0或1毫秒.

我几乎在每个字段上都有索引,所以我想这不是问题.对可能出问题的任何想法吗?还是仅仅是聚合框架的正常"缺陷?

我可以使用查找查询代替聚合查询,但是在请求之后我必须执行很多处理,并且可以使用$group等快速完成此过程.因此,我宁愿保留聚合框架.

谢谢

这是我的标准:

{
    "action" : "click",
    "timestamp" : {
            "$gt" : ISODate("2015-01-01T00:00:00Z"),
            "$lt" : ISODate("2015-02-011T00:00:00Z")
    },
    "itemId" : "5"
}

解决方案

aggregation framework的主要目的是简化对大量条目的查询,并生成少量对您有价值的结果. /p>

如前所述,您也可以使用多个find查询,但是请记住,不能使用find查询创建新字段.另一方面,$group阶段允许您定义新字段.

如果您想实现aggregation framework的功能,则很可能必须运行一个初始的find(或链接多个),提取该信息并使用编程语言对其进行进一步的操作.

aggregation pipeline似乎需要更长的时间,但至少您知道您只需要考虑一个系统的性能-MongoDB引擎.

在处理从find查询返回的数据时,您很可能不得不使用一种编程语言来进一步处理数据,从而根据所选择的编程语言的复杂性而增加了复杂性.

I have a mongoDB collection with millions of rows and I'm trying to optimize my queries. I'm currently using the aggregation framework to retrieve data and group them as I want. My typical aggregation query is something like : $match > $group > $ group > $project

However, I noticed that the last parts only take a few ms, the beginning is the slowest.

I tried to perform a query with only the $match filter, and then to perform the same query with collection.find. The aggregation query takes ~80ms while the find query takes 0 or 1ms.

I have indexes on pretty much each field so I guess this isn't the problem. Any idea on what could go wrong ? Or is it just a "normal" drawback of the aggregation framework ?

I could use find queries instead of aggregation queries, however I would have to perform a lot of processing after the request and this process can be done quickly with $group etc. so I would rather keep the aggregation framework.

Thanks,

EDIT :

Here is my criteria :

{
    "action" : "click",
    "timestamp" : {
            "$gt" : ISODate("2015-01-01T00:00:00Z"),
            "$lt" : ISODate("2015-02-011T00:00:00Z")
    },
    "itemId" : "5"
}

解决方案

The main purpose of the aggregation framework is to ease the query of a big number of entries and generate a low number of results that hold value to you.

As you have said, you can also use multiple find queries, but remember that you can not create new fields with find queries. On the other hand, the $group stage allows you to define your new fields.

If you would like to achieve the functionality of the aggregation framework, you would most likely have to run an initial find (or chain several ones), pull that information and further manipulate it with a programming language.

The aggregation pipeline might seem to take longer, but at least you know you only have to take into account the performance of one system - MongoDB engine.

Whereas, when it comes to manipulating the data returned from a find query, you would most likely have to further manipulate the data with a programming language, thus increasing the complexity depending on the intricacies of the programming language of choice.

这篇关于MongoDB {aggregation $ match} vs {find}速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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