MongoDB {聚合 $match} 与 {find} 速度 [英] MongoDB {aggregation $match} vs {find} speed
问题描述
我有一个包含数百万行的 mongoDB 集合,我正在尝试优化我的查询.我目前正在使用聚合框架来检索数据并根据需要对它们进行分组.我的典型聚合查询类似于: $match >$组>$组>$项目
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.
我尝试仅使用 $match 过滤器执行查询,然后使用 collection.find 执行相同的查询.聚合查询大约需要 80 毫秒,而查找查询需要 0 或 1 毫秒.
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 ?
我可以使用查找查询而不是聚合查询,但是我必须在请求之后执行大量处理,并且可以使用 $group
等快速完成此过程,所以我宁愿保留聚合框架.
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.
谢谢,
这是我的标准:
{
"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.
正如您所说,您还可以使用多个 find
查询,但请记住,您不能使用 find
查询创建新字段.另一方面,$group
阶段允许您定义新字段.
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.
如果您想实现 聚合框架
的功能,您很可能必须运行初始 find
(或链接多个),提取该信息并使用编程语言进一步操作它.
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.
聚合管道
可能看起来需要更长的时间,但至少您知道您只需要考虑一个系统的性能 - MongoDB 引擎.
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.
然而,在处理从 find
查询返回的数据时,您很可能必须使用编程语言进一步处理数据,从而增加复杂性,具体取决于选择的编程语言.
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 {聚合 $match} 与 {find} 速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!