Here are the compound index and single index I have for this Collection:
///db.Collection.getIndexes()
/* 1 */
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "service.Collection"
},
/* 2 */
{
"v" : 2,
"key" : {
"FirstId" : 1,
"SecondId" : 1,
"CreationTime" : -1
},
"name" : "FirstIdSecondIdCreationTime",
"collation" : {
"locale" : "en",
"caseLevel" : false,
"caseFirst" : "off",
"strength" : 1,
"numericOrdering" : false,
"alternate" : "non-ignorable",
"maxVariable" : "punct",
"normalization" : false,
"backwards" : false,
"version" : "57.1"
},
"ns" : "service.Collection"
},
/* 3 */
{
"v" : 2,
"key" : {
"CreationTime" : 1
},
"name" : "CreationTime",
"collation" : {
"locale" : "en",
"caseLevel" : false,
"caseFirst" : "off",
"strength" : 1,
"numericOrdering" : false,
"alternate" : "non-ignorable",
"maxVariable" : "punct",
"normalization" : false,
"backwards" : false,
"version" : "57.1"
},
"ns" : "service.Collection"
}
The expected result is an IXSCAN using the FirstIdSecondIdCreationTime index:
///service.Collection.find({ FirstId: "771367b7-4bef-49ab-bda1-6230254c6349", ///SecondId: "3bffb3cd-fb5e-43e5-abd1-e0b48c97f78f" })
/// .projection({})
/// .sort({_id:-1}).hint("FirstIdSecondIdCreationTime").explain('executionStats')
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "service.Collection",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"FirstId" : {
"$eq" : "771367b7-4bef-49ab-bda1-6230254c6349"
}
},
{
"SecondId" : {
"$eq" : "3bffb3cd-fb5e-43e5-abd1-e0b48c97f78f"
}
}
]
},
"winningPlan" : {
"stage" : "SORT",
"sortPattern" : {
"_id" : -1
},
"inputStage" : {
"stage" : "SORT_KEY_GENERATOR",
"inputStage" : {
"stage" : "FETCH",
"filter" : {
"$and" : [
{
"FirstId" : {
"$eq" : "771367b7-4bef-49ab-bda1-6230254c6349"
}
},
{
"SecondId" : {
"$eq" : "3bffb3cd-fb5e-43e5-abd1-e0b48c97f78f"
}
}
]
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"FirstId" : 1,
"SecondId" : 1,
"CreationTime" : -1
},
"indexName" : "FirstIdSecondIdCreationTime",
"collation" : {
"locale" : "en",
"caseLevel" : false,
"caseFirst" : "off",
"strength" : 1,
"numericOrdering" : false,
"alternate" : "non-ignorable",
"maxVariable" : "punct",
"normalization" : false,
"backwards" : false,
"version" : "57.1"
},
"isMultiKey" : false,
"multiKeyPaths" : {
"FirstId" : [ ],
"SecondId" : [ ],
"CreationTime" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"FirstId" : [
"[MinKey, MaxKey]"
],
"SecondId" : [
"[MinKey, MaxKey]"
],
"CreationTime" : [
"[MaxKey, MinKey]"
]
}
}
}
}
},
"rejectedPlans" : [ ]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 1,
"executionTimeMillis" : 5491,
"totalKeysExamined" : 856730,
"totalDocsExamined" : 856730,
"executionStages" : {
"stage" : "SORT",
"nReturned" : 1,
"executionTimeMillisEstimate" : 5261,
"works" : 856734,
"advanced" : 1,
"needTime" : 856732,
"needYield" : 0,
"saveState" : 6697,
"restoreState" : 6697,
"isEOF" : 1,
"invalidates" : 0,
"sortPattern" : {
"_id" : -1
},
"memUsage" : 432,
"memLimit" : 33554432,
"inputStage" : {
"stage" : "SORT_KEY_GENERATOR",
"nReturned" : 1,
"executionTimeMillisEstimate" : 5201,
"works" : 856732,
"advanced" : 1,
"needTime" : 856730,
"needYield" : 0,
"saveState" : 6697,
"restoreState" : 6697,
"isEOF" : 1,
"invalidates" : 0,
"inputStage" : {
"stage" : "FETCH",
"filter" : {
"$and" : [
{
"FirstId" : {
"$eq" : "771367b7-4bef-49ab-bda1-6230254c6349"
}
},
{
"SecondId" : {
"$eq" : "3bffb3cd-fb5e-43e5-abd1-e0b48c97f78f"
}
}
]
},
"nReturned" : 1,
"executionTimeMillisEstimate" : 5131,
"works" : 856731,
"advanced" : 1,
"needTime" : 856729,
"needYield" : 0,
"saveState" : 6697,
"restoreState" : 6697,
"isEOF" : 1,
"invalidates" : 0,
"docsExamined" : 856730,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 856730,
"executionTimeMillisEstimate" : 820,
"works" : 856731,
"advanced" : 856730,
"needTime" : 0,
"needYield" : 0,
"saveState" : 6697,
"restoreState" : 6697,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"FirstId" : 1,
"SecondId" : 1,
"CreationTime" : -1
},
"indexName" : "FirstIdSecondIdCreationTime",
"collation" : {
"locale" : "en",
"caseLevel" : false,
"caseFirst" : "off",
"strength" : 1,
"numericOrdering" : false,
"alternate" : "non-ignorable",
"maxVariable" : "punct",
"normalization" : false,
"backwards" : false,
"version" : "57.1"
},
"isMultiKey" : false,
"multiKeyPaths" : {
"FirstId" : [ ],
"SecondId" : [ ],
"CreationTime" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"FirstId" : [
"[MinKey, MaxKey]"
],
"SecondId" : [
"[MinKey, MaxKey]"
],
"CreationTime" : [
"[MaxKey, MinKey]"
]
},
"keysExamined" : 856730,
"seeks" : 1,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0,
"indexDef" : {
"indexName" : "FirstIdSecondIdCreationTime",
"isMultiKey" : false,
"multiKeyPaths" : {
"FirstId" : [ ],
"SecondId" : [ ],
"CreationTime" : [ ]
},
"keyPattern" : {
"FirstId" : 1,
"SecondId" : 1,
"CreationTime" : -1
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"direction" : "forward"
}
}
}
}
}
but the actual result is a COLLSCAN that takes over 8000ms:
"event": {
"dataset": "mongodb.log",
"module": "mongodb"
},
"service": {
"type": "mongodb"
},
"message": "command service.Collection command: find { find: \"Collection\",
filter: { FirstId: \"771367b7-4bef-49ab-bda1-6230254c6349\", SecondId: \"3bffb3cd-fb5e-43e5-abd1-e0b48c97f78f\" }, sort: { CreationTime: -1 }, limit: 1,
planSummary: COLLSCAN keysExamined:0 docsExamined:784787 hasSortStage:1 cursorExhausted:1 numYields:6175 nreturned:1 reslen:677
locks:{ Global: { acquireCount: { r: 12352 } }, Database: { acquireCount: { r: 6176 } }, Collection: { acquireCount: { r: 6176 } } } protocol:op_msg 8441ms",
"mongodb.docsExamined": 784787,
"fileset": {
"name": "log"
},
Why am I COLLSCANing instead of IXSCANing with the FirstIdSecondIDCreationTime compound index? Is there a way to change my index/ my query to speed up the query?
Per a suggestion in the comments, I've run explain("allPlansExecution").
///db.Collection.find({ FirstId: "771367b7-4bef-49ab-bda1-6230254c6349", ///SecondId: "3bffb3cd-fb5e-43e5-abd1-e0b48c97f78f" })
/// .projection({})
/// .sort({_id:-1}).explain('allPlansExecution')
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "service.Collection",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"FirstId" : {
"$eq" : "771367b7-4bef-49ab-bda1-6230254c6349"
}
},
{
"SecondId" : {
"$eq" : "3bffb3cd-fb5e-43e5-abd1-e0b48c97f78f"
}
}
]
},
"winningPlan" : {
"stage" : "FETCH",
"filter" : {
"$and" : [
{
"FirstId" : {
"$eq" : "771367b7-4bef-49ab-bda1-6230254c6349"
}
},
{
"SecondId" : {
"$eq" : "3bffb3cd-fb5e-43e5-abd1-e0b48c97f78f"
}
}
]
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"_id" : 1
},
"indexName" : "_id_",
"isMultiKey" : false,
"multiKeyPaths" : {
"_id" : [ ]
},
"isUnique" : true,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "backward",
"indexBounds" : {
"_id" : [
"[MaxKey, MinKey]"
]
}
}
},
"rejectedPlans" : [ ]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 1,
"executionTimeMillis" : 5408,
"totalKeysExamined" : 856748,
"totalDocsExamined" : 856748,
"executionStages" : {
"stage" : "FETCH",
"filter" : {
"$and" : [
{
"FirstId" : {
"$eq" : "771367b7-4bef-49ab-bda1-6230254c6349"
}
},
{
"SecondId" : {
"$eq" : "3bffb3cd-fb5e-43e5-abd1-e0b48c97f78f"
}
}
]
},
"nReturned" : 1,
"executionTimeMillisEstimate" : 4862,
"works" : 856749,
"advanced" : 1,
"needTime" : 856747,
"needYield" : 0,
"saveState" : 6694,
"restoreState" : 6694,
"isEOF" : 1,
"invalidates" : 0,
"docsExamined" : 856748,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 856748,
"executionTimeMillisEstimate" : 1220,
"works" : 856749,
"advanced" : 856748,
"needTime" : 0,
"needYield" : 0,
"saveState" : 6694,
"restoreState" : 6694,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"_id" : 1
},
"indexName" : "_id_",
"isMultiKey" : false,
"multiKeyPaths" : {
"_id" : [ ]
},
"isUnique" : true,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "backward",
"indexBounds" : {
"_id" : [
"[MaxKey, MinKey]"
]
},
"keysExamined" : 856748,
"seeks" : 1,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
},
"allPlansExecution" : [ ]
}
}
The "FirstIdSecondIdCreationTime" index was not automatically considered because it was created with a collation, and the query is being run without a collation.
Use the .collation() cursor method to specify the same collation for the query that was used for the index.
The 5.5 second run time using that index is pretty slow as well.
You may see some improvement in that query if you create an index on {FirstId: 1, SecondId: 1, _id: 1} so that they query executor can use the index to meet the sort instead of an in-memory sort.
Can you please sort by leading indexes i.e firstId, secondId and creationTime in the same sequence and see index is used. it will give an idea whether leading indexes fields should be there in sort as well.
Related
I have the MongoDB C# Driver 2.14.1 and a Mongo Server v3.6 running on a Docker container. I'm running an easy (I think) query over 3 collections. The biggest collection has 400k documents, each document averaged 64Kb. I'm filtering by two fields and returning the results in a cursor to reduce overhead. The results are sorted by descending by a field that is configured as an index as well. This is happening on my production server which is taking nearly 1GB of memory (not being released afterwards). The following queries explained (run on the server):
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "table1",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"Type" : {
"$eq" : "Type1"
}
},
{
"LocationId" : {
"$eq" : "Location1"
}
},
{
"Date" : {
"$gt" : ISODate("2021-01-18T23:00:00Z")
}
}
]
},
"winningPlan" : {
"stage" : "FETCH",
"filter" : {
"$and" : [
{
"Type" : {
"$eq" : "Type1"
}
},
{
"LocationId" : {
"$eq" : "Location1"
}
}
]
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"Date" : -1
},
"indexName" : "Date_-1",
"isMultiKey" : false,
"multiKeyPaths" : {
"Date" : [ ]
},
"isUnique" : false,
"isSparse" : true,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"Date" : [
"[new Date(9223372036854775807), new Date(1611010800000))"
]
}
}
},
"rejectedPlans" : [ ]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 1,
"executionTimeMillis" : 2,
"totalKeysExamined" : 7,
"totalDocsExamined" : 7,
"executionStages" : {
"stage" : "FETCH",
"filter" : {
"$and" : [
{
"Type" : {
"$eq" : "Type1"
}
},
{
"LocationId" : {
"$eq" : "Location1"
}
}
]
},
"nReturned" : 1,
"executionTimeMillisEstimate" : 0,
"works" : 8,
"advanced" : 1,
"needTime" : 6,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"docsExamined" : 7,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 7,
"executionTimeMillisEstimate" : 0,
"works" : 8,
"advanced" : 7,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"Date" : -1
},
"indexName" : "Date_-1",
"isMultiKey" : false,
"multiKeyPaths" : {
"Date" : [ ]
},
"isUnique" : false,
"isSparse" : true,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"Date" : [
"[new Date(9223372036854775807), new Date(1611010800000))"
]
},
"keysExamined" : 7,
"seeks" : 1,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
},
"serverInfo" : {
"host" : "7aa23bd7fe20",
"port" : 27018,
"version" : "3.6.18",
"gitVersion" : "2005f25eed7ed88fa698d9b800fe536bb0410ba4"
},
"ok" : 1
}
The second query:
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "table2",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"Type" : {
"$eq" : "Type1"
}
},
{
"LocationId" : {
"$eq" : "02"
}
},
{
"Date" : {
"$gt" : ISODate("2021-01-18T23:00:00Z")
}
}
]
},
"winningPlan" : {
"stage" : "FETCH",
"filter" : {
"$and" : [
{
"Type" : {
"$eq" : "Type1"
}
},
{
"LocationId" : {
"$eq" : "02"
}
}
]
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"Date" : -1
},
"indexName" : "Date_-1",
"isMultiKey" : false,
"multiKeyPaths" : {
"Date" : [ ]
},
"isUnique" : false,
"isSparse" : true,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"Date" : [
"[new Date(9223372036854775807), new Date(1611010800000))"
]
}
}
},
"rejectedPlans" : [ ]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 1,
"executionTimeMillis" : 63,
"totalKeysExamined" : 9896,
"totalDocsExamined" : 9896,
"executionStages" : {
"stage" : "FETCH",
"filter" : {
"$and" : [
{
"Type" : {
"$eq" : "Type1"
}
},
{
"LocationId" : {
"$eq" : "02"
}
}
]
},
"nReturned" : 1,
"executionTimeMillisEstimate" : 50,
"works" : 9897,
"advanced" : 1,
"needTime" : 9895,
"needYield" : 0,
"saveState" : 77,
"restoreState" : 77,
"isEOF" : 1,
"invalidates" : 0,
"docsExamined" : 9896,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 9896,
"executionTimeMillisEstimate" : 0,
"works" : 9897,
"advanced" : 9896,
"needTime" : 0,
"needYield" : 0,
"saveState" : 77,
"restoreState" : 77,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"Date" : -1
},
"indexName" : "Date_-1",
"isMultiKey" : false,
"multiKeyPaths" : {
"Date" : [ ]
},
"isUnique" : false,
"isSparse" : true,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"Date" : [
"[new Date(9223372036854775807), new Date(1611010800000))"
]
},
"keysExamined" : 9896,
"seeks" : 1,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
},
"serverInfo" : {
"host" : "7aa23bd7fe20",
"port" : 27018,
"version" : "3.6.18",
"gitVersion" : "2005f25eed7ed88fa698d9b800fe536bb0410ba4"
},
"ok" : 1
}
And the third:
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "table3",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"Type" : {
"$eq" : "Type1"
}
},
{
"LocationId" : {
"$eq" : "52"
}
},
{
"Date" : {
"$gt" : ISODate("2021-01-18T23:00:00Z")
}
}
]
},
"winningPlan" : {
"stage" : "FETCH",
"filter" : {
"$and" : [
{
"Type" : {
"$eq" : "Type1"
}
},
{
"LocationId" : {
"$eq" : "52"
}
}
]
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"Date" : -1
},
"indexName" : "Date_-1",
"isMultiKey" : false,
"multiKeyPaths" : {
"Date" : [ ]
},
"isUnique" : false,
"isSparse" : true,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"Date" : [
"[new Date(9223372036854775807), new Date(1611010800000))"
]
}
}
},
"rejectedPlans" : [ ]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 30,
"executionTimeMillis" : 934,
"totalKeysExamined" : 366370,
"totalDocsExamined" : 366370,
"executionStages" : {
"stage" : "FETCH",
"filter" : {
"$and" : [
{
"Type" : {
"$eq" : "Type1"
}
},
{
"LocationId" : {
"$eq" : "52"
}
}
]
},
"nReturned" : 30,
"executionTimeMillisEstimate" : 889,
"works" : 366371,
"advanced" : 30,
"needTime" : 366340,
"needYield" : 0,
"saveState" : 2862,
"restoreState" : 2862,
"isEOF" : 1,
"invalidates" : 0,
"docsExamined" : 366370,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 366370,
"executionTimeMillisEstimate" : 160,
"works" : 366371,
"advanced" : 366370,
"needTime" : 0,
"needYield" : 0,
"saveState" : 2862,
"restoreState" : 2862,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"Date" : -1
},
"indexName" : "Date_-1",
"isMultiKey" : false,
"multiKeyPaths" : {
"Date" : [ ]
},
"isUnique" : false,
"isSparse" : true,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"Date" : [
"[new Date(9223372036854775807), new Date(1611010800000))"
]
},
"keysExamined" : 366370,
"seeks" : 1,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
},
"serverInfo" : {
"host" : "7aa23bd7fe20",
"port" : 27018,
"version" : "3.6.18",
"gitVersion" : "2005f25eed7ed88fa698d9b800fe536bb0410ba4"
},
"ok" : 1
}
Now with my little knowledge of the "explainPlan" I would have imagined that the performance is OK, and not many records get returned on each query. Having said that I can see a pattern when I run the code via the .NET application (C# code) that makes the CPU go through the roof and right after the memory follows in the Mongo server Docker container.
I don't know if there is misconfigured or whether I can do something to improve the performance of it because I'm quite puzzle with the performance on just that query - I think is this query because the rest of the queries with similar amount of data on different parts of the system work quite well. Besides that, is there any mechanism or tool I can use to diagnose the memory consumption in my Mongo server?
I'm using mongodb 3.2.7, docs like:
{
"finished" : true,
"buildNo": 1,
"tryTimes" : 1,
"createdTime" : ISODate("2019-05-16T19:00:50.604+08:00"),
"modifiedTime" : ISODate("2019-12-27T18:33:25.682+08:00"),
}
I created my index using:
db.getCollection('builds').createIndex({"createdTime": 1})
db.getCollection('builds').createIndex({"tryTimes": 1, "createdTime": -1})
and my query is :
db.getCollection('builds').find({
"createdTime": {$lte: new Date("2020-04-15T00:00:00.000Z"), $gte: new Date("2020-04-01T00:00:00.000Z")},
"buildNo": 1,
"finished": {$ne: true}
})
.sort({"tryTimes": 1, "createdTime": -1})
.limit(200)
But this query doesn't use the index I created before. the explain() output:
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "builds",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"buildNo" : {
"$eq" : 1.0
}
},
{
"createdTime" : {
"$lte" : ISODate("2020-04-15T08:00:00.000+08:00")
}
},
{
"createdTime" : {
"$gte" : ISODate("2020-04-01T08:00:00.000+08:00")
}
},
{
"$not" : {
"finished" : {
"$eq" : true
}
}
}
]
},
"winningPlan" : {
"stage" : "SORT",
"sortPattern" : {
"tryTimes" : 1.0,
"createdTime" : -1.0
},
"limitAmount" : 200,
"inputStage" : {
"stage" : "SORT_KEY_GENERATOR",
"inputStage" : {
"stage" : "FETCH",
"filter" : {
"$and" : [
{
"buildNo" : {
"$eq" : 1.0
}
},
{
"$not" : {
"finished" : {
"$eq" : true
}
}
}
]
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"createdTime" : 1.0
},
"indexName" : "createdTime_1",
"isMultiKey" : false,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"createdTime" : [
"[new Date(1585699200000), new Date(1586908800000)]"
]
}
}
}
}
},
"rejectedPlans" : [
{
"stage" : "LIMIT",
"limitAmount" : 200,
"inputStage" : {
"stage" : "FETCH",
"filter" : {
"$and" : [
{
"buildNo" : {
"$eq" : 1.0
}
},
{
"createdTime" : {
"$lte" : ISODate("2020-04-15T08:00:00.000+08:00")
}
},
{
"createdTime" : {
"$gte" : ISODate("2020-04-01T08:00:00.000+08:00")
}
},
{
"$not" : {
"finished" : {
"$eq" : true
}
}
}
]
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"tryTimes" : 1.0,
"createdTime" : -1.0
},
"indexName" : "tryTimes_1_createdTime_-1",
"isMultiKey" : false,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"tryTimes" : [
"[MinKey, MaxKey]"
],
"createdTime" : [
"[MaxKey, MinKey]"
]
}
}
}
}
]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 200,
"executionTimeMillis" : 352,
"totalKeysExamined" : 48256,
"totalDocsExamined" : 48256,
"executionStages" : {
"stage" : "SORT",
"nReturned" : 200,
"executionTimeMillisEstimate" : 200,
"works" : 48459,
"advanced" : 200,
"needTime" : 48258,
"needYield" : 0,
"saveState" : 757,
"restoreState" : 757,
"isEOF" : 1,
"invalidates" : 0,
"sortPattern" : {
"tryTimes" : 1.0,
"createdTime" : -1.0
},
"memUsage" : 198706,
"memLimit" : 33554432,
"limitAmount" : 200,
"inputStage" : {
"stage" : "SORT_KEY_GENERATOR",
"nReturned" : 0,
"executionTimeMillisEstimate" : 180,
"works" : 48258,
"advanced" : 0,
"needTime" : 24539,
"needYield" : 0,
"saveState" : 757,
"restoreState" : 757,
"isEOF" : 1,
"invalidates" : 0,
"inputStage" : {
"stage" : "FETCH",
"filter" : {
"$and" : [
{
"buildNo" : {
"$eq" : 1.0
}
},
{
"$not" : {
"finished" : {
"$eq" : true
}
}
}
]
},
"nReturned" : 23718,
"executionTimeMillisEstimate" : 160,
"works" : 48257,
"advanced" : 23718,
"needTime" : 24538,
"needYield" : 0,
"saveState" : 757,
"restoreState" : 757,
"isEOF" : 1,
"invalidates" : 0,
"docsExamined" : 48256,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 48256,
"executionTimeMillisEstimate" : 20,
"works" : 48257,
"advanced" : 48256,
"needTime" : 0,
"needYield" : 0,
"saveState" : 757,
"restoreState" : 757,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"createdTime" : 1.0
},
"indexName" : "createdTime_1",
"isMultiKey" : false,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"createdTime" : [
"[new Date(1585699200000), new Date(1586908800000)]"
]
},
"keysExamined" : 48256,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
}
}
},
"serverInfo" : {
"host" : "...",
"port" : 9999,
"version" : "3.2.7",
"gitVersion" : "4249c1d2b5999ebbf1fdf3bc0e0e3b3ff5c0aaf2"
},
"ok" : 1.0}
even if I try to simplify the query to
db.getCollection('builds')
.find({ "buildNo": 1, "finished": true })
.sort({"tryTimes": 1, "createdTime": -1})
.limit(200)
, the index still doesn't work.
The wierd things is: I use the same index and query on another mongondb ver. 3.4.14.3. The index works...
Any ideas is welcomed and appreciated.
Thanks a lot!
If a query can be satisfied by multiple indexes (satisfied is used losely as Mongo actually chooses all possibly relevant indexes) defined in the collection, MongoDB will then test all the applicable indexes in parallel (meaning Mongo's performs a "race"). The first index that can returns 101 results will be selected by the query planner.
Meaning that for that certain query your using the "wrong" index wins.
What can you do?:
You can use $hint, hint basically forces Mongo to use a specific index, however Mongo this is not recommended because if changes occur Mongo will not adapt to those.
Like so:
db.getCollection('builds')
.find({ "buildNo": 1, "finished": true })
.sort({"tryTimes": 1, "createdTime": -1})
.limit(200)
.hint({tryTimes: 1, createdTime: -1})
I'm testing below aggregate
db.getCollection('token_transfer').explain("executionStats").aggregate([
{$match: {
$and: [{"contract_address":"0xTTTTTTTTTTT"}, {"timestamp": {$gte: ISODate("2019-12-13T00:45:05.000Z")}}]
}},
{$group: {
_id: null,
transfer_count: {$sum: 1},
transfer_volume_sum: {$sum: "$value"},
}}
])
indexes of the token_transfer collection is like below
{
"contract_address" : 1.0,
"value" : -1.0,
"timestamp" : 1.0
},
{
"tx_hash" : "hashed"
}
...
token_transfer collection has below fields
[contract_address, from, to, value, tx_hash, timestamp, ...]
shard key is like below
shard key: { "tx_hash" : "hashed" }
Key point is that it tooks 42706 executionTimeMillis at only shd2.
The other shards show under 5000 executionTimeMillis.
Why does slowness occur on certain shard which results in total query slow?
Below is my executionStats of aggregate.
/* 1 */
{
"mergeType" : "mongos",
"splitPipeline" : {
"shardsPart" : [
{
"$match" : {
"$and" : [
{
"contract_address" : {
"$eq" : "0xTTTTTTTTTTT"
}
},
{
"timestamp" : {
"$gte" : ISODate("2019-12-13T00:45:05.000Z")
}
}
]
}
},
{
"$group" : {
"_id" : {
"$const" : null
},
"transfer_count" : {
"$sum" : {
"$const" : 1.0
}
},
"transfer_volume_sum" : {
"$sum" : "$value"
}
}
}
],
"mergerPart" : [
{
"$group" : {
"_id" : "$$ROOT._id",
"transfer_count" : {
"$sum" : "$$ROOT.transfer_count"
},
"transfer_volume_sum" : {
"$sum" : "$$ROOT.transfer_volume_sum"
},
"$doingMerge" : true
}
}
]
},
"shards" : {
"shd3" : {
"host" : "x-db1:33333",
"stages" : [
{
"$cursor" : {
"query" : {
"$and" : [
{
"contract_address" : "0xTTTTTTTTTTT"
},
{
"timestamp" : {
"$gte" : ISODate("2019-12-13T00:45:05.000Z")
}
}
]
},
"fields" : {
"value" : 1,
"_id" : 0
},
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "test.token_transfer",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"contract_address" : {
"$eq" : "0xTTTTTTTTTTT"
}
},
{
"timestamp" : {
"$gte" : ISODate("2019-12-13T00:45:05.000Z")
}
}
]
},
"winningPlan" : {
"stage" : "PROJECTION",
"transformBy" : {
"value" : 1,
"_id" : 0
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"contract_address" : 1.0,
"value" : -1.0,
"timestamp" : 1.0
},
"indexName" : "contract_address_1_value_-1_timestamp_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"contract_address" : [],
"value" : [],
"timestamp" : []
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"contract_address" : [
"[\"0xTTTTTTTTTTT\", \"0xTTTTTTTTTTT\"]"
],
"value" : [
"[MaxKey, MinKey]"
],
"timestamp" : [
"[new Date(1576197905000), new Date(9223372036854775807)]"
]
}
}
},
"rejectedPlans" : []
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 671,
"executionTimeMillis" : 1384,
"totalKeysExamined" : 1231507,
"totalDocsExamined" : 0,
"executionStages" : {
"stage" : "PROJECTION",
"nReturned" : 671,
"executionTimeMillisEstimate" : 1370,
"works" : 1231507,
"advanced" : 671,
"needTime" : 1230835,
"needYield" : 0,
"saveState" : 9622,
"restoreState" : 9622,
"isEOF" : 1,
"invalidates" : 0,
"transformBy" : {
"value" : 1,
"_id" : 0
},
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 671,
"executionTimeMillisEstimate" : 1340,
"works" : 1231507,
"advanced" : 671,
"needTime" : 1230835,
"needYield" : 0,
"saveState" : 9622,
"restoreState" : 9622,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"contract_address" : 1.0,
"value" : -1.0,
"timestamp" : 1.0
},
"indexName" : "contract_address_1_value_-1_timestamp_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"contract_address" : [],
"value" : [],
"timestamp" : []
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"contract_address" : [
"[\"0xTTTTTTTTTTT\", \"0xTTTTTTTTTTT\"]"
],
"value" : [
"[MaxKey, MinKey]"
],
"timestamp" : [
"[new Date(1576197905000), new Date(9223372036854775807)]"
]
},
"keysExamined" : 1231507,
"seeks" : 1230836,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
}
}
},
{
"$group" : {
"_id" : {
"$const" : null
},
"transfer_count" : {
"$sum" : {
"$const" : 1.0
}
},
"transfer_volume_sum" : {
"$sum" : "$value"
}
}
}
]
},
"shd1" : {
"host" : "x-db2:11111",
"stages" : [
{
"$cursor" : {
"query" : {
"$and" : [
{
"contract_address" : "0xTTTTTTTTTTT"
},
{
"timestamp" : {
"$gte" : ISODate("2019-12-13T00:45:05.000Z")
}
}
]
},
"fields" : {
"value" : 1,
"_id" : 0
},
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "test.token_transfer",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"contract_address" : {
"$eq" : "0xTTTTTTTTTTT"
}
},
{
"timestamp" : {
"$gte" : ISODate("2019-12-13T00:45:05.000Z")
}
}
]
},
"winningPlan" : {
"stage" : "PROJECTION",
"transformBy" : {
"value" : 1,
"_id" : 0
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"contract_address" : 1.0,
"value" : -1.0,
"timestamp" : 1.0
},
"indexName" : "contract_address_1_value_-1_timestamp_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"contract_address" : [],
"value" : [],
"timestamp" : []
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"contract_address" : [
"[\"0xTTTTTTTTTTT\", \"0xTTTTTTTTTTT\"]"
],
"value" : [
"[MaxKey, MinKey]"
],
"timestamp" : [
"[new Date(1576197905000), new Date(9223372036854775807)]"
]
}
}
},
"rejectedPlans" : []
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 680,
"executionTimeMillis" : 4545,
"totalKeysExamined" : 1234518,
"totalDocsExamined" : 0,
"executionStages" : {
"stage" : "PROJECTION",
"nReturned" : 680,
"executionTimeMillisEstimate" : 3685,
"works" : 1234518,
"advanced" : 680,
"needTime" : 1233837,
"needYield" : 0,
"saveState" : 9656,
"restoreState" : 9656,
"isEOF" : 1,
"invalidates" : 0,
"transformBy" : {
"value" : 1,
"_id" : 0
},
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 680,
"executionTimeMillisEstimate" : 3685,
"works" : 1234518,
"advanced" : 680,
"needTime" : 1233837,
"needYield" : 0,
"saveState" : 9656,
"restoreState" : 9656,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"contract_address" : 1.0,
"value" : -1.0,
"timestamp" : 1.0
},
"indexName" : "contract_address_1_value_-1_timestamp_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"contract_address" : [],
"value" : [],
"timestamp" : []
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"contract_address" : [
"[\"0xTTTTTTTTTTT\", \"0xTTTTTTTTTTT\"]"
],
"value" : [
"[MaxKey, MinKey]"
],
"timestamp" : [
"[new Date(1576197905000), new Date(9223372036854775807)]"
]
},
"keysExamined" : 1234518,
"seeks" : 1233838,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
}
}
},
{
"$group" : {
"_id" : {
"$const" : null
},
"transfer_count" : {
"$sum" : {
"$const" : 1.0
}
},
"transfer_volume_sum" : {
"$sum" : "$value"
}
}
}
]
},
"shd2" : {
"host" : "x-db3:22222",
"stages" : [
{
"$cursor" : {
"query" : {
"$and" : [
{
"contract_address" : "0xTTTTTTTTTTT"
},
{
"timestamp" : {
"$gte" : ISODate("2019-12-13T00:45:05.000Z")
}
}
]
},
"fields" : {
"value" : 1,
"_id" : 0
},
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "test.token_transfer",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"contract_address" : {
"$eq" : "0xTTTTTTTTTTT"
}
},
{
"timestamp" : {
"$gte" : ISODate("2019-12-13T00:45:05.000Z")
}
}
]
},
"winningPlan" : {
"stage" : "PROJECTION",
"transformBy" : {
"value" : 1,
"_id" : 0
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"contract_address" : 1.0,
"value" : -1.0,
"timestamp" : 1.0
},
"indexName" : "contract_address_1_value_-1_timestamp_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"contract_address" : [],
"value" : [],
"timestamp" : []
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"contract_address" : [
"[\"0xTTTTTTTTTTT\", \"0xTTTTTTTTTTT\"]"
],
"value" : [
"[MaxKey, MinKey]"
],
"timestamp" : [
"[new Date(1576197905000), new Date(9223372036854775807)]"
]
}
}
},
"rejectedPlans" : []
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 720,
"executionTimeMillis" : 42706,
"totalKeysExamined" : 1181569,
"totalDocsExamined" : 0,
"executionStages" : {
"stage" : "PROJECTION",
"nReturned" : 720,
"executionTimeMillisEstimate" : 4284,
"works" : 1181569,
"advanced" : 720,
"needTime" : 1180848,
"needYield" : 0,
"saveState" : 9249,
"restoreState" : 9249,
"isEOF" : 1,
"invalidates" : 0,
"transformBy" : {
"value" : 1,
"_id" : 0
},
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 720,
"executionTimeMillisEstimate" : 4244,
"works" : 1181569,
"advanced" : 720,
"needTime" : 1180848,
"needYield" : 0,
"saveState" : 9249,
"restoreState" : 9249,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"contract_address" : 1.0,
"value" : -1.0,
"timestamp" : 1.0
},
"indexName" : "contract_address_1_value_-1_timestamp_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"contract_address" : [],
"value" : [],
"timestamp" : []
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"contract_address" : [
"[\"0xTTTTTTTTTTT\", \"0xTTTTTTTTTTT\"]"
],
"value" : [
"[MaxKey, MinKey]"
],
"timestamp" : [
"[new Date(1576197905000), new Date(9223372036854775807)]"
]
},
"keysExamined" : 1181569,
"seeks" : 1180849,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
}
}
},
{
"$group" : {
"_id" : {
"$const" : null
},
"transfer_count" : {
"$sum" : {
"$const" : 1.0
}
},
"transfer_volume_sum" : {
"$sum" : "$value"
}
}
}
]
}
},
"ok" : 1.0,
"$clusterTime" : {
"clusterTime" : Timestamp(1576201630, 116),
"signature" : {
"hash" : { "$binary" : "AAAAAAAAAAAAAAAAAAAAAAAAAAA=", "$type" : "00" },
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1576201630, 115)
}
There is index in 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
}
But the query which uses this index is running very slow for no reason:
> 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
}
Explain
{
"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
}
As you can see there are no other queries that can lock database or collection.
What can cause such behavior?
I would like to ask about mongodb indexes. Can I use a different index in the find and the sort. By example I have two indexes:
(a:-1)
(b:1,c:1)
What indexes uses this sentence?
({a:[$gt30}},{a:[$lt50}}]}.sort({c:1})
Can I use a different index in the find and the sort.
After reading some more into this you will see at the bottom of the documentation page on index intersectioning: http://docs.mongodb.org/manual/core/index-intersection/#index-intersection-and-sort
Index intersection does not apply when the sort() operation requires an index completely separate from the query predicate.
So no, even if ypou created an index of {c:1} it could not be used independantly to intersect {a:1}
What indexes uses this sentence?
In this case only {a:1} will be used.
Creating an index on a single field called Single Field Index.
Creating multiple Single Field indexes to boost your query and the sort performance won't help much!. You should use Compound Indexes instead.
Check the documentation on MongoDB: https://docs.mongodb.com/manual/core/index-compound/
If you want to learn how to index your fields and how to measure the performance of your queries.
And Check this tutorial on Youtube: https://dplink.app/nxLgvk7lR
Suppose I am having persons collection having documents like below :
{
dob:
{ age : 50} ,
gender : "male" ,
phone : ""
}
Now i create indexes as below .
1 : db.persons.createIndex({"dob.age" : -1})
2 : db.persons.createIndex({phone : 1 , gender : 1})
Now If i execute below query like yours
db.persons.explain("executionStats").find({$and : [ {"dob.age" : {$lt : 50} } ,
{"dob.age" : {$gt : 30} } ] } ).sort({gender : 1 })
I will get below execution stats :
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "college.persons",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"dob.age" : {
"$lt" : 50
}
},
{
"dob.age" : {
"$gt" : 30
}
}
]
},
"queryHash" : "22FEA299",
"planCacheKey" : "5E8F38C1",
"winningPlan" : {
"stage" : "SORT",
"sortPattern" : {
"phone" : 1
},
"inputStage" : {
"stage" : "SORT_KEY_GENERATOR",
"inputStage" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"dob.age" : -1
},
"indexName" : "dob.age_-1",
"isMultiKey" : false,
"multiKeyPaths" : {
"dob.age" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"dob.age" : [
"(50.0, 30.0)"
]
}
}
}
}
},
"rejectedPlans" : [ ]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 1734,
"executionTimeMillis" : 10,
"totalKeysExamined" : 1734,
"totalDocsExamined" : 1734,
"executionStages" : {
"stage" : "SORT",
"nReturned" : 1734,
"executionTimeMillisEstimate" : 0,
"works" : 3471,
"advanced" : 1734,
"needTime" : 1736,
"needYield" : 0,
"saveState" : 27,
"restoreState" : 27,
"isEOF" : 1,
"sortPattern" : {
"phone" : 1
},
"memUsage" : 1914799,
"memLimit" : 33554432,
"inputStage" : {
"stage" : "SORT_KEY_GENERATOR",
"nReturned" : 1734,
"executionTimeMillisEstimate" : 0,
"works" : 1736,
"advanced" : 1734,
"needTime" : 1,
"needYield" : 0,
"saveState" : 27,
"restoreState" : 27,
"isEOF" : 1,
"inputStage" : {
"stage" : "FETCH",
"nReturned" : 1734,
"executionTimeMillisEstimate" : 0,
"works" : 1735,
"advanced" : 1734,
"needTime" : 0,
"needYield" : 0,
"saveState" : 27,
"restoreState" : 27,
"isEOF" : 1,
"docsExamined" : 1734,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 1734,
"executionTimeMillisEstimate" : 0,
"works" : 1735,
"advanced" : 1734,
"needTime" : 0,
"needYield" : 0,
"saveState" : 27,
"restoreState" : 27,
"isEOF" : 1,
"keyPattern" : {
"dob.age" : -1
},
"indexName" : "dob.age_-1",
"isMultiKey" : false,
"multiKeyPaths" : {
"dob.age" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"dob.age" : [
"(50.0, 30.0)"
]
},
"keysExamined" : 1734,
"seeks" : 1,
"dupsTested" : 0,
"dupsDropped" : 0
}
}
}
}
},
"serverInfo" : {
"host" : "RGGYSLT-0483",
"port" : 27017,
"version" : "4.2.0",
"gitVersion" : "a4b751dcf51dd249c5865812b390cfd1c0129c30"
},
"ok" : 1
}
This Means Data was fetched with IXScan on single field first and then sorted on
third field separately .
But the moment i change the query to sort on fields for which the index is already created things change . Now {"dob.age" : -1} index gets rejected .
In Mongo db Winning plan is the one for which 100 docs can be fetched early and Mongo db caches that plan for a query . Now this cache will be purged after 1000 docs insertions , index rebuild , server restarts or new index insertions.
Hence which index will be used depends upon winning plans .
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "college.persons",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"dob.age" : {
"$lt" : 50
}
},
{
"dob.age" : {
"$gt" : 30
}
}
]
},
"queryHash" : "DA8248FA",
"planCacheKey" : "E779554F",
"winningPlan" : {
"stage" : "FETCH",
"filter" : {
"$and" : [
{
"dob.age" : {
"$lt" : 50
}
},
{
"dob.age" : {
"$gt" : 30
}
}
]
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"gender" : 1,
"phone" : 1
},
"indexName" : "gender_1_phone_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"gender" : [ ],
"phone" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"gender" : [
"[MinKey, MaxKey]"
],
"phone" : [
"[MinKey, MaxKey]"
]
}
}
},
"rejectedPlans" : [
{
"stage" : "SORT",
"sortPattern" : {
"gender" : 1,
"phone" : 1
},
"inputStage" : {
"stage" : "SORT_KEY_GENERATOR",
"inputStage" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"dob.age" : -1
},
"indexName" : "dob.age_-1",
"isMultiKey" : false,
"multiKeyPaths" : {
"dob.age" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"dob.age" : [
"(50.0, 30.0)"
]
}
}
}
}
}
]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 1734,
"executionTimeMillis" : 12,
"totalKeysExamined" : 5002,
"totalDocsExamined" : 5002,
"executionStages" : {
"stage" : "FETCH",
"filter" : {
"$and" : [
{
"dob.age" : {
"$lt" : 50
}
},
{
"dob.age" : {
"$gt" : 30
}
}
]
},
"nReturned" : 1734,
"executionTimeMillisEstimate" : 0,
"works" : 5003,
"advanced" : 1734,
"needTime" : 3268,
"needYield" : 0,
"saveState" : 41,
"restoreState" : 41,
"isEOF" : 1,
"docsExamined" : 5002,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 5002,
"executionTimeMillisEstimate" : 0,
"works" : 5003,
"advanced" : 5002,
"needTime" : 0,
"needYield" : 0,
"saveState" : 41,
"restoreState" : 41,
"isEOF" : 1,
"keyPattern" : {
"gender" : 1,
"phone" : 1
},
"indexName" : "gender_1_phone_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"gender" : [ ],
"phone" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"gender" : [
"[MinKey, MaxKey]"
],
"phone" : [
"[MinKey, MaxKey]"
]
},
"keysExamined" : 5002,
"seeks" : 1,
"dupsTested" : 0,
"dupsDropped" : 0
}
}
},
"serverInfo" : {
"host" : "RGGYSLT-0483",
"port" : 27017,
"version" : "4.2.0",
"gitVersion" : "a4b751dcf51dd249c5865812b390cfd1c0129c30"
},
"ok" : 1
}