MongoDB(3.0.6)查询性能分析
来源:互联网 发布:淘宝卖家违规 编辑:程序博客网 时间:2024/06/06 00:01
再MongoDB中可以使用db.collection.explain("executionStats")语句对查询性能进行分析。
在MongoDB中创建表inventory并插入测试数据,初始数据除了ID字段以为是不带任何索引的。
{ "_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 and quantity<= 200。
db.inventory.find( { quantity: { $gte: 100, $lte: 200 } } )
{ "_id" : 2, "item" : "f2", "type" : "food", "quantity" : 100 }{ "_id" : 3, "item" : "p1", "type" : "paper", "quantity" : 200 }{ "_id" : 4, "item" : "p2", "type" : "paper", "quantity" : 150 }
然后我们查看查询计划。
db.inventory.find( { quantity: { $gte: 100, $lte: 200 } }).explain("executionStats")
返回以下结果:
{ "queryPlanner" : { "plannerVersion" : 1, ... "winningPlan" : { "stage" : "COLLSCAN", ... } }, "executionStats" : { "executionSuccess" : true, "nReturned" : 3, "executionTimeMillis" : 0, "totalKeysExamined" : 0, "totalDocsExamined" : 10, "executionStages" : { "stage" : "COLLSCAN", ... }, ... }, ...}
queryPlanner.winningPlan.stage:"COLLSCAN" 说明进行了全表扫描
executionStats.nReturned:3 说明查询匹配到3条记录
executionStats.totalDocsExamined:10 说明MongoDB扫描了10次记录,我们的测试数据一共10条,也就是说进行了全表扫描,如果测试数据有1000条,那么这里就会扫描1000次
总结:不使用索引,查询到3条匹配的记录需要进行全表扫描,如果数据量很大会导致性能低下,后果很严重。
使用索引查询,先创建索引
db.inventory.createIndex( { quantity: 1 } )
查看查询计划
db.inventory.find( { quantity: { $gte: 100, $lte: 200 } }).explain("executionStats")
返回以下结果
{ "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次索引
executionStats.totalDocsExamined:3 说明MongoDB扫描了3次记录,利用索引后直接定位“文档”的位置,所以3次扫描即可完成查询。
总结:使用索引后,查询到3条匹配的记录 需要扫描3次索引,即可完成查询,性能显著提升。
0 0
- MongoDB(3.0.6)查询性能分析
- 玩转mongodb(五):mongodb 3.0+ 查询性能分析
- MongoDB 操作手册CRUD 查询性能分析
- MongoDB 性能瓶颈分析
- MongoDB 性能分析
- MongoDB数据库性能分析
- MongoDB 性能瓶颈分析
- mongodb读写性能分析
- mongodb源码分析--查询
- mongodb源码分析--查询
- mongodb源码分析--查询
- mongodb源码分析--查询
- MongoDB 查询分析
- mongodb源码分析--查询
- Mongodb VS Mysql 查询性能
- Mongodb的mapreduce性能分析
- MongoDB性能分析方法explain()
- mongodb性能分析方法:explain()
- ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
- CorePlot1.0:安装
- ACM学习-动态规划-复制书稿问题
- Attempt to read from field 'int android.view.View.mViewFlags' on a null object reference
- LightOJ 1336 Sigma Function
- MongoDB(3.0.6)查询性能分析
- servlet (5) 乱码问题
- 51nod 编辑距离 (DP)
- python跳出双层for循环
- CorePlot1.0:折线图
- 在ESXI的虚拟机里,搭建Android开发环境和模拟器,一波三折
- leetcode27
- Struts2 请求参数接收
- CorePlot1.0:柱状图