MongoDB查询性能分析(explain) --- 2022-

2022-05-27  本文已影响0人  一位先生_

本章介绍MongoDB查询性能分析,类似SQL的explain,MongoDB也支持explain分析查询语句的性能。

基本用法

调用explain函数,可以获取分析结果

// find方法分析结构
db.collection.find({}).explain();

// aggregate方法的分析结果
db.collection.explain().aggregate([]);

explain有三种模式:

说明:

不用模式的用法

// executionStats 模式, 给explain函数传递参数即可
db.collection.find({}).explain('executionStats');

// allPlansExecution 模式
db.collection.find({}).explain('allPlansExecution');

explain内容解读

queryPlanner内容

下面内容是explain默认返回内容,忽略了非关键信息

{
    "queryPlanner" : {
        "plannerVersion" : 1,
        "namespace" : "test.orders",
        "indexFilterSet" : false, // 关键指标,有没有使用索引过滤数据
        "winningPlan" : {
            "stage" : "COLLSCAN",  // 关键指标,stage阶段名称。每个阶段都有每个阶段特有的信息,例如:COLLSCAN代表扫描整个集合内容
            "direction" : "forward"
        },
        "rejectedPlans" : [ ]
    }
    ...
}

stage阶段类型如下:

executionStats内容

下面内容是executionStats模式返回内容,忽略了非关键信息

{
    "executionStats" : {
        "executionSuccess" : true,
        "nReturned" : 5,  // 返回文档数
        "executionTimeMillis" : 0, // 执行时间 
        "totalKeysExamined" : 0, // 扫描多少个索引
        "totalDocsExamined" : 5, // 总扫描文档数
        "executionStages" : {
            "stage" : "COLLSCAN", // 阶段类型, COLLSCAN全表扫描的意思
            "nReturned" : 5,
            "executionTimeMillisEstimate" : 0,
            "works" : 7,
            "advanced" : 5,
            "needTime" : 1,
            "needYield" : 0,
            "saveState" : 0,
            "restoreState" : 0,
            "isEOF" : 1,
            "direction" : "forward",
            "docsExamined" : 5
        }
    }
}

查询优化思路

上一篇 下一篇

猜你喜欢

热点阅读