检查慢查询
没加索引前
> db.values.find({}).sort({close:-1}).limit(1).explain("executionStats")
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "stocks.values",
"indexFilterSet" : false,
"parsedQuery" : {
},
"winningPlan" : {
"stage" : "SORT",
"sortPattern" : {
"close" : -1
},
"limitAmount" : 1,
"inputStage" : {
"stage" : "SORT_KEY_GENERATOR",
"inputStage" : {
"stage" : "COLLSCAN",
"direction" : "forward"
}
}
},
"rejectedPlans" : [ ]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 1, //返回的文档数
"executionTimeMillis" : 11782,//执行时间
"totalKeysExamined" : 0,//扫描索引的数目(没有建索引 所以是0)
"totalDocsExamined" : 4308303,//扫描文档的数目
"executionStages" : {
"stage" : "SORT",
"nReturned" : 1,
"executionTimeMillisEstimate" : 11153,
"works" : 4308308,
"advanced" : 1,
"needTime" : 4308306,
"needYield" : 0,
"saveState" : 34177,
"restoreState" : 34177,
"isEOF" : 1,
"invalidates" : 0,
"sortPattern" : {
"close" : -1
},
"memUsage" : 182,
"memLimit" : 33554432,
"limitAmount" : 1,
"inputStage" : {
"stage" : "SORT_KEY_GENERATOR",
"nReturned" : 4308303,
"executionTimeMillisEstimate" : 9375,
"works" : 4308306,
"advanced" : 4308303,
"needTime" : 2,
"needYield" : 0,
"saveState" : 34177,
"restoreState" : 34177,
"isEOF" : 1,
"invalidates" : 0,
"inputStage" : {
"stage" : "COLLSCAN",
"nReturned" : 4308303,
"executionTimeMillisEstimate" : 2276,
"works" : 4308305,
"advanced" : 4308303,
"needTime" : 1,
"needYield" : 0,
"saveState" : 34177,
"restoreState" : 34177,
"isEOF" : 1,
"invalidates" : 0,
"direction" : "forward",
"docsExamined" : 4308303
}
}
}
},
"serverInfo" : {
"host" : "DESKTOP-ALJ721J",
"port" : 27017,
"version" : "4.0.1",
"gitVersion" : "54f1582fc6eb01de4d4c42f26fc133e623f065fb"
},
"ok" : 1
}
加索引后,时间可能有点长
> db.values.find({}).sort({close:-1}).limit(1).explain("executionStats")
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "stocks.values",
"indexFilterSet" : false,
"parsedQuery" : {
},
"winningPlan" : {
"stage" : "LIMIT",
"limitAmount" : 1,
"inputStage" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"close" : 1
},
"indexName" : "close_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"close" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "backward",
"indexBounds" : {
"close" : [
"[MaxKey, MinKey]"
]
}
}
}
},
"rejectedPlans" : [ ]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 1,//返回的数目
"executionTimeMillis" : 1,//执行时间变成了1ms
"totalKeysExamined" : 1,//检查的索引数目为1
"totalDocsExamined" : 1,//检查的文档数目为1
"executionStages" : {
"stage" : "LIMIT",
"nReturned" : 1,
"executionTimeMillisEstimate" : 0,
"works" : 2,
"advanced" : 1,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"limitAmount" : 1,
"inputStage" : {
"stage" : "FETCH",
"nReturned" : 1,
"executionTimeMillisEstimate" : 0,
"works" : 1,
"advanced" : 1,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 0,
"invalidates" : 0,
"docsExamined" : 1,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 1,
"executionTimeMillisEstimate" : 0,
"works" : 1,
"advanced" : 1,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 0,
"invalidates" : 0,
"keyPattern" : {
"close" : 1
},
"indexName" : "close_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"close" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "backward",
"indexBounds" : {
"close" : [
"[MaxKey, MinKey]"
]
},
"keysExamined" : 1,
"seeks" : 1,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
}
},
"serverInfo" : {
"host" : "DESKTOP-ALJ721J",
"port" : 27017,
"version" : "4.0.1",
"gitVersion" : "54f1582fc6eb01de4d4c42f26fc133e623f065fb"
},
"ok" : 1
}
现在的索引数 一个单键索引_id,一个单键索引 close,一个单键索引stock_symbol
> db.values.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "stocks.values"
},
{
"v" : 2,
"key" : {
"close" : 1
},
"name" : "close_1",
"ns" : "stocks.values"
},
{
"v" : 2,
"key" : {
"stock_symbol" : 1
},
"name" : "stock_symbol_1",
"ns" : "stocks.values"
}
]
执行
> db.values.find({stock_symbol:"ARWR"}).sort({close:1}).explain("executionStats")
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "stocks.values",
"indexFilterSet" : false,
"parsedQuery" : {
"stock_symbol" : {
"$eq" : "ARWR"
}
},
"winningPlan" : {
"stage" : "FETCH",
"filter" : {
"stock_symbol" : {
"$eq" : "ARWR"
}
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"close" : 1
},
"indexName" : "close_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"close" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"close" : [
"[MinKey, MaxKey]"
]
}
}
},
"rejectedPlans" : [
{
"stage" : "SORT",
"sortPattern" : {
"close" : 1
},
"inputStage" : {
"stage" : "SORT_KEY_GENERATOR",
"inputStage" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"stock_symbol" : 1
},
"indexName" : "stock_symbol_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"stock_symbol" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"stock_symbol" : [
"[\"ARWR\", \"ARWR\"]"
]
}
}
}
}
}
]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 2804,
"executionTimeMillis" : 16486,
"totalKeysExamined" : 4308303,
"totalDocsExamined" : 4308303,
"executionStages" : {
"stage" : "FETCH",
"filter" : {
"stock_symbol" : {
"$eq" : "ARWR"
}
},
"nReturned" : 2804,
"executionTimeMillisEstimate" : 15477,
"works" : 4308304,
"advanced" : 2804,
"needTime" : 4305499,
"needYield" : 0,
"saveState" : 34382,
"restoreState" : 34382,
"isEOF" : 1,
"invalidates" : 0,
"docsExamined" : 4308303,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 4308303,
"executionTimeMillisEstimate" : 5226,
"works" : 4308304,
"advanced" : 4308303,
"needTime" : 0,
"needYield" : 0,
"saveState" : 34382,
"restoreState" : 34382,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"close" : 1
},
"indexName" : "close_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"close" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"close" : [
"[MinKey, MaxKey]"
]
},
"keysExamined" : 4308303,
"seeks" : 1,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
},
"serverInfo" : {
"host" : "DESKTOP-ALJ721J",
"port" : 27017,
"version" : "4.0.1",
"gitVersion" : "54f1582fc6eb01de4d4c42f26fc133e623f065fb"
},
"ok" : 1
}
好像全表扫描了。
建立一个复合索引
> db.values.createIndex({stock_symbol:1,close:1})
> db.values.find({stock_symbol:"ARWR"}).sort({close:1}).explain("executionStats")
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "stocks.values",
"indexFilterSet" : false,
"parsedQuery" : {
"stock_symbol" : {
"$eq" : "ARWR"
}
},
"winningPlan" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"stock_symbol" : 1,
"close" : 1
},
"indexName" : "stock_symbol_1_close_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"stock_symbol" : [ ],
"close" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"stock_symbol" : [
"[\"ARWR\", \"ARWR\"]"
],
"close" : [
"[MinKey, MaxKey]"
]
}
}
},
"rejectedPlans" : [
{
"stage" : "SORT",
"sortPattern" : {
"close" : 1
},
"inputStage" : {
"stage" : "SORT_KEY_GENERATOR",
"inputStage" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"stock_symbol" : 1
},
"indexName" : "stock_symbol_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"stock_symbol" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"stock_symbol" : [
"[\"ARWR\", \"ARWR\"]"
]
}
}
}
}
}
]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 2804,
"executionTimeMillis" : 14,
"totalKeysExamined" : 2804,
"totalDocsExamined" : 2804,
"executionStages" : {
"stage" : "FETCH",
"nReturned" : 2804,
"executionTimeMillisEstimate" : 10,
"works" : 2805,
"advanced" : 2804,
"needTime" : 0,
"needYield" : 0,
"saveState" : 23,
"restoreState" : 23,
"isEOF" : 1,
"invalidates" : 0,
"docsExamined" : 2804,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 2804,
"executionTimeMillisEstimate" : 0,
"works" : 2805,
"advanced" : 2804,
"needTime" : 0,
"needYield" : 0,
"saveState" : 23,
"restoreState" : 23,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"stock_symbol" : 1,
"close" : 1
},
"indexName" : "stock_symbol_1_close_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"stock_symbol" : [ ],
"close" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"stock_symbol" : [
"[\"ARWR\", \"ARWR\"]"
],
"close" : [
"[MinKey, MaxKey]"
]
},
"keysExamined" : 2804,
"seeks" : 1,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
},
"serverInfo" : {
"host" : "DESKTOP-ALJ721J",
"port" : 27017,
"version" : "4.0.1",
"gitVersion" : "54f1582fc6eb01de4d4c42f26fc133e623f065fb"
},
"ok" : 1
}
时间降到了14ms
{ "_id" : ObjectId("4d094f7ec96767d7a02a0dbb"), "exchange" : "NASDAQ", "stock_symbol" : "GOOG", "date" : "2005-05-12", "open" : 230.81, "high" : 232.23, "low" : 228.2, "close" : 228.72, "volume" : 8948200, "adj close" : 228.72 }
1.close 和 stock_symbol 各有一个单键索引 现在执行
db.values.find({stock_symbol:"GOOG", close:{$gt:200}}).explain("executionStats")
输出下面的
"nReturned" : 730,
"executionTimeMillis" : 6,
"totalKeysExamined" : 894,
"totalDocsExamined" : 894,
单键索引也是起了作用,查询优化器帮助我们优化,总会选totalKeysExamined和nReturned最接近的。 但这不是最优的,最快的是建立stock_symbol 和 close的复合索引。
2.使用explain(true)查看查询计划
> db.values.find({stock_symbol:"GOOG", close:{$gt:200}}).explain(true)
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "stocks.values",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"stock_symbol" : {
"$eq" : "GOOG"
}
},
{
"close" : {
"$gt" : 200
}
}
]
},
"winningPlan" : {//赢得执行计划
"stage" : "FETCH",
"filter" : {
"close" : {
"$gt" : 200
}
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"stock_symbol" : 1
},
"indexName" : "stock_symbol_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"stock_symbol" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"stock_symbol" : [
"[\"GOOG\", \"GOOG\"]"
]
}
}
},
"rejectedPlans" : [//被拒绝的计划
{
"stage" : "FETCH",
"filter" : {
"stock_symbol" : {
"$eq" : "GOOG"
}
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"close" : 1
},
"indexName" : "close_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"close" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"close" : [
"(200.0, inf.0]"
]
}
}
},
{
"stage" : "FETCH",
"filter" : {
"close" : {
"$gt" : 200
}
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"stock_symbol" : 1,
"volume" : 1
},
"indexName" : "stock_symbol_1_volume_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"stock_symbol" : [ ],
"volume" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"stock_symbol" : [
"[\"GOOG\", \"GOOG\"]"
],
"volume" : [
"[MinKey, MaxKey]"
]
}
}
}
]
},
"executionStats" : {//执行状态
"executionSuccess" : true,
"nReturned" : 730,
"executionTimeMillis" : 5,
"totalKeysExamined" : 894,
"totalDocsExamined" : 894,
"executionStages" : {
"stage" : "FETCH",
"filter" : {
"close" : {
"$gt" : 200
}
},
"nReturned" : 730,
"executionTimeMillisEstimate" : 0,
"works" : 895,
"advanced" : 730,
"needTime" : 164,
"needYield" : 0,
"saveState" : 9,
"restoreState" : 9,
"isEOF" : 1,
"invalidates" : 0,
"docsExamined" : 894,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 894,
"executionTimeMillisEstimate" : 0,
"works" : 895,
"advanced" : 894,
"needTime" : 0,
"needYield" : 0,
"saveState" : 9,
"restoreState" : 9,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"stock_symbol" : 1
},
"indexName" : "stock_symbol_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"stock_symbol" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"stock_symbol" : [
"[\"GOOG\", \"GOOG\"]"
]
},
"keysExamined" : 894,
"seeks" : 1,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
},
"allPlansExecution" : [ //全部的待选执行计划
{
"nReturned" : 101,
"executionTimeMillisEstimate" : 0,
"totalKeysExamined" : 101,
"totalDocsExamined" : 101,
"executionStages" : {
"stage" : "FETCH",
"filter" : {
"close" : {
"$gt" : 200
}
},
"nReturned" : 101,
"executionTimeMillisEstimate" : 0,
"works" : 101,
"advanced" : 101,
"needTime" : 0,
"needYield" : 0,
"saveState" : 2,
"restoreState" : 2,
"isEOF" : 0,
"invalidates" : 0,
"docsExamined" : 101,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 101,
"executionTimeMillisEstimate" : 0,
"works" : 101,
"advanced" : 101,
"needTime" : 0,
"needYield" : 0,
"saveState" : 2,
"restoreState" : 2,
"isEOF" : 0,
"invalidates" : 0,
"keyPattern" : {
"stock_symbol" : 1
},
"indexName" : "stock_symbol_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"stock_symbol" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"stock_symbol" : [
"[\"GOOG\", \"GOOG\"]"
]
},
"keysExamined" : 101,
"seeks" : 1,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
},
{
"nReturned" : 0,
"executionTimeMillisEstimate" : 0,
"totalKeysExamined" : 101,
"totalDocsExamined" : 101,
"executionStages" : {
"stage" : "FETCH",
"filter" : {
"stock_symbol" : {
"$eq" : "GOOG"
}
},
"nReturned" : 0,
"executionTimeMillisEstimate" : 0,
"works" : 101,
"advanced" : 0,
"needTime" : 101,
"needYield" : 0,
"saveState" : 9,
"restoreState" : 9,
"isEOF" : 0,
"invalidates" : 0,
"docsExamined" : 101,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 101,
"executionTimeMillisEstimate" : 0,
"works" : 101,
"advanced" : 101,
"needTime" : 0,
"needYield" : 0,
"saveState" : 9,
"restoreState" : 9,
"isEOF" : 0,
"invalidates" : 0,
"keyPattern" : {
"close" : 1
},
"indexName" : "close_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"close" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"close" : [
"(200.0, inf.0]"
]
},
"keysExamined" : 101,
"seeks" : 1,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
},
{
"nReturned" : 88,
"executionTimeMillisEstimate" : 0,
"totalKeysExamined" : 101,
"totalDocsExamined" : 101,
"executionStages" : {
"stage" : "FETCH",
"filter" : {
"close" : {
"$gt" : 200
}
},
"nReturned" : 88,
"executionTimeMillisEstimate" : 0,
"works" : 101,
"advanced" : 88,
"needTime" : 13,
"needYield" : 0,
"saveState" : 9,
"restoreState" : 9,
"isEOF" : 0,
"invalidates" : 0,
"docsExamined" : 101,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 101,
"executionTimeMillisEstimate" : 0,
"works" : 101,
"advanced" : 101,
"needTime" : 0,
"needYield" : 0,
"saveState" : 9,
"restoreState" : 9,
"isEOF" : 0,
"invalidates" : 0,
"keyPattern" : {
"stock_symbol" : 1,
"volume" : 1
},
"indexName" : "stock_symbol_1_volume_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"stock_symbol" : [ ],
"volume" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"stock_symbol" : [
"[\"GOOG\", \"GOOG\"]"
],
"volume" : [
"[MinKey, MaxKey]"
]
},
"keysExamined" : 101,
"seeks" : 1,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
}
]
},
"serverInfo" : {
"host" : "DESKTOP-ALJ721J",
"port" : 27017,
"version" : "4.0.1",
"gitVersion" : "54f1582fc6eb01de4d4c42f26fc133e623f065fb"
},
"ok" : 1
}