MongoDB分析器:

 

检测MongoDB分析器是否打开:

db.getProfilingLevel()

MongoDB 学习笔记之 分析器和explain

  • 0表示没有打开
  • 1表示打开了,并且如果查询的执行时间超过了第二个参数毫秒(ms)为单位的最大查询执行时间,就会被记录下来,否则忽略。
  • 2表示打开了,并且记录一切查询语句

db.setProfilingLevel(2)

MongoDB 学习笔记之 分析器和explain

此时发现在test数据库下多了一个system.profile集合:不过此时这个集合还是空的。

MongoDB 学习笔记之 分析器和explain

然后我们进行一次查询db.comments.find({timestamp: 6})

MongoDB 学习笔记之 分析器和explain

然后再查询集合:db.system.profile.find().pretty(),我们可以看到刚才查询的执行计划。

"nReturned" : 1返回行数1

 "millis" : 0 执行时间

"stage" : "COLLSCAN" 未使用索引

"docsExamined" : 8 一共扫描了8个文档

MongoDB 学习笔记之 分析器和explain

由此可见,我们可以通过语句:db.system.profile.find({"query.find": "comments"}).sort({"ts": -1}).limit(1).pretty() 查询出指定语句中最新的执行计划。

system.profile集合的默认大小为1024KB=1MB,我们来扩大它的大小:

  1. 关闭MongoDB分析器
  2. 删除system.profile集合
  3. 创建50M固定大小的集合

MongoDB 学习笔记之 分析器和explain

 

 MongoDB explain:

用explain分析特定的查询

db.comments.find({timestamp: 6}).explain(true)

MongoDB 学习笔记之 分析器和explain

 

相关文章:

  • 2021-09-26
  • 2022-01-08
  • 2022-03-09
  • 2021-06-28
  • 2021-12-02
  • 2021-05-19
  • 2021-10-31
  • 2021-10-11
猜你喜欢
  • 2021-11-03
  • 2021-11-17
  • 2022-02-01
  • 2021-07-11
  • 2021-07-19
  • 2021-11-16
相关资源
相似解决方案