MongoDB中有索引:
{
"v" : 1,
"key" : {
"project_id" : 1,
"parse_date" : 1
},
"name" : "project_id_1_parse_date_1",
"ns" : "test.offer",
"partialFilterExpression" : {
"active" : false,
"errors" : {
"$exists" : true
}
},
"background" : true
}
但是使用此索引的查询运行速度非常慢:
> db.currentOp()
{
"inprog" : [
{
"desc" : "conn16685",
"threadId" : "119533779793664",
"connectionId" : 16685,
"client" : "127.0.0.1:49154",
"active" : true,
"opid" : 100695187,
"secs_running" : 60,
"microsecs_running" : NumberLong(60950200),
"op" : "command",
"ns" : "test.offer",
"query" : {
"count" : "offer",
"query" : {
"project_id" : ObjectId("5818acae439a6b1e588b4568"),
"parse_date" : ISODate("2017-01-20T08:21:51.876Z"),
"active" : false,
"errors" : {
"$exists" : true
}
}
},
"planSummary" : "IXSCAN { project_id: 1.0, parse_date: 1.0 }",
"numYields" : 2422,
"locks" : {
"Global" : "r",
"Database" : "r",
"Collection" : "r"
},
"waitingForLock" : false,
"lockStats" : {
"Global" : {
"acquireCount" : {
"r" : NumberLong(4846)
}
},
"Database" : {
"acquireCount" : {
"r" : NumberLong(2423)
}
},
"Collection" : {
"acquireCount" : {
"r" : NumberLong(2423)
}
}
}
},
{
"desc" : "conn17711",
"threadId" : "119533772400384",
"connectionId" : 17711,
"client" : "127.0.0.1:45980",
"appName" : "MongoDB Shell",
"active" : true,
"opid" : 100827280,
"secs_running" : 0,
"microsecs_running" : NumberLong(73),
"op" : "command",
"ns" : "admin.$cmd",
"query" : {
"currentOp" : 1
},
"numYields" : 0,
"locks" : {
},
"waitingForLock" : false,
"lockStats" : {
}
}
],
"ok" : 1
}
解释
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "test.offer",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"active" : {
"$eq" : false
}
},
{
"parse_date" : {
"$eq" : ISODate("2017-01-20T08:21:51.876Z")
}
},
{
"project_id" : {
"$eq" : ObjectId("5818acae439a6b1e588b4568")
}
},
{
"errors" : {
"$exists" : true
}
}
]
},
"winningPlan" : {
"stage" : "FETCH",
"filter" : {
"$and" : [
{
"active" : {
"$eq" : false
}
},
{
"errors" : {
"$exists" : true
}
}
]
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"project_id" : 1,
"parse_date" : 1
},
"indexName" : "project_id_1_parse_date_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"project_id" : [ ],
"parse_date" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : true,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"project_id" : [
"[ObjectId('5818acae439a6b1e588b4568'), ObjectId('5818acae439a6b1e588b4568')]"
],
"parse_date" : [
"[new Date(1484900511876), new Date(1484900511876)]"
]
}
}
},
"rejectedPlans" : [
{
"stage" : "FETCH",
"filter" : {
"errors" : {
"$exists" : true
}
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"project_id" : 1,
"parse_date" : 1,
"active" : 1
},
"indexName" : "project_id_1_parse_date_1_active_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"project_id" : [ ],
"parse_date" : [ ],
"active" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"project_id" : [
"[ObjectId('5818acae439a6b1e588b4568'), ObjectId('5818acae439a6b1e588b4568')]"
],
"parse_date" : [
"[new Date(1484900511876), new Date(1484900511876)]"
],
"active" : [
"[false, false]"
]
}
}
},
{
"stage" : "FETCH",
"filter" : {
"$and" : [
{
"parse_date" : {
"$eq" : ISODate("2017-01-20T08:21:51.876Z")
}
},
{
"errors" : {
"$exists" : true
}
}
]
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"project_id" : 1,
"active" : 1
},
"indexName" : "project_id_1_active_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"project_id" : [ ],
"active" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : true,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"project_id" : [
"[ObjectId('5818acae439a6b1e588b4568'), ObjectId('5818acae439a6b1e588b4568')]"
],
"active" : [
"[false, false]"
]
}
}
}
]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 8084,
"executionTimeMillis" : 21352,
"totalKeysExamined" : 8084,
"totalDocsExamined" : 8084,
"executionStages" : {
"stage" : "FETCH",
"filter" : {
"$and" : [
{
"active" : {
"$eq" : false
}
},
{
"errors" : {
"$exists" : true
}
}
]
},
"nReturned" : 8084,
"executionTimeMillisEstimate" : 21333,
"works" : 8085,
"advanced" : 8084,
"needTime" : 0,
"needYield" : 0,
"saveState" : 998,
"restoreState" : 998,
"isEOF" : 1,
"invalidates" : 0,
"docsExamined" : 8084,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 8084,
"executionTimeMillisEstimate" : 120,
"works" : 8085,
"advanced" : 8084,
"needTime" : 0,
"needYield" : 0,
"saveState" : 998,
"restoreState" : 998,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"project_id" : 1,
"parse_date" : 1
},
"indexName" : "project_id_1_parse_date_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"project_id" : [ ],
"parse_date" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : true,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"project_id" : [
"[ObjectId('5818acae439a6b1e588b4568'), ObjectId('5818acae439a6b1e588b4568')]"
],
"parse_date" : [
"[new Date(1484900511876), new Date(1484900511876)]"
]
},
"keysExamined" : 8084,
"seeks" : 1,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
},
"allPlansExecution" : [
{
"nReturned" : 99,
"executionTimeMillisEstimate" : 0,
"totalKeysExamined" : 101,
"totalDocsExamined" : 101,
"executionStages" : {
"stage" : "FETCH",
"filter" : {
"errors" : {
"$exists" : true
}
},
"nReturned" : 99,
"executionTimeMillisEstimate" : 0,
"works" : 101,
"advanced" : 99,
"needTime" : 2,
"needYield" : 0,
"saveState" : 6,
"restoreState" : 6,
"isEOF" : 0,
"invalidates" : 0,
"docsExamined" : 101,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 101,
"executionTimeMillisEstimate" : 0,
"works" : 101,
"advanced" : 101,
"needTime" : 0,
"needYield" : 0,
"saveState" : 6,
"restoreState" : 6,
"isEOF" : 0,
"invalidates" : 0,
"keyPattern" : {
"project_id" : 1,
"parse_date" : 1,
"active" : 1
},
"indexName" : "project_id_1_parse_date_1_active_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"project_id" : [ ],
"parse_date" : [ ],
"active" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"project_id" : [
"[ObjectId('5818acae439a6b1e588b4568'), ObjectId('5818acae439a6b1e588b4568')]"
],
"parse_date" : [
"[new Date(1484900511876), new Date(1484900511876)]"
],
"active" : [
"[false, false]"
]
},
"keysExamined" : 101,
"seeks" : 1,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
},
{
"nReturned" : 3,
"executionTimeMillisEstimate" : 0,
"totalKeysExamined" : 101,
"totalDocsExamined" : 101,
"executionStages" : {
"stage" : "FETCH",
"filter" : {
"$and" : [
{
"parse_date" : {
"$eq" : ISODate("2017-01-20T08:21:51.876Z")
}
},
{
"errors" : {
"$exists" : true
}
}
]
},
"nReturned" : 3,
"executionTimeMillisEstimate" : 0,
"works" : 101,
"advanced" : 3,
"needTime" : 98,
"needYield" : 0,
"saveState" : 6,
"restoreState" : 6,
"isEOF" : 0,
"invalidates" : 0,
"docsExamined" : 101,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 101,
"executionTimeMillisEstimate" : 0,
"works" : 101,
"advanced" : 101,
"needTime" : 0,
"needYield" : 0,
"saveState" : 6,
"restoreState" : 6,
"isEOF" : 0,
"invalidates" : 0,
"keyPattern" : {
"project_id" : 1,
"active" : 1
},
"indexName" : "project_id_1_active_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"project_id" : [ ],
"active" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : true,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"project_id" : [
"[ObjectId('5818acae439a6b1e588b4568'), ObjectId('5818acae439a6b1e588b4568')]"
],
"active" : [
"[false, false]"
]
},
"keysExamined" : 101,
"seeks" : 1,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
},
{
"nReturned" : 101,
"executionTimeMillisEstimate" : 0,
"totalKeysExamined" : 101,
"totalDocsExamined" : 101,
"executionStages" : {
"stage" : "FETCH",
"filter" : {
"$and" : [
{
"active" : {
"$eq" : false
}
},
{
"errors" : {
"$exists" : true
}
}
]
},
"nReturned" : 101,
"executionTimeMillisEstimate" : 0,
"works" : 101,
"advanced" : 101,
"needTime" : 0,
"needYield" : 0,
"saveState" : 6,
"restoreState" : 6,
"isEOF" : 0,
"invalidates" : 0,
"docsExamined" : 101,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 101,
"executionTimeMillisEstimate" : 0,
"works" : 101,
"advanced" : 101,
"needTime" : 0,
"needYield" : 0,
"saveState" : 6,
"restoreState" : 6,
"isEOF" : 0,
"invalidates" : 0,
"keyPattern" : {
"project_id" : 1,
"parse_date" : 1
},
"indexName" : "project_id_1_parse_date_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"project_id" : [ ],
"parse_date" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : true,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"project_id" : [
"[ObjectId('5818acae439a6b1e588b4568'), ObjectId('5818acae439a6b1e588b4568')]"
],
"parse_date" : [
"[new Date(1484900511876), new Date(1484900511876)]"
]
},
"keysExamined" : 101,
"seeks" : 1,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
}
]
},
"serverInfo" : {
"host" : "...",
"port" : 27017,
"version" : "3.4.1",
"gitVersion" : "5e103c4f5583e2566a45d740225dc250baacfbd7"
},
"ok" : 1
}
如您所见,没有其他查询可以锁定数据库或集合。
什么会导致这种行为?