[英]My mongodb aggregation query is taking too much time. How can I enhance the performance?
I'm joining two collection using aggregation and using "output" to generate a new collection with the joined data, but it's taking too much time(never ends).我正在使用聚合加入两个集合,并使用“输出”来生成一个包含加入数据的新集合,但这需要太多时间(永远不会结束)。 How can I enhance the performance?
我怎样才能提高性能? The collection A has 260k documents and collection B has 17.2m.
集合 A 有 260k 文档,集合 B 有 17.2m。
I have already tested the same script with different data sets and it works fine.我已经用不同的数据集测试了相同的脚本,它工作正常。 At first glance, issue seems to be related with the size of collections.
乍一看,问题似乎与 collections 的大小有关。
db.colection_A.aggregate([
{
$match : { property_X: "X" }
},
{ "$lookup":
{
from: "collection_B",
localField: "property_A",
foreignField: "property_B",
as: "joined_data"
}
},
{ $unwind:
{
path: "$joined_data",
preserveNullAndEmptyArrays: false
}
},
{ $project:
{
"_id": 0,
"joined_data": 1
}
},
{ $replaceRoot:
{ newRoot: "$joined_data" }
},
//{ $limit : 1 }
{ $out: "new_collection"}
]);
Expected result is creation of the collection "new_collection" containing data filtered in the "match" and "lookup" conditions.预期结果是创建包含在“匹配”和“查找”条件中过滤的数据的集合“new_collection”。
The aggregation query was working fine, the problem was related with indexing in Mongo.聚合查询工作正常,问题与 Mongo 中的索引有关。 After creation of the index, the query performed much better.
创建索引后,查询执行得更好。 Index was created with:
索引是通过以下方式创建的:
db.collection_B.createIndex( { property_B: 1 } );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.