我使用mongodb 2.6。我的一个查询有时会选择一个错误的索引来使用。
我的收藏集RawAssetStatistics
有两个索引:\_id.asset_1
和\_id.timestamp_-1__id.asset_1
。
我的查询:
db.RawAssetStatistics.find({
"_id.timestamp": { $lte: ISODate("2016-04-14T06:36:00.000Z"), $gte:ISODate("2016-04-14T05:36:00.000Z") },
"_id.asset":ObjectId('56ec426fdf0f6b4766e93b7f')
}).sort({"_id.timestamp":1})
通常使用\_id.timestamp_-1__id.asset_1
并且需要很少的milis才能完成。但有时会使用\_id.asset_1
,这会更糟糕。
有没有办法检测为什么Mongo做出这么糟糕的决定。
使用提示解释查询以使用\_id.asset_1
:
{
"cursor" : "BtreeCursor _id.asset_1",
"isMultiKey" : false,
"n" : 61,
"nscannedObjects" : 8991,
"nscanned" : 8991,
"nscannedObjectsAllPlans" : 8991,
"nscannedAllPlans" : 8991,
"scanAndOrder" : true,
"indexOnly" : false,
"nYields" : 70,
"nChunkSkips" : 0,
"millis" : 10376,
"indexBounds" : {
"_id.asset" : [
[
ObjectId("56ec426fdf0f6b4766e93b7f"),
ObjectId("56ec426fdf0f6b4766e93b7f")
]
]
},
"server" : "UNI-DB:27017",
"filterSet" : false,
"stats" : {
"type" : "SORT",
"works" : 9055,
"yields" : 70,
"unyields" : 70,
"invalidates" : 89,
"advanced" : 61,
"needTime" : 8992,
"needFetch" : 0,
"isEOF" : 1,
"forcedFetches" : 0,
"memUsage" : 27572,
"memLimit" : 33554432,
"children" : [
{
"type" : "KEEP_MUTATIONS",
"works" : 8992,
"yields" : 70,
"unyields" : 70,
"invalidates" : 89,
"advanced" : 61,
"needTime" : 8930,
"needFetch" : 0,
"isEOF" : 1,
"children" : [
{
"type" : "FETCH",
"works" : 8992,
"yields" : 70,
"unyields" : 70,
"invalidates" : 89,
"advanced" : 61,
"needTime" : 8930,
"needFetch" : 0,
"isEOF" : 1,
"alreadyHasObj" : 0,
"forcedFetches" : 0,
"matchTested" : 61,
"children" : [
{
"type" : "IXSCAN",
"works" : 8992,
"yields" : 70,
"unyields" : 70,
"invalidates" : 89,
"advanced" : 8991,
"needTime" : 0,
"needFetch" : 0,
"isEOF" : 1,
"keyPattern" : "{ _id.asset: 1 }",
"isMultiKey" : 0,
"boundsVerbose" : "field #0['_id.asset']: [ObjectId('56ec426fdf0f6b4766e93b7f'), ObjectId('56ec426fdf0f6b4766e93b7f')]",
"yieldMovedCursor" : 0,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0,
"matchTested" : 0,
"keysExamined" : 8991,
"children" : []
}
]
}
]
}
]
}
}
使用提示解释查询以使用" _id.timestamp_-1__id.asset_1"
{
"cursor" : "BtreeCursor _id.timestamp_-1__id.asset_1 reverse",
"isMultiKey" : false,
"n" : 61,
"nscannedObjects" : 61,
"nscanned" : 181,
"nscannedObjectsAllPlans" : 61,
"nscannedAllPlans" : 181,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 1,
"nChunkSkips" : 0,
"millis" : 2,
"indexBounds" : {
"_id.timestamp" : [
[
ISODate("2016-04-14T05:36:00.000Z"),
ISODate("2016-04-14T06:36:00.000Z")
]
],
"_id.asset" : [
[
ObjectId("56ec426fdf0f6b4766e93b7f"),
ObjectId("56ec426fdf0f6b4766e93b7f")
]
]
},
"server" : "UNI-DB:27017",
"filterSet" : false,
"stats" : {
"type" : "FETCH",
"works" : 182,
"yields" : 1,
"unyields" : 1,
"invalidates" : 0,
"advanced" : 61,
"needTime" : 120,
"needFetch" : 0,
"isEOF" : 1,
"alreadyHasObj" : 0,
"forcedFetches" : 0,
"matchTested" : 0,
"children" : [
{
"type" : "IXSCAN",
"works" : 182,
"yields" : 1,
"unyields" : 1,
"invalidates" : 0,
"advanced" : 61,
"needTime" : 120,
"needFetch" : 0,
"isEOF" : 1,
"keyPattern" : "{ _id.timestamp: -1, _id.asset: 1 }",
"isMultiKey" : 0,
"boundsVerbose" : "field #0['_id.timestamp']: [new Date(1460612160000), new Date(1460615760000)], field #1['_id.asset']: [ObjectId('56ec426fdf0f6b4766e93b7f'), ObjectId('56ec426fdf0f6b4766e93b7f')]",
"yieldMovedCursor" : 0,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0,
"matchTested" : 0,
"keysExamined" : 181,
"children" : []
}
]
}
}