我試圖重現指數路口指令(http://docs.mongodb.org/manual/core/index-intersection/)的第一個例子,但面臨的一個問題:蒙戈不使用股指雙雙爲什麼MongoDB不使用索引相交?
我的步驟:
- 下載蒙戈(3.0.3)和安裝
- 運行的mongod:mongod.exe --dbpath d:\數據(文件夾爲空)
- 運行蒙戈:mongo.exe
添加索引:
db.orders.ensureIndex({ qty: 1 }) db.orders.ensureIndex({ item: 1 }) db.orders.getIndexes() [{ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "test.orders" }, { "v" : 1, "key" : { "qty" : 1 }, "name" : "qty_1", "ns" : "test.orders" }, { "v" : 1, "key" : { "item" : 1 }, "name" : "item_1", "ns" : "test.orders" }]
檢查查詢說明:
db.orders.find({ item: "abc123", qty: { $gt: 15 } }).explain() { "queryPlanner" : { "plannerVersion" : 1, "namespace" : "test.orders", "indexFilterSet" : false, "parsedQuery" : { "$and" : [ { "item" : { "$eq" : "abc123" } }, { "qty" : { "$gt" : 15 } } ] }, "winningPlan" : { "stage" : "KEEP_MUTATIONS", "inputStage" : { "stage" : "FETCH", "filter" : { "qty" : { "$gt" : 15 } }, "inputStage" : { "stage" : "IXSCAN", "keyPattern" : { "item" : 1 }, "indexName" : "item_1", "isMultiKey" : false, "direction" : "forward", "indexBounds" : { "item" : [ "[\"abc123\", \"abc123\"]" ] } } } }, "rejectedPlans" : [ { "stage" : "KEEP_MUTATIONS", "inputStage" : { "stage" : "FETCH", "filter" : { "item" : { "$eq" : "abc123" } }, "inputStage" : { "stage" : "IXSCAN", "keyPattern" : { "qty" : 1 }, "indexName" : "qty_1", "isMultiKey" : false, "direction" : "forward", "indexBounds" : { "qty" : [ "(15.0, 1.#INF]" ] } } } } ] }, "serverInfo" : { "host" : "localhost", "port" : 27017, "version" : "3.0.3", "gitVersion" : "b40106b36eecd1b4407eb1ad1af6bc60593c6105" }, "ok" : 1 }
正如你可以看到winningPlan只包含ITEM_1指數。有包含qty_1索引的rejectPlans。但是沒有包含索引交集的計劃。 我知道有很多條件可以選擇特定的索引。但在我的情況下,mongo甚至沒有計劃!
有人能幫助我嗎?
FWIW,與MongoDB 3.0.2相同 –
數據庫中有多少個文檔?什麼「解釋(真)」說?查詢需要多長時間?數據字段的分佈是什麼,如果有的話? – mnemosyn