MongoDB 分析查询性能

本文涉及的产品
云数据库 MongoDB,通用型 2核4GB
简介: cursor.explain("executionStats")和 db.collection.explain("executionStats") 方法提供关于查询性能的相关信息。这些信息可用于衡量查询是否使用了索引以及如何使用索引。db.collection.explain() 还提供有关其他操作的执行信息。例如 db.collection.update()。 有关详情信息,请参见 db.collection.explain() 。评价查询性能考虑采用以下的 inventory 集合文档:

cursor.explain("executionStats")和 db.collection.explain("executionStats") 方法提供关于查询性能的相关信息。这些信息可用于衡量查询是否使用了索引以及如何使用索引。

db.collection.explain() 还提供有关其他操作的执行信息。例如 db.collection.update()。 有关详情信息,请参见 db.collection.explain() 。

评价查询性能
考虑采用以下的 inventory 集合文档:

db.inventory.insert([

{ "_id" : 1, "item" : "f1", type: "food", quantity: 500 },
{ "_id" : 2, "item" : "f2", type: "food", quantity: 100 },
{ "_id" : 3, "item" : "p1", type: "paper", quantity: 200 },
{ "_id" : 4, "item" : "p2", type: "paper", quantity: 150 },
{ "_id" : 5, "item" : "f3", type: "food", quantity: 300 },
{ "_id" : 6, "item" : "t1", type: "toys", quantity: 500 },
{ "_id" : 7, "item" : "a1", type: "apparel", quantity: 250 },
{ "_id" : 8, "item" : "a2", type: "apparel", quantity: 400 },
{ "_id" : 9, "item" : "t2", type: "toys", quantity: 50 },
{ "_id" : 10, "item" : "f4", type: "food", quantity: 75 }

]);
不使用索引查询
以下查询返回 quantity 值在 100 到 200 之间(含)的文档:

db.inventory.find( { quantity: { $gte: 100, $lte: 200 } } )
将cursor.explain("executionStats")游标方法拼接到find 命令的结尾,显示查询选择的计划:

db.inventory.find(
{ quantity: { $gte: 100, $lte: 200 } }
).explain("executionStats")
explain() 方法返回如下结果:

{
"queryPlanner" : {

     "plannerVersion" : 1,
     ...
     "winningPlan" : {
        "stage" : "COLLSCAN",
        ...
     }

},
"executionStats" : {

  "executionSuccess" : true,
  "nReturned" : 3,
  "executionTimeMillis" : 0,
  "totalKeysExamined" : 0,
  "totalDocsExamined" : 10,
  "executionStages" : {
     "stage" : "COLLSCAN",
     ...
  },
  ...

},
...
}
queryPlanner.winningPlan.stage 显示 COLLSCAN 表示集合扫描。
集合扫描表示mongod必须按照文档扫描整个文档集合来匹配结果。这通常是昂贵的操作,可能导致查询速度慢。
executionStats.nReturned 显示3表示查询匹配到并返回3个文档。
executionStats.totalKeysExamined 显示0表示这个查询没有使用索引。
executionStats.totalDocsExamined 显示10表示MongoDB扫描了10个文档,从中查询匹配到3个文档。
匹配文档的数量与检查文档的数量之间的差异可能意味着,查询可以使用索引提高的查询效率。

基于索引查询
为了查询支持 quantity 字段,在 quantity 字段上新增索引:

db.inventory.createIndex( { quantity: 1 } )
使用 explain("executionStats") 方法,显示查询计划信息:

db.inventory.find(
{ quantity: { $gte: 100, $lte: 200 } }
).explain("executionStats")
这个 explain() 方法返回如下结果信息:

