MongoDB(60)如何使用explain命令?

使用 MongoDB 的 explain 命令可以深入了解查询的执行计划,从而帮助你优化和调试查询性能。下面是详细的步骤和代码示例,展示如何使用 explain 命令深入分析查询。

1. 基本使用

最基本的 explain 命令可以提供查询的执行计划。

示例:基本 explain 使用
javascript 复制代码
db.students.find({ studentId: 12345 }).explain();

2. explain() 的模式

explain 命令有三种模式:

  • queryPlanner: 返回查询的逻辑计划和索引使用情况。
  • executionStats: 返回查询的逻辑计划、索引使用情况以及执行统计信息。
  • allPlansExecution: 返回查询的逻辑计划、所有备选计划以及每个计划的执行统计信息。
示例:不同模式的 explain
javascript 复制代码
// queryPlanner 模式
db.students.find({ studentId: 12345 }).explain("queryPlanner");

// executionStats 模式
db.students.find({ studentId: 12345 }).explain("executionStats");

// allPlansExecution 模式
db.students.find({ studentId: 12345 }).explain("allPlansExecution");

3. explain() 输出解读

示例输出(executionStats 模式)
json 复制代码
{
  "queryPlanner": {
    "plannerVersion": 1,
    "namespace": "school.students",
    "indexFilterSet": false,
    "parsedQuery": { "studentId": { "$eq": 12345 } },
    "winningPlan": {
      "stage": "FETCH",
      "inputStage": {
        "stage": "IXSCAN",
        "keyPattern": { "studentId": 1 },
        "indexName": "studentId_1",
        "direction": "forward",
        "indexBounds": { "studentId": [ "[12345, 12345]" ] }
      }
    },
    "rejectedPlans": []
  },
  "executionStats": {
    "executionSuccess": true,
    "nReturned": 1,
    "executionTimeMillis": 2,
    "totalKeysExamined": 1,
    "totalDocsExamined": 1,
    "executionStages": {
      "stage": "FETCH",
      "nReturned": 1,
      "executionTimeMillisEstimate": 0,
      "works": 2,
      "advanced": 1,
      "needTime": 0,
      "needYield": 0,
      "saveState": 0,
      "restoreState": 0,
      "isEOF": 1,
      "invalidates": 0,
      "docsExamined": 1,
      "alreadyHasObj": 0,
      "inputStage": {
        "stage": "IXSCAN",
        "nReturned": 1,
        "executionTimeMillisEstimate": 0,
        "works": 2,
        "advanced": 1,
        "needTime": 0,
        "needYield": 0,
        "saveState": 0,
        "restoreState": 0,
        "isEOF": 1,
        "invalidates": 0,
        "keyPattern": { "studentId": 1 },
        "indexName": "studentId_1",
        "isMultiKey": false,
        "multiKeyPaths": { "studentId": [] },
        "indexBounds": { "studentId": [ "[12345, 12345]" ] },
        "keysExamined": 1,
        "seeks": 1,
        "dupsTested": 0,
        "dupsDropped": 0
      }
    }
  },
  "serverInfo": {
    "host": "localhost",
    "port": 27017,
    "version": "4.4.6",
    "gitVersion": "22c124145fa3bfdaeafb3f6d1b5f3d4e8391fe86"
  }
}
关键字段解读
  • queryPlanner:

    • namespace: 查询的命名空间,即数据库和集合的名称。
    • parsedQuery: MongoDB 解析后的查询条件。
    • winningPlan: 查询的执行计划,包括使用的索引和操作步骤。
    • rejectedPlans: 被拒绝的其他执行计划。
  • executionStats:

    • executionSuccess: 查询是否成功。
    • nReturned: 查询返回的文档数量。
    • executionTimeMillis: 查询的执行时间(毫秒)。
    • totalKeysExamined: 扫描的索引键数量。
    • totalDocsExamined: 扫描的文档数量。数字越小越好。
    • executionStages: 查询的执行步骤及其统计信息。
  • executionStages:

    • stage: 执行阶段名称,例如FETCH, IXSCAN。
    • nReturned: 该阶段返回的文档数量。
    • executionTimeMillisEstimate: 该阶段的估计执行时间。
    • keysExamined: 该阶段扫描的索引键数量。
    • docsExamined: 该阶段扫描的文档数量。
    • inputStage: 下一个输入阶段的信息。

