mongodb之使用之使用explain和和hint性能分析和优化性能分析和优化
当你第一眼看到explain和hint的时候,第一个反应就是mysql中所谓的这两个关键词,确实可以看出,这个就是在mysql中借
鉴过来的,既然是借鉴 过来的,我想大家都知道这两个关键字的用处,话不多说,速速观看~~~
一:explain演示
1. 构建数据
为了方便演示,我需要create ten data to inventory,而且还是要在no index 的情况下,比如下面这样:
1 db.inventory.insertMany([
2 { "_id" : 1, "item" : "f1", type: "food", quantity: 500 },
3 { "_id" : 2, "item" : "f2", type: "food", quantity: 100 },
4 { "_id" : 3, "item" : "p1", type: "paper", quantity: 200 },
5 { "_id" : 4, "item" : "p2", type: "paper", quantity: 150 },
6 { "_id" : 5, "item" : "f3", type: "food", quantity: 300 },
7 { "_id" : 6, "item" : "t1", type: "toys", quantity: 500 },
8 { "_id" : 7, "item" : "a1", type: "apparel", quantity: 250 },
9 { "_id" : 8, "item" : "a2", type: "apparel", quantity: 400 },
10 { "_id" : 9, "item" : "t2", type: "toys", quantity: 50 },
11 { "_id" : 10, "item" : "f4", type: "food", quantity: 75 }]);
2. 无索引查询
b.inventory.find(
{ quantity: { $gte: 100, $lte: 200 } }
).explain("executionStats")