- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个包含大约 100GB 数据和 $match 表达式中每个字段的 mongodb,我有一个索引(单字段索引)。
现在我尝试了 aggregate() 并将 $project 作为管道的第一部分编写,$match 位于其后。
聚合运行并返回正确的结果,但需要几个小时!这真的只处理过滤后的 ($match) 数据,还是 mongo 聚合所有数据并在之后过滤?
在我的测试用例中,$match 筛选了大约 150MB(而不是 100GB 的完整数据大小)。
不小心,我更改了顺序,在管道定义中的 $project 之前写了 $match。这样,它在几秒钟内就完成了。
mongodb一般什么时候减少输入数据,是否也对$match中字段的索引做处理?
最佳答案
正如您所注意到的,管道运算符的顺序非常重要,尤其是在处理大型集合时。如果操作不当,您可能会耗尽内存,更不用说这个过程需要很长时间了。如前所述in the docs :
The following pipeline operators take advantage of an index when they occur at the beginning of the pipeline:
$match
$sort
$limit
$skip.
只要 $match
出现在前面,就可以使用索引。还注明了in the docs
The MongoDB aggregation pipeline streams MongoDB documents from one pipeline operator to the next to process the documents. Pipeline operators can be repeated in the pipe.
这意味着如果您的 $project
前面有一个 $match
,则它只能看到整个集合的一小部分。
关于mongodb - 蒙戈 : aggregate - $match before $project,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14987151/
我是一名优秀的程序员,十分优秀!