db.collection.explain()
评估查询的性能
inventory包含以下文档的集合:
1
2
3
4
5
6
7
8
9
10
|
{ "_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(含)的文档 :
1
|
db.inventory.find( { quantity: { $gte: 100, $lte: 200 } } )
|
该查询返回以下文档:
1
2
3
|
{ "_id" : 2, "item" : "f2", "type" : "food", "quantity" : 100 }
{ "_id" : 3, "item" : "p1", "type" : "paper", "quantity" : 200 }
{ "_id" : 4, "item" : "p2", "type" : "paper", "quantity" : 150 }
|
要查看所选的查询计划,请将 cursor.explain("executionStats")
游标方法链接到find命令的末尾:
1
2
3
|
db.inventory.find(
{ quantity: { $gte: 100, $lte: 200 } }
).explain("executionStats")
|
explain()返回以下结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
{
"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表示查询匹配并返回三个文档。
- executionStats.totalKeysExamined显示0 以指示这是查询未使用索引。
- executionStats.totalDocsExamined显示10 以指示MongoDB必须扫描十个文档(即集合中的所有文档)才能找到三个匹配的文档。
匹配文档数和已检查文档数之间的差异可能表明,为了提高效率,查询可能会受益于索引的使用。
索引查询
要支持对该quantity字段的查询,请在该字段上添加一个索引 quantity:
1
|
db.inventory.createIndex( { quantity: 1 } )
|
要查看查询计划统计信息,请使用以下 explain(“executionStats”)方法:
1
2
3
|
db.inventory.find(
{ quantity: { $gte: 100, $lte: 200 } }
).explain("executionStats")
|
该explain()方法返回以下结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
{
"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表示查询匹配并返回三个文档。
- executionStats.totalKeysExamined显示3 以指示MongoDB扫描了三个索引条目。检查的键数与返回的文档数相匹配,这意味着mongod只需检查索引键即可返回结果。在 mongod没有扫描所有的文件,只有三个匹配文档不得不被拉入内存中。这导致非常有效的查询。
- executionStats.totalDocsExamined屏幕显示3 MongoDB扫描了三个文档。
没有索引,查询将扫描整个10 文档集合以返回3匹配的文档。该查询还必须扫描每个文档的整体,可能会将它们拉入内存。这导致昂贵且可能缓慢的查询操作。
当使用索引运行时,查询会扫描3索引条目和3文档以返回3匹配的文档,从而产生非常高效的查询。
比较索引表现
若要手动比较使用多个索引的查询的性能,可以将该hint() 方法与该explain() 方法结合使用。
考虑以下查询:
1
2
3
4
5
6
|
db.inventory.find( {
quantity: {
$gte: 100, $lte: 300
},
type: "food"
} )
|
该查询返回以下文档:
1
2
|
{ "_id" : 2, "item" : "f2", "type" : "food", "quantity" : 100 }
{ "_id" : 5, "item" : "f3", "type" : "food", "quantity" : 300 }
|
要支持查询,请添加复合索引。对于复合索引,字段的顺序很重要。
例如,添加以下两个复合索引。第一个索引按quantity字段顺序排序,然后按 type字段排序。第二个索引按顺序排序type,然后按quantity字段排序。
1
2
|
db.inventory.createIndex( { quantity: 1, type: 1 } )
db.inventory.createIndex( { type: 1, quantity: 1 } )
|
评估第一个索引对查询的影响:
1
2
3
|
db.inventory.find(
{ quantity: { $gte: 100, $lte: 300 }, type: "food" }
).hint({ quantity: 1, type: 1 }).explain("executionStats")
|
该explain()方法返回以下输出:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
{
"queryPlanner" : {
...
"winningPlan" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"quantity" : 1,
"type" : 1
},
...
}
}
},
"rejectedPlans" : [ ]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 2,
"executionTimeMillis" : 0,
"totalKeysExamined" : 5,
"totalDocsExamined" : 2,
"executionStages" : {
...
}
},
...
}
|
MongoDB扫描了5个索引键(executionStats.totalKeysExamined)以返回2个匹配的文档(executionStats.nReturned)。
评估第二个索引对查询的影响:
1
2
3
|
db.inventory.find(
{ quantity: { $gte: 100, $lte: 300 }, type: "food" }
).hint({ type: 1, quantity: 1 }).explain("executionStats")
|
该explain()方法返回以下输出:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
{
"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 }效率更高。