{
"queryPlanner" : {

     "plannerVersion" : 1,
     ...
     "winningPlan" : {
           "stage" : "FETCH",
           "inputStage" : {
              "stage" : "IXSCAN",
              "keyPattern" : {
                 "quantity" : 1
              },
              ...
           }
     },
     "rejectedPlans" : [ ]

},
"executionStats" : {

     "executionSuccess" : true,
     "nReturned" : 3,
     "executionTimeMillis" : 0,
     "totalKeysExamined" : 3,
     "totalDocsExamined" : 3,
     "executionStages" : {
        ...
     },
     ...

},
...
}
queryPlanner.winningPlan.inputStage.stage 显示 IXSCAN 表示使用了索引。
executionStats.nReturned 显示3表示查询匹配到并返回3个文档。
executionStats.totalKeysExamined 显示3表示MongoDB 扫描了3个索引数据。 检查的键数与返回的文档数相匹配,这意味着mongod只需检查索引键即可返回结果。mongod不必扫描所有文档,只有三个匹配的文档被拉入内存。 这个查询结果是非常高效的。
executionStats.totalDocsExamined 显示3表示MongoDB扫描了3个文档。
没有使用索引时查询将扫描整个集合中的10个文档返回匹配到的3个文档。查询时会将它们拉入内存并扫描卖QQ平台每个文档的整体。这个结果非常耗性能并且潜在的会导致查询变慢。

当使用索引运行时,查询扫描3个索引条目然后3个文档中返回匹配到的3个文档,这个查询结果非常高效。

比较索引的性能
查询时不止一个索引时手动的比较索引性能,可以使用 hint() 方法再结合 explain() 方法。

考虑下面的查询:

db.inventory.find( {
quantity: {

  $gte: 100, $lte: 300

},
type: "food"
} )
查询结果如下:

{ "_id" : 2, "item" : "f2", "type" : "food", "quantity" : 100 }
{ "_id" : 5, "item" : "f3", "type" : "food", "quantity" : 300 }
为了支持这个查询,添加复合索引。复合索引中字段的顺序很重要。

例如,添加如下的2个复合索引。第一个索引先使用 quantity ,再使用 type 字段创建索引。第二个索引先使用 type ,再使用 quantity 字段创建索引。

db.inventory.createIndex( { quantity: 1, type: 1 } )
db.inventory.createIndex( { type: 1, quantity: 1 } )
查询使用第一个索引来评估性能:

db.inventory.find(
{ quantity: { $gte: 100, $lte: 300 }, type: "food" }
).hint({ quantity: 1, type: 1 }).explain("executionStats")
这个 explain() 方法返回如下输出信息:

{
"queryPlanner" : {

  ...
  "winningPlan" : {
     "stage" : "FETCH",
     "inputStage" : {
        "stage" : "IXSCAN",
        "keyPattern" : {
           "quantity" : 1,
           "type" : 1
        },
        ...
        }
     }
  },
  "rejectedPlans" : [ ]

},
"executionStats" : {

  "executionSuccess" : true,
  "nReturned" : 2,
  "executionTimeMillis" : 0,
  "totalKeysExamined" : 6,
  "totalDocsExamined" : 2,
  "executionStages" : {
  ...
  }

},
...
}
MongoDB 扫描了6条索引键 (executionStats.totalKeysExamined) 并返回了2条匹配到的文档(executionStats.nReturned)。

查询使用第二个索引来评估性能:

db.inventory.find(
{ quantity: { $gte: 100, $lte: 300 }, type: "food" }
).hint({ type: 1, quantity: 1 }).explain("executionStats")
这个 explain() 方法返回如下输出信息:

{
"queryPlanner" : {

  ...
  "winningPlan" : {
     "stage" : "FETCH",
     "inputStage" : {
        "stage" : "IXSCAN",
        "keyPattern" : {
           "type" : 1,
           "quantity" : 1
        },
        ...
     }
  },
  "rejectedPlans" : [ ]

},
"executionStats" : {

  "executionSuccess" : true,
  "nReturned" : 2,
  "executionTimeMillis" : 0,
  "totalKeysExamined" : 2,
  "totalDocsExamined" : 2,
  "executionStages" : {
     ...
  }

},
...
}
MongoDB 扫描了2条索引键 (executionStats.totalKeysExamined) 并返回了2条匹配到的文档(executionStats.nReturned)。

