mongodb - MongoDB:查找后的操作需要很长时间
问题描述
我在使用大集合聚合 $lookup 时遇到速度问题。
这是两个请求。两者都包含在 $match 上。这个操作非常快。然后我做了一个 $lookup,这也很快。
之后,我对 $lookup 结果进行了操作。在第一种情况下,这个操作非常快,在第二种情况下,它非常慢。
第一个也是快速的请求:
db.getCollection('table1').aggregate([
{
"$match": {
"type": "test",
}
},
{
"$lookup": {
"from": "table2",
"localField": "name",
"foreignField": "name",
"as": "lookup_names"
}
},
{
"$project": {
"names": {
// reduce on lookup_names is very fast
"$reduce": {
"input": "$lookup_names",
"initialValue": ""
"in": {
"$concat": ["$$value.name", ".", "$$this.name"]
}
}
}
}
}
])
第二个缓慢的请求:
db.getCollection('table1').aggregate([
{
"$match": {
"type": "test"
}
},
{
"$lookup": {
"from": "table2",
"localField": "name",
"foreignField": "name",
"as": "lookup_names"
}
},
{
// match on lookup_names is very slow
"$match": {
"lookup_names.name": "test"
}
}
])
有人能解释一下为什么第二个请求很慢吗?也许给我一个解决方案让它更快?
解决方案
推荐阅读
- java - Java:尝试暂停程序时,整个程序冻结
- string-comparison - 我一直在尝试将从 2 个来源收到的名称相互匹配,并检查它们是否几乎匹配
- python-3.x - 如何检查图形是否包含负循环但无法从源头到达?
- java - 为什么“THIS”关键字将子对象表示为父类的方法?
- wordpress - 如何将所有页脚小部件合并到一个小部件
- php - 如果 elseIf 没有按预期工作,PHP 将返回简写
- r - 如何更改 R 中 matplot 函数中的 x 轴限制?
- r - 试图成为基准 dplyr vs data.table
- maven - jmeter-maven-plugin 存储库配置
- r - 在 r 中找到一个句子与许多其他句子的余弦相似度