带索引的MongoDB $lookup比SQL关联慢?4倍耗时疑问
带索引的关联查询耗时异常增加的对比疑问
MongoDB场景说明
以下是使用$lookup的MongoDB聚合查询语句:
db.inventory.aggregate( [ { $lookup: { from: "order", localField: "_id", foreignField: "item_id", as: "inventory_docs" } } ] )
- 该
$lookup依赖的order集合item_id字段已建立索引 - 当处理100,000条文档时,带
$lookup的查询耗时是无$lookup时的4倍,远超“仅小幅增加”的预期
补充执行计划
对应的执行计划文档如下:
{ "$lookup": { "from": "order", "as": "inventory_docs", "localField": "_id", "foreignField": "item_id", "let": {}, "pipeline": [ { "$project": { "_id": 1 } } ] }, "totalDocsExamined": 0, "totalKeysExamined": 100008, "collectionScans": 0, "indexesUsed": [ "_id_" ], "nReturned": 100008, "executionTimeMillisEstimate": 18801 }
从执行计划可看出:基于索引字段_id查询100,000条文档耗时约18秒,速度明显过慢。
核心疑问
SQL数据库中,带索引的关联查询是否也会出现耗时增至4倍的情况?
内容的提问来源于stack exchange,提问作者Bear Bile Farming is Torture
相关产品推荐
相关产品推荐

