在收集具有大集合的$ 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"
}
}
])
有人可以解释一下为什么第二个请求很慢吗?也许给我一个解决方案以使其更快?