支持下列操作返回查询计划
aggregate(); count(); distinct(); find(); group(); remove(); update()
cursor.explain(verbosity) 为一个游标返回其查询执行计划(Reports on the query execution plan for a cursor)
cursor.explain(verbosity) 最通常的行式为db.collection.find().explain()。其中verbosity说明返回信息的粒度。
执行计划中几类常见的操作描述
COLLSCAN 全表扫描
IXSCAN 索引扫描
FETCH 根据索引去检索文档
SHARD_MERGE 合并分片结果
db.collection.find().explain(verbose)
explain()输出一个以文档形式展现的执行计划,可以包括统计信息(可选)。
参数verbose:
可选参数。缺省值为queryPlanner,用于查看指定执行计划的特定部分。即给定不同的参数则输出信息的详细程度不同
常用的包括queryPlanner,executionStats,以及allPlansExecution
queryPlanner模式
这个是缺省模式。
MongoDB运行查询优化器对当前的查询进行评估并选择一个最佳的查询计划
executionStats模式
mongoDB运行查询优化器对当前的查询进行评估并选择一个最佳的查询计划进行执行
在执行完毕后返回这个最佳执行计划执行完成时的相关统计信息
对于写操作db.collection.explain()返回关于更新和删除操作的信息,但是并不将修改应用到数据库
对于那些被拒绝的执行计划,不返回其统计信息
allPlansExecution模式
该模式是前2种模式的更细化,即会包括上述2种模式的所有信息
即按照最佳的执行计划执行以及列出统计信息,而且还会列出一些候选的执行计划
如果有多个查询计划 ,executionStats信息包括这些执行计划的部分统计信息
db.collection.explain().find()
该方法与db.collection.find().explain()类似,但是存在以下关键差异
The db.collection.explain() method wraps the explain command and is the preferred way to run explain.
db.collection.explain().find() is similar to db.collection.find().explain() with the following key differences:
The db.collection.explain().find() construct allows for the additional chaining of query modifiers.
For list of query modifiers, see db.collection.explain().find().help().
The db.collection.explain().find() returns a cursor, which requires a call to .next(), or its alias .finish(),
to return the explain() results. If run interactively in the mongo shell, the mongo shell
automatically calls .finish() to return the results. For scripts, however, you must explicitly call .next(),
or .finish(), to return the results. For list of cursor-related methods, see db.collection.explain().find().help().
db.collection.explain().aggregate() is equivalent to passing the explain option to the db.collection.aggregate() method.
//获取explain的支持的运算方法
> 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
.group(...) - explain a group operation
.remove(...) - explain a remove operation
.update(...) - explain an update operation
Explainable collection methods
.getCollection()
.getVerbosity()
.setVerbosity(verbosity)
//获取explain().find()支持的运算方法
> db.collection.explain().find().help()
Explain query methods
.finish() - sends explain command to the server and returns the result
.forEach(func) - apply a function to the explain results
.hasNext() - whether this explain query still has a result to retrieve
.next() - alias for .finish()
Explain query modifiers
.addOption(n)
.batchSize(n)
.comment(comment)
.count()
.hint(hintSpec)
.limit(n)
.maxTimeMS(n)
.max(idxDoc)
.min(idxDoc)
.readPref(mode, tagSet)
.showDiskLoc()
.skip(n)
.snapshot()
.sort(sortSpec)
"port"
: 27017,
"version" : "3.2.10",
"gitVersion" : "79d9b3ab5ce20f51c272b4411202710a082d0317"
"ok" : 1
//注意,下面将explain()放置到count()之后提示错误
> db.users.count().explain()
2016-12-08T16:53:22.760+0800 E QUERY [thread1] TypeError: db.users.count(...).explain is not a function :
@(shell):1:1
2、演示db.collection.explain().update()用法
//先插入一个文档
> db.example.insert({id:1,ename:"leshami",blog:"http://blog.csdn.net/leshami"})
WriteResult({ "nInserted" : 1 })
//下面查看update的执行计划
> db.example.explain().update({id:1},{$set:{name:"robinson_0612"}})
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "test.example",
"indexFilterSet" : false,
"parsedQuery" : {
"id" : {
"$eq" : 1
"winningPlan" : {
"stage" : "UPDATE",
"inputStage" : {
"stage" : "COLLSCAN",
"filter" : {
"id" : {
"$eq" : 1
"direction" : "forward"
"rejectedPlans" : [ ]
"serverInfo" : {
"host" : "node233.edq.com",
"port" : 27017,
"version" : "3.2.10",
"gitVersion" : "79d9b3ab5ce20f51c272b4411202710a082d0317"
"ok" : 1
//再次查看文档,如下,文档并没有被更新,正如前文所述,该方式并不将修改应用到数据库
> db.example.find().pretty()
"_id" : ObjectId("584924b4de4a7c9eeef9ef9d"),
"id" : 1,
"ename" : "leshami",
"blog" : "http://blog.csdn.net/leshami"
//同样将update前置到explain之前也是错误的
> db.example.update({id:1},{$set:{name:"robinson_0612"}}).explain()
2016-12-08T17:24:24.708+0800 E QUERY [thread1] TypeError: db.example.update(...).explain is not a function :
@(shell):1:1
3、执行计划相关描述
> db.persons.find({age:26}).explain()
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "test.persons",
"indexFilterSet" : false,
"parsedQuery" : {
"age" : {
"$eq" : 26
"winningPlan" : {
"stage" : "COLLSCAN",
"filter" : {
"age" : {
"$eq" : 26
"direction" : "forward"
"rejectedPlans" : [ ]
"serverInfo" : {
"host" : "node233.edq.com",
"port" : 27017,
"version" : "3.2.10",
"gitVersion" : "79d9b3ab5ce20f51c272b4411202710a082d0317"
"ok" : 1
4、执行计划统计信息描述
> db.inventory.find({id:500}).explain("executionStats")
"queryPlanner" : {
.........
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 1,
"executionTimeMillis" : 21896,
"totalKeysExamined" : 0,
"totalDocsExamined" : 5000000,
"executionStages" : {
"stage" : "COLLSCAN",
"filter" : {
"id" : {
"$eq" : 500
"nReturned" : 1,
"executionTimeMillisEstimate" : 19230,
"works" : 5000002,
"advanced" : 1,
"needTime" : 5000000,
"needYield" : 0,
"saveState" : 39065,
"restoreState" : 39065,
"isEOF" : 1,
"invalidates" : 0,
"direction" : "forward",
"docsExamined" : 5000000
"serverInfo" : {
...........
"ok" : 1
https:
5、获取所有的执行计划
//如下所示,由于当前我们没有多个执行计划,因此仅返回一个执行计划
> db.persons.find({age:26}).explain("allPlansExecution")
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "test.persons",
"indexFilterSet" : false,
"parsedQuery" : {
"age" : {
"$eq" : 26
"winningPlan" : {
"stage" : "COLLSCAN",
"filter" : {
"age" : {
"$eq" : 26
"direction" : "forward"
"rejectedPlans" : [ ]
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 3,
"executionTimeMillis" : 0,
"totalKeysExamined" : 0,
"totalDocsExamined" : 11,
"executionStages" : {
"stage" : "COLLSCAN",
"filter" : {
"age" : {
"$eq" : 26
"nReturned" : 3,
"executionTimeMillisEstimate" : 0,
"works" : 13,
"advanced" : 3,
"needTime" : 9,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"direction" : "forward",
"docsExamined" : 11
"allPlansExecution" : [ ]
"serverInfo" : {
"host" : "node233.edq.com",
"port" : 27017,
"version" : "3.2.10",
"gitVersion" : "79d9b3ab5ce20f51c272b4411202710a082d0317"
"ok" : 1
前言cursor.explain("executionStats") 和 db.collection.explain("executionStats") 方法提供一个 查询的性能统计情况。这些数据输出...
来自: Defonds 的专栏
此系列主要是作为自己学习记录,原文地址:https://blog.csdn.net/u012702547/article/details/80606614-----------------------...
来自: _我不是二狗的博客
Returns information on the query plan for the following operations: aggregate(); count();distinct();...
来自: database_shaofei的专栏
mongodb中的集合是非结构化的,所以列的个数,类型之类的没有什么限制,很随意,构建一个测试集合
db.test.find()
{ “_id” : ObjectId(“57bd09252e...
来自: 敖尔其楞的专栏
查询计划的生成, 主要是通过QueryPlanner::plan 作为入口, 里面包含了基本的逻辑, 但是真正的QuerySolutionNode 是通过QueryPlannerAccess类来实现。...
来自: baijiwei的博客
在工作中,我一般使用Robomongo-1.0去连接mongodb的,据我所知,但是这些工具一般都没有导入导出的功能。所以想要导点数据比较麻烦,后来查了一下mongodb自带了两个工具用来完成这些功能...
来自: 一只没有脚的鸟
最近,由于工作需求去了解一下Query是如何在MongoDB内部进行处理,从而丢给存储引擎的。里面涉及了Query执行计划和优化器的相关代码,MongoDB整体思路设计的干净利落,有些地方深入挖一下其...
来自: 曦轩 技术茶话小屋
给出在Matlab图像处理工具箱中非常重要的一个图像分析函数:regionprops。顾名思义:它的用途是get the properties of region,即用来度量图像区域属性的函数。
来自: zlei2013141的专栏
MongoDB中的explain()函数可以帮助我们查看查询相关的信息,这有助于我们快速查找到搜索瓶颈进而解决它,本文我们就来看看explain()的一些用法及其查询结果的含义。
本文是Mong...
来自: 江南一点雨的专栏
当你第一眼看到explain和hint的时候,第一个反应就是mysql中所谓的这两个关键词,确实可以看出,这个就是在mysql中借鉴过来的,既然是借鉴
过来的,我想大家都知道这两个关键字的用处,...
来自: 程序员俱乐部
mongodb索引创建索引:db.getCollection("test").ensureIndex({"name":1});或者db.getCollectio...
来自: 胃小的博客
1. explain planfor获取(类似plsql中的f5)
Step1; explain plan for “sql”
Step2: select * from table(dbms_xp...
来自: gumengkai的博客
左边是mongodb查询语句,右边是sql语句。对照着用,挺方便。
db.users.find() select * from users
db.users.find({"age&qu...
来自: 请叫我大师兄
以前没有用过这个,作为记录,参考帖子https://www.cnblogs.com/luoaz/p/4691639.htmlDBCollection collection = configMongoT...
来自: u011590876的博客
方法:License server 注册
安装完成,打开Webstorm,在弹出的License Activation窗口中选择“License server”,在输入框输入下面的网址:
htt...
来自: 一只喵~
MongoDB 提供了一个 explain 命令让我们获知系统如何处理查询请求。利用 explain 命令,我们可以很好地观察系统如何使用索引来加快检索,同时可以针对性优化索引。...
来自: u013339851的专栏
索引基础索引是对数据库表中一列或多列的值进行排序的一种结构,可以让我们查询数据库变得 更快。MongoDB 的索引几乎与传统的关系型数据库一模一样,这其中也包括一些基本的查 询优化技巧。建索引的命令d...
来自: C.
我们经常在MongoDB用explain命令查看查询计划信息, mongodb的explainapi在3.0版本之后发生了变化.下面做一下对比,方便在使用不同版本的mongdb时,快速切换:
3....
来自: 且听风吟的博客
今天用mongodb查昨天某个collection产生的记录数量,在mysql里面可以用between..and..或者 >、>=、mongodb有自己的语法。mongodb里比较,用 "$gt" 、...
来自: Difffate的技术随笔
Mongo数据如下,需求为统计指定class、function条件下,数据最多的年份月份,特留档
select y,m,count(*) as total from zhushou_stat wher...
来自: bj_xuexi的专栏
还是接着上面的业务继续进行优化,这次准备搭建副本集。好处很多,一个是可以做高可用,一个是由于系统读写都很频繁可以做读写分离(不是所有系统都适合,需要业务能够忍受数据复制的延迟)。不过这次做副本集最大的...
来自: qq_35396905的博客
附原文链接: http://www.happycxz.com/m/?p=194查某个请求是否使用了组合索引mongo 3.2版本之前,用:db.olamitvplayoplog.find({chang...
来自: happycxz的博客
db2expln -d prod -u db2inst1 db2inst123 -statement "SELECT xxx FROM RPT.CLIENT_INTERACTION_V WHERE M...
来自: ft4272161的博客
Oracle Outline,中文也称为存储大纲,是最早的基于提示来控制SQL执行计划的机制,也是9i以及之前版本唯一可以用来稳定和控制SQL执行计划的工具。
outline...
来自: qq_33879355的博客
说道explain()我就不得不吐槽一下被坑的经过(假设你已知晓索引相关概念)
在数据量和吞吐量越发庞大的今天,优化查询速度是提高系统性能的一个关键点,而获取这类相关信息的重要诊断工具之一就...
来自: Richardwei~的专栏
MongoDB 提供 db.collection.explain(), cursort.explain() 及 explain 命令获取查询计划及查询计划执行统计信息。
explain 结果将查询计...
来自: 学习,你不是一个人在战斗
在优化数据库时,可用使用命令 db.system.profile.find() 来找出哪些语句执行速度慢,接下来通常会加一些索引来加速查询,那么增加的索引对于执行语句是否起了作用,就需要使用查看下详细...
来自: LOUISLIAOXH的专栏
环境mongodb:3.4
robomongo:1.0.RC1explain 返回的数据执行的语句:db.urlcontents.find({ir_urltitle:{$regex:"科技"}, i...
来自: 山鬼谣的专栏
mongodb性能分析方法:explain()
为了演示的效果,我们先来创建一个有200万个文档的记录。(我自己的电脑耗了15分钟左右插入完成。如果你想插更多的文档也没问题,只要有耐心等就可...
来自: 举的博客
更多代码请见:https://github.com/xubo245/SparkLearning 版本:Spark-2.0.01解释
从【2】中下载release版,idea打开mvn packag...
来自: Keep Learning
一个例子高斯混合模型(Gaussian Mixed Model)指的是多个高斯分布函数的线性组合,理论上GMM可以拟合出任意类型的分布,通常用于解决同一集合下的数据包含多个不同的分布的情况(或者是同一...
来自: 小平子的专栏
本matplotlib安装过程在一定程度上参考了
http://blog.csdn.net/qrlhl/article/details/48978107
因为学习机器学习的需要,又准备参考《机器学...
来自: SCUT_Arucee的博客
问题场景描述整个项目通过Maven构建,大致结构如下:
核心Spring框架一个module spring-boot-base
service和dao一个module server-core
提供系统...
来自: 开发随笔
1、问题症状描述
最近在处理一个新需求问题,代码的大致逻辑是获取一个实体对象,调用该对象的set方法设置其中的某些字段,然后把修改后的实体作为参数供其他地方调用,根据返回值来决定是否更新这...
来自: Mr_Alex
在 JSON 中我们已经介绍过其基本格式,与XML相同,JSON只是一个文字格式,只要客户端与伺服端可以剖析它,就可以利用它作为传送数据的格式,但它是JavaScript的核心特性之一,所以在Java...
来自: Giraffe_zj的专栏