我试图重现指数路口指令(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