代码改变世界

MongoDB db.collection.explain()

  abce  阅读(27)  评论(0编辑  收藏  举报

db.collection.explain() 封装了 explain 命令,推荐使用 db.collection.explain() 来执行 explain 命令。

 

db.collection.explain()返回以下方法的查询计划:aggregate()、count()、find()、remove()、distinct()、findAndModify()

 

因此,db.collection.explain()的使用方法就是在后面加上上面列举出的方法,格式:

1
db.collection.explain().<method(...)>

 

比如:

1
db.products.explain().remove( { category: "apparel" }, { justOne: true } )

 

具体可以查看一下帮助:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
> db.collection.explain().help()
Explainable operations
        .aggregate(...) - explain an aggregation operation
        .count(...) - explain a count operation
        .distinct(...) - explain a distinct operation
        .find(...) - get an explainable query
        .findAndModify(...) - explain a findAndModify operation
        .mapReduce(...) - explain a mapReduce operation
        .remove(...) - explain a remove operation
        .update(...) - explain an update operation
Explainable collection methods
        .getCollection()
        .getVerbosity()
        .setVerbosity(verbosity)

db.products.explain()返回的信息量,取决与 verbosity 的取值。verbosity 有三种模式,定义了 explain 输出结果的详细模式:

·缺省是queryPlanner,返回优化器选中的执行计划,但不会执行。

·executionStats:选出执行计划,执行执行计划并返回执行的统计信息;对于写操作,并不会真正将修改应用到数据库

·allPlansExecution:选出执行计划,执行执行计划并返回执行的统计信息的同时,还会返回其它候选执行计划

 

db.collection.explain() 使用 explain 跳过已经存在的缓存计划,并阻止 mongodb 生成新的执行计划条目。

 

示例

1
2
3
4
5
6
7
db.collection
    .explain("executionStats")
    .aggregate([
        { $match: { col1: "col1_val" }},
        { $group: { _id: "$id", total: { $sum: "$amount" } } },
        { $sort: { total: -1 } }
    ])
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2022-04-25 lsblk的使用
2022-04-25 SQLServer中的索引类型
2022-04-25 将数据库迁移到云之前需要考虑的一些事项
2016-04-25 DG - physical standby failover切换过程
点击右上角即可分享
微信分享提示