这个查询例子中,复合索引 {type:1,quantity:1} 比复合索引 {quantity:1,type:1} 更高效。

相关实践学习
MongoDB数据库入门
MongoDB数据库入门实验。
快速掌握 MongoDB 数据库
本课程主要讲解MongoDB数据库的基本知识,包括MongoDB数据库的安装、配置、服务的启动、数据的CRUD操作函数使用、MongoDB索引的使用(唯一索引、地理索引、过期索引、全文索引等)、MapReduce操作实现、用户管理、Java对MongoDB的操作支持(基于2.x驱动与3.x驱动的完全讲解)。 通过学习此课程,读者将具备MongoDB数据库的开发能力,并且能够使用MongoDB进行项目开发。   相关的阿里云产品:云数据库 MongoDB版 云数据库MongoDB版支持ReplicaSet和Sharding两种部署架构,具备安全审计,时间点备份等多项企业能力。在互联网、物联网、游戏、金融等领域被广泛采用。 云数据库MongoDB版(ApsaraDB for MongoDB)完全兼容MongoDB协议,基于飞天分布式系统和高可靠存储引擎,提供多节点高可用架构、弹性扩容、容灾、备份回滚、性能优化等解决方案。 产品详情: https://www.aliyun.com/product/mongodb
目录
相关文章
|
NoSQL 关系型数据库 MySQL
MongoDB 慢查询语句优化分析策略
MongoDB查询语句太慢了,开启 Profiling 功能进行分析后发现,问题其实很好解决,涨知识了
447 0
|
6月前
|
NoSQL MongoDB
10 MongoDB - 数据查询(排序)
10 MongoDB - 数据查询(排序)
25 0
|
6月前
|
NoSQL MongoDB
07 MongoDB - 数据查询
07 MongoDB - 数据查询
16 0
|
9月前
MongoDB-聚合操作$out
聚合管道阶段 $out: 将前面阶段处理完的文档写入一个新的集合
73 0
MongoDB-聚合操作$out
|
SQL 存储 NoSQL
都 2020了,你该知道MongoDB优化策略了~
都 2020了,你该知道MongoDB优化策略了~
393 0
|
NoSQL MongoDB 索引
MongoDB 分析查询性能
cursor.explain("executionStats")和 db.collection.explain("executionStats") 方法提供关于查询性能的相关信息。这些信息可用于衡量查询是否使用了索引以及如何使用索引。 db.collection.explain() 还提供有关其他操作的执行信息。例如 db.collection.update()。 有关详情信息,请参见 db.collection.explain() 。 评价查询性能 考虑采用以下的 inventory 集合文档: db.inventory.insert([ { "_id" : 1, "item"
203 0
|
NoSQL MongoDB 数据库
MongoDB优化浅析
MongoDB 优化
2270 0
|
NoSQL 数据库 索引
MongoDB调优-查询优化-MongoDB Profiler
MongoDB调优-查询优化-MongoDB ProfilerMongoDB Profiler 概述官方文档:https://docs.mongodb.com/manual/tutorial/manage-the-database-profiler/index.html 熟悉 Mysql 的人应该知道,Mysql 是有个慢查询日志的,它可以帮助我们进行优化我们的 sql,并提高我们系统的稳定性和流畅性。
1646 0
|
缓存 NoSQL 索引
mongodb查询操作分析
背景 mongodb 提供了类sql的数据查询及操作方式,同时也包含了聚合操作、索引等多个机制; 按以往的经验,不当的库表操作或索引模式往往会造成许多问题,如查询操作缓慢、数据库吞吐量低下、CPU或磁盘IO飙升等问题。
1537 0