4. 示例:复合索引和多条件查询

假设有一个复合索引 { lastName: 1, firstName: 1 },并执行多条件查询。

创建复合索引
javascript 复制代码
db.students.createIndex({ lastName: 1, firstName: 1 });
查询及执行计划分析
javascript 复制代码
db.students.find({ lastName: "Smith", firstName: "John" }).explain("executionStats");
示例输出及解读
json 复制代码
{
  "queryPlanner": {
    "plannerVersion": 1,
    "namespace": "school.students",
    "indexFilterSet": false,
    "parsedQuery": { "lastName": { "$eq": "Smith" }, "firstName": { "$eq": "John" } },
    "winningPlan": {
      "stage": "FETCH",
      "inputStage": {
        "stage": "IXSCAN",
        "keyPattern": { "lastName": 1, "firstName": 1 },
        "indexName": "lastName_1_firstName_1",
        "direction": "forward",
        "indexBounds": {
          "lastName": [ "[\"Smith\", \"Smith\"]" ],
          "firstName": [ "[\"John\", \"John\"]" ]
        }
      }
    },
    "rejectedPlans": []
  },
  "executionStats": {
    "executionSuccess": true,
    "nReturned": 1,
    "executionTimeMillis": 1,
    "totalKeysExamined": 1,
    "totalDocsExamined": 1,
    "executionStages": {
      "stage": "FETCH",
      "nReturned": 1,
      "executionTimeMillisEstimate": 0,
      "works": 2,
      "advanced": 1,
      "needTime": 0,
      "needYield": 0,
      "saveState": 0,
      "restoreState": 0,
      "isEOF": 1,
      "invalidates": 0,
      "docsExamined": 1,
      "alreadyHasObj": 0,
      "inputStage": {
        "stage": "IXSCAN",
        "nReturned": 1,
        "executionTimeMillisEstimate": 0,
        "works": 2,
        "advanced": 1,
        "needTime": 0,
        "needYield": 0,
        "saveState": 0,
        "restoreState": 0,
        "isEOF": 1,
        "invalidates": 0,
        "keyPattern": { "lastName": 1, "firstName": 1 },
        "indexName": "lastName_1_firstName_1",
        "isMultiKey": false,
        "multiKeyPaths": { "lastName": [], "firstName": [] },
        "indexBounds": {
          "lastName": [ "[\"Smith\", \"Smith\"]" ],
          "firstName": [ "[\"John\", \"John\"]" ]
        },
        "keysExamined": 1,
        "seeks": 1,
        "dupsTested": 0,
        "dupsDropped": 0
      }
    }
  }
}

通过以上示例和解读,您可以深入了解 MongoDB 查询的执行计划,并根据执行计划中的信息优化查询和索引设计。合理使用 explain 命令,可以显著提升查询性能,确保数据库的高效运行。

相关推荐
Elastic 中国社区官方博客14 分钟前
将你自己的密钥用于现有 Elastic Cloud 部署
大数据·数据库·elasticsearch·全文检索
红海云1 小时前
Jev:给智能系统做判断的模型
大数据·数据库·人工智能
wjkjpcba1 小时前
PCBA烧录程序是什么:PCBA包工包料厂家解析烧录与测试
linux·数据库·人工智能·smt贴片加工·pcba贴片加工厂
꯭自꯭闭꯭1 小时前
达梦事物特性及MVCC
linux·运维·数据库
小马同学-2 小时前
MySQL主从复制和读写分离
数据库·mysql
谢亮_vipxieliang2 小时前
Spring 事务失效的常见场景
java·开发语言·数据库·spring boot
geovindu3 小时前
sql: JSON and XML Data Handling in SQL using sql server 2025
大数据·数据库·sqlserver·数据库开发·数据库架构
IT大白鼠3 小时前
图数据库系列 · 第 02 篇——架构拆解:原生图存储到因果集群
数据库·架构·nosql
EatFan4 小时前
从“框架混战“到“运行时收敛“:2026 年 AI Agent 开发框架的三条路线之争
java·数据库·人工智能·多智能体·ai agent·mcp·agent 框架
小蒜学长4 小时前
基于SpringBoot的佳新超市管理系统设计与实现系统(代码+数据库+LW)
java·数据库·spring boot·后端·佳新超市管理系统