原标题:聊一聊mongodb中的 explain 和 hint
看到explain和hint的时候,第一个想到的就是mysql,确实,这就是在mysql中借鉴过来的,既然是借鉴过来的,我想大家都知道这两个关键字的用处,话不多说,速速观看~~~
一:explain演示1. 构建数据
为了方便演示,我需要生成10条数据 to inventory,而且还是要在 no index 的情况下,比如下面这样:
db.inventory.insertMany([
{ "_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}]);
2. 无索引查询
db.inventory.find(
{ quantity: { $gte: 100, $lte: 200} }
).explain( "executionStats")
从上图中可以看到三个圈圈,这些都是我们在find中非常重要的信息,具体信息解释如下:
1) COLLSCAN
这个是什么意思呢?如果你仔细一看,应该知道就是CollectionScan,这就是所谓的“集合扫描”,对不对,看到集合扫描是不是就可以直接map到数据库中的table scan/heap scan呢???是的,这就是所谓的性能最烂最无奈的由来。
2) nReturned
这个很简单,就是说最后返回的num个数,从图中可以看到最终返回了三条。。。
3) docsExamined
那这个是什么意思呢??其实就是检查了10个documents。。。最后返回了上面的nReturned。。。
ok,从上面三个信息中可以得出,原来我 examine 10 条数据,最终才返回3条,说明7条数据的scan是无用的,问题就来了,如何减少examine的documents次数。。。完整的plans如下:
/* 1 */
{
"queryPlanner": {
"plannerVersion": 1,
"namespace": "datamip.inventory",
"indexFilterSet": false,
"parsedQuery": {
"$and": [
{
"quantity": {
"$lte": 200.0
}
},
{
"quantity": {
"$gte": 100.0
}
}
]
},
"winningPlan": {
"stage": "COLLSCAN",
"filter": {
"$and": [
{
"quantity": {
"$lte": 200.0
}
},
{
"quantity": {
"$gte": 100.0
}
}
]
},
"direction": "forward"
},
"rejectedPlans": []
},
"executionStats": {
"executionSuccess": true,
"nReturned": 3,
"executionTimeMillis": 1,
"totalKeysExamined": 0,
"totalDocsExamined": 10,
"executionStages": {
"stage": "COLLSCAN",
"filter": {
"$and": [
{
"quantity": {
"$lte": 200.0
}
},
{
"quantity": {
"$gte": 100.0
}
}
]
},
"nReturned": 3,
"executionTimeMillisEstimate": 0,
"works": 12,
"advanced": 3,
"needTime": 8,
"needYield": 0,
"saveState": 0,
"restoreState": 0,
"isEOF": 1,
"invalidates": 0,
"direction": "forward",
"docsExamined": 10
}
},
"serverInfo": {
"host": "localhost.localdomain",
"port": 27017,
"version": "3.2.8",
"gitVersion": "ed70e33130c977bda0024c125b56d159573dbaf0"
},
"ok": 1.0
}
3. 使用 single field 加速查找
知道前因后果之后,就可以进行针对性的建立索引,比如拿 quality 字段举例如下:
db.inventory.createIndex({ quantity: 1})
db.inventory.find(
{ quantity: { $gte: 100, $lte: 200} }
).explain( "executionStats")
这时候有意思了,当执行完 createindex 之后,再次 explain,有4个重要的parameters需要特别注意一下:
1) IXSCAN
这个时候再也不是所谓的COLLSCAN了,而是IndexScan,说明已经命中索引了。
2) nReturned,totalDocsExamined,totalKeysExamined
从图中可以看到三个参数都是3,这就说明 mongodb 查看了3个key,3个document,返回了3个文档,这就是所谓的高性能吧。
二:hint演示
说到hint,也挺有意思,它用来强制mongodb 执行指定的索引,为了方便演示,做两组复合索引,比如这次在quality和type上构建一下:
构建完之后,我强制 mongodb 去使用 quantity 开头的复合索引,从而强制mongodb 放弃那个以{type:1,quantity:1}的复合索引,很有意思哦,比如下图:
从图中可以看到,mongodb 检查了6个keys最终找到了2个文档,下面我不hint来看一下mongodb自己抉择的最优plan是怎么样的。
再看上面的图,你应该明白了,mongodb果然执行了那个最优的plan,是不是很好玩,好了,本篇就说到这里,希望对你有帮助~返回搜狐,查看更多
责任编辑: