0

数据量:8520206

limit不同,导致查询性能有问题,SQL 1耗时217毫秒,而SQL 2耗时34秒。

SQL 1:

db.alarmdata.explain(“executionStats”).aggregate([
{$match:{“message_type”:1,”$or”:[{“device_no”:”1000023536″},{“device_newno”:”1000023536″}]}},
{$sort:{“time”:-1}},
{$limit : 13 }
])

SQL 2:

db.alarmdata.explain(“executionStats”).aggregate([
{$match:{“message_type”:1,”$or”:[{“device_no”:”1000023536″},{“device_newno”:”1000023536″}]}},
{$sort:{“time”:-1}},
{$limit : 14 }
])

执行计划 1:

{
“stages” : [
{
“$cursor” : {
“query” : {
“message_type” : 1.0,
“$or” : [
{
“device_no” : “1000023536”
},
{
“device_newno” : “1000023536”
}
]
},
“sort” : {
“time” : -1
},
“limit” : NumberLong(13),
“queryPlanner” : {
“plannerVersion” : 1,
“namespace” : “alarmdata”,
“indexFilterSet” : false,
“parsedQuery” : {
“$and” : [
{
“$or” : [
{
“device_newno” : {
“$eq” : “1000023536”
}
},
{
“device_no” : {
“$eq” : “1000023536”
}
}
]
},
{
“message_type” : {
“$eq” : 1.0
}
}
]
},
“queryHash” : “D0A855A8”,
“planCacheKey” : “5CE75013”,
“winningPlan” : {
“stage” : “FETCH”,
“filter” : {
“$or” : [
{
“device_newno” : {
“$eq” : “1000023536”
}
},
{
“device_no” : {
“$eq” : “1000023536”
}
}
]
},
“inputStage” : {
“stage” : “IXSCAN”,
“keyPattern” : {
“message_type” : -1.0,
“time” : -1.0
},
“indexName” : “message_type_-1_time_-1”,
“isMultiKey” : false,
“multiKeyPaths” : {
“message_type” : [],
“time” : []
},
“isUnique” : false,
“isSparse” : false,
“isPartial” : false,
“indexVersion” : 2,
“direction” : “forward”,
“indexBounds” : {
“message_type” : [
“[1.0, 1.0]”
],
“time” : [
“[MaxKey, MinKey]”
]
}
}
},
“rejectedPlans” : []
},
“executionStats” : {
“executionSuccess” : true,
“nReturned” : 13,
“executionTimeMillis” : 217,
“totalKeysExamined” : 94895,
“totalDocsExamined” : 94895,
“executionStages” : {
“stage” : “FETCH”,
“filter” : {
“$or” : [
{
“device_newno” : {
“$eq” : “1000023536”
}
},
{
“device_no” : {
“$eq” : “1000023536”
}
}
]
},
“nReturned” : 13,
“executionTimeMillisEstimate” : 155,
“works” : 94895,
“advanced” : 13,
“needTime” : 94882,
“needYield” : 0,
“saveState” : 742,
“restoreState” : 742,
“isEOF” : 0,
“docsExamined” : 94895,
“alreadyHasObj” : 0,
“inputStage” : {
“stage” : “IXSCAN”,
“nReturned” : 94895,
“executionTimeMillisEstimate” : 46,
“works” : 94895,
“advanced” : 94895,
“needTime” : 0,
“needYield” : 0,
“saveState” : 742,
“restoreState” : 742,
“isEOF” : 0,
“keyPattern” : {
“message_type” : -1.0,
“time” : -1.0
},
“indexName” : “message_type_-1_time_-1”,
“isMultiKey” : false,
“multiKeyPaths” : {
“message_type” : [],
“time” : []
},
“isUnique” : false,
“isSparse” : false,
“isPartial” : false,
“indexVersion” : 2,
“direction” : “forward”,
“indexBounds” : {
“message_type” : [
“[1.0, 1.0]”
],
“time” : [
“[MaxKey, MinKey]”
]
},
“keysExamined” : 94895,
“seeks” : 1,
“dupsTested” : 0,
“dupsDropped” : 0
}
}
}
}
}
],
“serverInfo” : {
“host” : “mongodb”,
“port” : 27317,
“version” : “4.2.5”,
“gitVersion” : “2261279b51ea13df08ae708ff278f0679c59dc32”
},
“ok” : 1.0
}

执行计划 2:

{
“stages” : [
{
“$cursor” : {
“query” : {
“message_type” : 1.0,
“$or” : [
{
“device_no” : “1000023536”
},
{
“device_newno” : “1000023536”
}
]
},
“sort” : {
“time” : -1
},
“limit” : NumberLong(14),
“queryPlanner” : {
“plannerVersion” : 1,
“namespace” : “alarmdata”,
“indexFilterSet” : false,
“parsedQuery” : {
“$and” : [
{
“$or” : [
{
“device_newno” : {
“$eq” : “1000023536”
}
},
{
“device_no” : {
“$eq” : “1000023536”
}
}
]
},
{
“message_type” : {
“$eq” : 1.0
}
}
]
},
“queryHash” : “D0A855A8”,
“planCacheKey” : “5CE75013”,
“winningPlan” : {
“stage” : “FETCH”,
“filter” : {
“$or” : [
{
“device_newno” : {
“$eq” : “1000023536”
}
},
{
“device_no” : {
“$eq” : “1000023536”
}
}
]
},
“inputStage” : {
“stage” : “IXSCAN”,
“keyPattern” : {
“message_type” : -1.0,
“time” : -1.0
},
“indexName” : “message_type_-1_time_-1”,
“isMultiKey” : false,
“multiKeyPaths” : {
“message_type” : [],
“time” : []
},
“isUnique” : false,
“isSparse” : false,
“isPartial” : false,
“indexVersion” : 2,
“direction” : “forward”,
“indexBounds” : {
“message_type” : [
“[1.0, 1.0]”
],
“time” : [
“[MaxKey, MinKey]”
]
}
}
},
“rejectedPlans” : []
},
“executionStats” : {
“executionSuccess” : true,
“nReturned” : 13,
“executionTimeMillis” : 34243,
“totalKeysExamined” : 8222438,
“totalDocsExamined” : 8222438,
“executionStages” : {
“stage” : “FETCH”,
“filter” : {
“$or” : [
{
“device_newno” : {
“$eq” : “1000023536”
}
},
{
“device_no” : {
“$eq” : “1000023536”
}
}
]
},
“nReturned” : 13,
“executionTimeMillisEstimate” : 22390,
“works” : 8222439,
“advanced” : 13,
“needTime” : 8222425,
“needYield” : 0,
“saveState” : 64243,
“restoreState” : 64243,
“isEOF” : 1,
“docsExamined” : 8222438,
“alreadyHasObj” : 0,
“inputStage” : {
“stage” : “IXSCAN”,
“nReturned” : 8222438,
“executionTimeMillisEstimate” : 4845,
“works” : 8222439,
“advanced” : 8222438,
“needTime” : 0,
“needYield” : 0,
“saveState” : 64243,
“restoreState” : 64243,
“isEOF” : 1,
“keyPattern” : {
“message_type” : -1.0,
“time” : -1.0
},
“indexName” : “message_type_-1_time_-1”,
“isMultiKey” : false,
“multiKeyPaths” : {
“message_type” : [],
“time” : []
},
“isUnique” : false,
“isSparse” : false,
“isPartial” : false,
“indexVersion” : 2,
“direction” : “forward”,
“indexBounds” : {
“message_type” : [
“[1.0, 1.0]”
],
“time” : [
“[MaxKey, MinKey]”
]
},
“keysExamined” : 8222438,
“seeks” : 1,
“dupsTested” : 0,
“dupsDropped” : 0
}
}
}
}
}
],
“serverInfo” : {
“host” : “mongodb”,
“port” : 27017,
“version” : “4.2.5”,
“gitVersion” : “2261279b51ea13df08ae708ff278f0679c59dc32”
},
“ok” : 1.0
}

已回答的问题