如果mongodb aggregate不为$lookup使用索引,为什么使用索引时性能会提高?
我有以下代码片段来运行聚合命令如果mongodb aggregate不为$lookup使用索引,为什么使用索引时性能会提高?,mongodb,Mongodb,我有以下代码片段来运行聚合命令 console.time("something"); const cursor = await db.collection("main").aggregate([ { $match: { mainField: mainField, }, }, { $lookup: { from: "re
console.time("something");
const cursor = await db.collection("main").aggregate([
{
$match: {
mainField: mainField,
},
},
{
$lookup: {
from: "reference",
localField: "referenceId",
foreignField: "referenceField",
as: "something",
},
},
]);
const results = await cursor.toArray();
console.timeEnd("something");
我有一个便宜的云服务器用于测试(2gb ram、1个cpu等),mongodb存储在那里
我将10k个文档插入到主集合和参考集合中(因此合并插入了20k个文档)
如果不使用索引并运行上述聚合查询,则返回结果需要30秒以上的时间
如果我在引用集合上有以下索引并运行上面的聚合查询,那么结果大约需要1.2秒
await db.collection("reference").createIndex({ referenceField: 1 });
不幸的是,MongoDB手册目前没有提到
$lookup
的潜在索引用法,但事实确实如此
与您的示例类似的一个简单的$lookup
查询会对另一个集合中的foreignField
执行相等匹配,因此您添加了正确的索引以提高性能(假设此字段也具有合理的选择性)
在MongoDB 4.0中,
$lookup
的索引用法未在中报告。MongoDB问题跟踪器中有一个相关的问题需要关注/投票:。我创建了一个文档建议()来提到MongoDB 3.4中的索引使用,但它在积压工作中没有发挥作用。MongoDB3.6增加了,因此可能的解释选项现在更广泛了。然而,索引使用的可能性至少应该在手册中提到,所以我将提交一份PR来改进这一点。