查看查询过程的方法:
> db.inventory.find({"quantity": 500,
"type":"toys"}).limit(1).explain("executionStats")
查看所有的使用index的查询方案:
mongoDBv3.0 interprets true as allPlansExecution and false as queryPlanner
db.values.find({stock_symbol: "GOOG", close: {$gt: 200}}).explain(true)
强制使用某种index的方法
It’s understandable why the optimizer rejects the collection scan, but it might be less clear why the index on {close: 1} doesn’t satisfy. You can use hint() to find out.
hint() forces the query optimizer to use a particular index:
query = {stock_symbol: "GOOG", close: {$gt: 200}}
db.values.find(query).hint({close: 1}).explain()
index cache
当查询执行成功后,该查询模式会被记录下来,如下:
{
pattern: {
stock_symbol: 'equality',
close: 'bound',
index: {
stock_symbol: 1
},
nscanned: 894
}
}
这个模式记录了当你请求stock_symbol的精确匹配(equality)和close的区间匹配(bound)时,使用index{stock_symbol:1}
index cache失效的三种情景:
1. 100 writes are made to the collection.
2. Indexes are added or removed from the collection.
3. A query using a cached query plan does a lot more work than expected. Here,what qualifies as “a lot more work” is a value for nscanned exceeding the cached nscanned value by at least a factor of 10.
本文介绍了MongoDB中如何查看查询过程、所有使用索引的查询方案及如何强制使用特定索引的方法。此外还讨论了索引缓存的工作原理及其失效条件。
1707

被折叠的 条评论
为什么被折叠?



