MongoDB 中的 $lookup 是否比 SQL 数据库中涉及索引的联接慢?

问题描述 投票:0回答:1
db.inventory.aggregate( [
   {
     $lookup:
       {
         from: "order",
         localField: "_id",
         foreignField: "item_id",
         as: "inventory_docs"
       }
  }
] )

$lookup
是基于
item_id
字段加入的,该字段已建立索引。 如果 100,000 个文档通过此
$lookup
,查询时间会比没有此
$lookup
增加 4 倍。

鉴于

$lookup
已建立索引,查询速度会慢 4 倍是出乎意料的。我预计查询时间会略有增加。

SQL 数据库也是如此吗?索引连接会使查询时间增加 4 倍吗?

sql database mongodb join nosql
1个回答
0
投票

我建议您对该聚合进行解释。 检查索引是否正在使用,并确保查找是否花费了所有额外时间。 您可以在此处分享解释日志以提供更多上下文 您可以找到用于解释聚合管道的文档:

https://www.mongodb.com/docs/manual/reference/method/db.collection.explain/

© www.soinside.com 2019 - 2024. All rights reserved.