MongoDB db.collection.explain()
2024-04-25 14:15 abce 阅读(46) 评论(0) 收藏 举报db.collection.explain() 封装了 explain 命令,推荐使用 db.collection.explain() 来执行 explain 命令。
db.collection.explain()返回以下方法的查询计划:aggregate()、count()、find()、remove()、distinct()、findAndModify()
因此,db.collection.explain()的使用方法就是在后面加上上面列举出的方法,格式:
db.collection.explain().<method(...)>
比如:
db.products.explain().remove( { category: "apparel" }, { justOne: true } )
具体可以查看一下帮助:
> 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 生成新的执行计划条目。
示例
db.collection
.explain("executionStats")
.aggregate([
{ $match: { col1: "col1_val" }},
{ $group: { _id: "$id", total: { $sum: "$amount" } } },
{ $sort: { total: -1 } }
])

浙公网安备 33010602011771号