在mongodb日志中显示他们正在使用IXScan
[conn37225]命令磨练。$ cmd命令:count {count:" events",query: {eventName:"已开始",createdAt:{$ gt:新日期(1449789260690), $ lte:new Date(1450221260693)},data.quizId: " 54fe05f26aa23aeb72e3fdbb" },fields:null} planSummary:IXSCAN { createdAt:1,data.quizId:1,eventName:1} keyUpdates:0 numYields:4279 lock(micros)r:53800992 reslen:48 38679ms
但是db.currentOp()
不要
{
"opid" : 4719819,
"active" : true,
"secs_running" : 4,
"microsecs_running" : NumberLong(4221829),
"op" : "query",
"ns" : "hone.events",
"query" : {
"count" : "events",
"query" : {
"eventName" : "Started",
"createdAt" : {
"$gt" : ISODate("2015-12-10T23:14:20.690Z"),
"$lte" : ISODate("2015-12-15T23:14:20.693Z")
},
"data.quizId" : "54fe05f26aa23aeb72e3fdbb"
},
"fields" : null
},
"client" : "10.8.0.7:43823",
"desc" : "conn37225",
"threadId" : "0x7f60aee9a700",
"connectionId" : 37225,
"locks" : {
"^" : "r",
"^hone" : "R"
},
"waitingForLock" : false,
"numYields" : 384,
"lockStats" : {
"timeLockedMicros" : {
"r" : NumberLong(6417513),
"w" : NumberLong(0)
},
"timeAcquiringMicros" : {
"r" : NumberLong(800491),
"w" : NumberLong(0)
}
}
}
mongodb是否正确使用{ createdAt: 1, data.quizId: 1, eventName: 1 }
索引?
将hint()
与explain()
一起使用可以看出差异。
使用hint()
专门使用该索引并且工作得很快。
我想知道导致它变慢的原因。
没有提示
> db.events.find({ eventName: "Completed", createdAt: { $gt: new Date(1449945185127), $lte: new Date(1450377185140) }, "data.quizId": "55ce6dde5239249a70f9ef54" }).explain("executionStats")
{
"cursor" : "BtreeCursor createdAt_1_data.quizId_1_eventName_1",
"isMultiKey" : false,
"n" : 0,
"nscannedObjects" : 0,
"nscanned" : 2519326,
"nscannedObjectsAllPlans" : 2543445,
"nscannedAllPlans" : 7536363,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 79008,
"nChunkSkips" : 0,
"millis" : 74443,
"indexBounds" : {
"createdAt" : [
[
ISODate("2015-12-12T18:33:05.127Z"),
ISODate("2015-12-17T18:33:05.140Z")
]
],
"data.quizId" : [
[
"55ce6dde5239249a70f9ef54",
"55ce6dde5239249a70f9ef54"
]
],
"eventName" : [
[
"Completed",
"Completed"
]
]
},
"allPlans" : [
{
"cursor" : "BtreeCursor createdAt_1_data.quizId_1_eventName_1",
"isMultiKey" : false,
"n" : 0,
"nscannedObjects" : 0,
"nscanned" : 2519326,
"scanAndOrder" : false,
"indexOnly" : false,
"nChunkSkips" : 0,
"indexBounds" : {
"createdAt" : [
[
ISODate("2015-12-12T18:33:05.127Z"),
ISODate("2015-12-17T18:33:05.140Z")
]
],
"data.quizId" : [
[
"55ce6dde5239249a70f9ef54",
"55ce6dde5239249a70f9ef54"
]
],
"eventName" : [
[
"Completed",
"Completed"
]
]
}
},
{
"cursor" : "BtreeCursor eventName_1",
"isMultiKey" : false,
"n" : 0,
"nscannedObjects" : 2497710,
"nscanned" : 2497710,
"scanAndOrder" : false,
"indexOnly" : false,
"nChunkSkips" : 0,
"indexBounds" : {
"eventName" : [
[
"Completed",
"Completed"
]
]
}
},
{
"cursor" : "BtreeCursor createdAt_1_data.questionId_1_eventName_1",
"isMultiKey" : false,
"n" : 0,
"nscannedObjects" : 45735,
"nscanned" : 2519327,
"scanAndOrder" : false,
"indexOnly" : false,
"nChunkSkips" : 0,
"indexBounds" : {
"createdAt" : [
[
ISODate("2015-12-12T18:33:05.127Z"),
ISODate("2015-12-17T18:33:05.140Z")
]
],
"data.questionId" : [
[
{
"$minElement" : 1
},
{
"$maxElement" : 1
}
]
],
"eventName" : [
[
"Completed",
"Completed"
]
]
}
}
],
"server" : "mongodb-05:27017",
"filterSet" : false,
"stats" : {
"type" : "FETCH",
"works" : 2519328,
"yields" : 79008,
"unyields" : 79008,
"invalidates" : 0,
"advanced" : 0,
"needTime" : 2519326,
"needFetch" : 0,
"isEOF" : 1,
"alreadyHasObj" : 0,
"forcedFetches" : 0,
"matchTested" : 0,
"children" : [
{
"type" : "IXSCAN",
"works" : 2519327,
"yields" : 79008,
"unyields" : 79008,
"invalidates" : 0,
"advanced" : 0,
"needTime" : 2519326,
"needFetch" : 0,
"isEOF" : 1,
"keyPattern" : "{ createdAt: 1, data.quizId: 1, eventName: 1 }",
"isMultiKey" : 0,
"boundsVerbose" : "field #0['createdAt']: (new Date(1449945185127), new Date(1450377185140)], field #1['data.quizId']: [\"55ce6dde5239249a70f9ef54\", \"55ce6dde5239249a70f9ef54\"], field #2['eventName']: [\"Completed\", \"Completed\"]",
"yieldMovedCursor" : 0,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0,
"matchTested" : 0,
"keysExamined" : 2519326,
"children" : [ ]
}
]
}
}
提示
> db.events.find({ eventName: "Completed", createdAt: { $gt: new Date(1449945185127), $lte: new Date(1450377185140) }, "data.quizId": "55ce6dde5239249a70f9ef54" }).hint('createdAt_1_data.quizId_1_eventName_1').explain("executionStats")
{
"cursor" : "BtreeCursor createdAt_1_data.quizId_1_eventName_1",
"isMultiKey" : false,
"n" : 0,
"nscannedObjects" : 0,
"nscanned" : 2519326,
"nscannedObjectsAllPlans" : 0,
"nscannedAllPlans" : 2519326,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 19682,
"nChunkSkips" : 0,
"millis" : 8211,
"indexBounds" : {
"createdAt" : [
[
ISODate("2015-12-12T18:33:05.127Z"),
ISODate("2015-12-17T18:33:05.140Z")
]
],
"data.quizId" : [
[
"55ce6dde5239249a70f9ef54",
"55ce6dde5239249a70f9ef54"
]
],
"eventName" : [
[
"Completed",
"Completed"
]
]
},
"allPlans" : [
{
"cursor" : "BtreeCursor createdAt_1_data.quizId_1_eventName_1",
"isMultiKey" : false,
"n" : 0,
"nscannedObjects" : 0,
"nscanned" : 2519326,
"scanAndOrder" : false,
"indexOnly" : false,
"nChunkSkips" : 0,
"indexBounds" : {
"createdAt" : [
[
ISODate("2015-12-12T18:33:05.127Z"),
ISODate("2015-12-17T18:33:05.140Z")
]
],
"data.quizId" : [
[
"55ce6dde5239249a70f9ef54",
"55ce6dde5239249a70f9ef54"
]
],
"eventName" : [
[
"Completed",
"Completed"
]
]
}
}
],
"server" : "mongodb-05:27017",
"filterSet" : false,
"stats" : {
"type" : "FETCH",
"works" : 2519327,
"yields" : 19682,
"unyields" : 19682,
"invalidates" : 0,
"advanced" : 0,
"needTime" : 2519326,
"needFetch" : 0,
"isEOF" : 1,
"alreadyHasObj" : 0,
"forcedFetches" : 0,
"matchTested" : 0,
"children" : [
{
"type" : "IXSCAN",
"works" : 2519327,
"yields" : 19682,
"unyields" : 19682,
"invalidates" : 0,
"advanced" : 0,
"needTime" : 2519326,
"needFetch" : 0,
"isEOF" : 1,
"keyPattern" : "{ createdAt: 1, data.quizId: 1, eventName: 1 }",
"isMultiKey" : 0,
"boundsVerbose" : "field #0['createdAt']: (new Date(1449945185127), new Date(1450377185140)], field #1['data.quizId']: [\"55ce6dde5239249a70f9ef54\", \"55ce6dde5239249a70f9ef54\"], field #2['eventName']: [\"Completed\", \"Completed\"]",
"yieldMovedCursor" : 0,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0,
"matchTested" : 0,
"keysExamined" : 2519326,
"children" : [ ]
}
]
}
}