MongoDB in Action Second Edition笔记之基于Index查询优化

      • 查看查询过程的方法
      • 查看所有的使用index的查询方案
      • 强制使用某种index的方法
      • index cache

查看查询过程的方法:

> 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)