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

本文介绍了MongoDB中如何查看查询过程、所有使用索引的查询方案及如何强制使用特定索引的方法。此外还讨论了索引缓存的工作原理及其失效条件。

查看查询过程的方法:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值