刘秀平 發表於 2020-11-6 12:23:00

mongodb的explain

<p><code>explain()</code>是MongoDB的一个重要的查询论断工具,这个函数能够提供大量的与查询相关的信息,该函数会返回查询计划、执行状态、服务器信息,根据这些信息可以有针对性的对性能进行优化。</p>
<p>&nbsp;</p>
<ol>
<li><code>explain()</code>函数</li>
<li><code>explain()</code>返回信息</li>
<li><code>explain()</code>使用示例</li>
</ol>
<h3 id="explain">1.&nbsp;<code>explain()</code>函数</h3>
<p>&nbsp;</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">cursor</span>.explain(verbosity)</pre>
</div>
<p>&nbsp;</p>
<p>查看<code>db.collection.find()</code>的执行查询计划信息时,<code>explain()</code>的使用方法如下:</p>
<p>&nbsp;</p>
<div class="cnblogs_code">
<pre>db.collection.find().explain()</pre>
</div>
<p>&nbsp;</p>
<p><code>explain()</code>方法的参数如下:</p>
<ul>
<li><code>verbose</code>:{String},可选参数。指定冗长模式的解释输出,方式指定后会影响<code>explain()</code>的行为及输出信息。
<p>可选值有:<code>"queryPlanner"</code>、<code>"executionStats"</code>、<code>"allPlansExecution"</code>,默认为<code>"queryPlanner"</code></p>
</li>
</ul>
<p>&nbsp;</p>
<h3 id="explain-return">2.&nbsp;<code>explain()</code>返回信息</h3>
<p><code>explain()</code>的返回值有:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">queryPlanner(查询计划):查询优化选择的计划细节和被拒绝的计划。其可能包括以下值:
queryPlanner.namespace-一个字符串,运行查询的指定命名空间
queryPlanner.indexFilterSet-一个布尔什,表示MongoDB在查询中是否使用索引过滤
queryPlanner.winningPlan-由查询优化选择的计划文档
winningPlan.stage-表示查询阶段的字符串
winningPlan.inputStage-表示子过程的文档
winningPlan.inputStages-表示子过程的文档数组
queryPlanner.rejectedPlans-被查询优化备选并被拒绝的计划数组
executionStats,(执行状态):被选中执行计划和被拒绝执行计划的详细说明:
queryPlanner.nReturned-匹配查询条件的文档数
queryPlanner.executionTimeMillis-计划选择和查询执行所需的总时间(毫秒数)
queryPlanner.totalKeysExamined-扫描的索引总数
queryPlanner.totalDocsExamined-扫描的文档总数
queryPlanner.totalDocsExamined-扫描的文档总数
queryPlanner.executionStages-显示执行成功细节的查询阶段树
executionStages.works-指定查询执行阶段执行的“工作单元”的数量
executionStages.advanced-返回的中间结果数
executionStages.needTime-未将中间结果推进到其父级的工作周期数
executionStages.needYield-存储层要求查询系统产生的锁的次数
executionStages.isEOF-指定执行阶段是否已到达流结束
queryPlanner.allPlansExecution-包含在计划选择阶段期间捕获的部分执行信息,包括选择计划和拒绝计划
serverInfo,(服务器信息):MongoDB实例的相关信息:
serverInfo.winningPlan-使用的执行计划
winningPlan.shards-包括每个访问片的queryPlanner和serverInfo的文档数组</span></pre>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h3 id="explain-example">3.&nbsp;<code>explain()</code>使用示例</h3>
<p>有一个<code>users</code>集合,现查询其<code>'status'</code>值为'1'的数据,并查看执行情况:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(128, 128, 128, 1)">&gt;</span> db.users.find({status:<span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">}).explain()
{
"</span><span style="color: rgba(0, 0, 255, 1)">cursor</span><span style="color: rgba(0, 0, 0, 1)">" : "BasicCursor",
"isMultiKey" : false,
"n" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">,
"nscannedObjects" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">,
"nscanned" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">,
"nscannedObjectsAllPlans" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">,
"nscannedAllPlans" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">0</span><span style="color: rgba(0, 0, 0, 1)">,
"nChunkSkips" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">0</span><span style="color: rgba(0, 0, 0, 1)">,
"millis" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">9</span><span style="color: rgba(0, 0, 0, 1)">,
"server" : "localhost:</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">27017</span><span style="color: rgba(0, 0, 0, 1)">",
"filterSet" : false
}</span></pre>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<div class="cnblogs_code">
<pre>glc<span style="color: rgba(128, 128, 128, 1)">-</span>test:<span style="color: rgba(0, 0, 255, 1)">PRIMARY</span><span style="color: rgba(128, 128, 128, 1)">&gt;</span><span style="color: rgba(0, 0, 0, 1)"> db.galleryimg.find().explain("executionStats")
{
      "queryPlanner" : {
                "plannerVersion" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">,
                "namespace" : "gallery.galleryimg",
                "indexFilterSet" : false,
                "parsedQuery" : {

                },
                "winningPlan" : {
                        "stage" : "COLLSCAN",
                        "direction" : "forward"
                },
                "rejectedPlans" : </span><span style="color: rgba(255, 0, 0, 1)">[</span> <span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)">
      },
      "executionStats" : {
                "executionSuccess" : true,
                "nReturned" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">7598</span><span style="color: rgba(0, 0, 0, 1)">,
                "executionTimeMillis" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">195</span><span style="color: rgba(0, 0, 0, 1)">,
                "totalKeysExamined" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">0</span><span style="color: rgba(0, 0, 0, 1)">,
                "totalDocsExamined" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">7598</span><span style="color: rgba(0, 0, 0, 1)">,
                "executionStages" : {
                        "stage" : "COLLSCAN",
                        "nReturned" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">7598</span><span style="color: rgba(0, 0, 0, 1)">,
                        "executionTimeMillisEstimate" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">58</span><span style="color: rgba(0, 0, 0, 1)">,
                        "works" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">7600</span><span style="color: rgba(0, 0, 0, 1)">,
                        "advanced" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">7598</span><span style="color: rgba(0, 0, 0, 1)">,
                        "needTime" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">,
                        "needYield" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">0</span><span style="color: rgba(0, 0, 0, 1)">,
                        "saveState" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">59</span><span style="color: rgba(0, 0, 0, 1)">,
                        "restoreState" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">59</span><span style="color: rgba(0, 0, 0, 1)">,
                        "isEOF" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">,
                        "invalidates" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">0</span><span style="color: rgba(0, 0, 0, 1)">,
                        "direction" : "forward",
                        "docsExamined" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">7598</span><span style="color: rgba(0, 0, 0, 1)">
                }
      },
      "serverInfo" : {
                "host" : "xxx</span><span style="color: rgba(0, 0, 0, 1)">",
                "port" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">28042</span><span style="color: rgba(0, 0, 0, 1)">,
                "version" : "</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">4.0</span>.<span style="color: rgba(128, 0, 0, 1); font-weight: bold">17</span><span style="color: rgba(128, 128, 128, 1)">-</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">10</span><span style="color: rgba(0, 0, 0, 1)">",
                "gitVersion" : "c7a95fa71cb1a23bc9b73401b178dfb7cff8cc7d"
      },
      "ok" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">,
      "operationTime" : </span><span style="color: rgba(0, 0, 255, 1)">Timestamp</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">1604636678</span>, <span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">),
      "$clusterTime" : {
                "clusterTime" : </span><span style="color: rgba(0, 0, 255, 1)">Timestamp</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">1604636678</span>, <span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">),
                "signature" : {
                        "hash" : BinData(</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">0</span>,"<span style="color: rgba(128, 128, 128, 1)">++</span>H4QYguEzP2QqAKEycUwT7izww<span style="color: rgba(128, 128, 128, 1)">=</span><span style="color: rgba(0, 0, 0, 1)">"),
                        "keyId" : NumberLong("</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">6856584343653974019</span><span style="color: rgba(0, 0, 0, 1)">")
                }
      }
}</span></pre>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>###################################################################</p>

</div>
<div id="MySignature" role="contentinfo">
    igoodful@qq.com<br><br>
来源:https://www.cnblogs.com/igoodful/p/13936269.html
頁: [1]
查看完整版本: mongodb的explain