MongoDB的Basic Indexes 和 Compound Indexes结合使用

MongoDB的Basic Indexes 和 Compound Indexes结合使用

转自:http://blog.csdn.net/crazyjixiang/article/details/6847383

前几天看到一个关于 MongoDB 深入索引的PPT


MongoDB Indexing: The Details


仔细的从头看到尾, 里面有个关于Compound Indexes的Range and Equality的讲解,在ppt第129页,重新回顾下这个例子.向一个collection中插入 9 条数据,像下面这样:

  1. > db.good.find()  
  2. "_id" : ObjectId("4e8d629d8ad8bdf2ed6c1990"), "x" : 1, "y" : "b" }  
  3. "_id" : ObjectId("4e8d62a38ad8bdf2ed6c1991"), "x" : 3, "y" : "d" }  
  4. "_id" : ObjectId("4e8d62ad8ad8bdf2ed6c1992"), "x" : 4, "y" : "g" }  
  5. "_id" : ObjectId("4e8d62b28ad8bdf2ed6c1993"), "x" : 5, "y" : "c" }  
  6. "_id" : ObjectId("4e8d62ba8ad8bdf2ed6c1994"), "x" : 6, "y" : "a" }  
  7. "_id" : ObjectId("4e8d62c18ad8bdf2ed6c1995"), "x" : 7, "y" : "e" }  
  8. "_id" : ObjectId("4e8d62ce8ad8bdf2ed6c1996"), "x" : 8, "y" : "c" }  
  9. "_id" : ObjectId("4e8d62d38ad8bdf2ed6c1997"), "x" : 9, "y" : "f" }  
  10. "_id" : ObjectId("4e8d719a6cee6416a5a75a43"), "x" : 5, "y" : "d" }  

然后给x 和 y进行联合索引

  1. db.good.ensureIndex({x:1,y:1})  

我们来进行这样的查找

  1. <pre name="code" class="cpp">> db.good.find({x:{$gte:4}, y:'c'}).explain()  
  2. {  
  3.     "cursor" : "BtreeCursor x_1_y_1",  
  4.     "nscanned" : 7,  
  5.     "nscannedObjects" : 2,  
  6.     "n" : 2,  
  7.     "millis" : 0,  
  8.     "nYields" : 0,  
  9.     "nChunkSkips" : 0,  
  10.     "isMultiKey" : false,  
  11.     "indexOnly" : false,  
  12.     "indexBounds" : {  
  13.         "x" : [  
  14.             [  
  15.                 4,  
  16.                 1.7976931348623157e+308  
  17.             ]  
  18.         ],  
  19.         "y" : [  
  20.             [  
  21.                 "c",  
  22.                 "c"  
  23.             ]  
  24.         ]  
  25.     }  
  26. }  


 

  可以看出 nscanned 非常高! 而 n只有 2 .官网上有这样一句话: 

If nscanned is much higher than nreturned, the database is scanning many objects to find the target objects. Consider creating an index to improve this.

这里nscanned可以认为是扫描的记录数.n为返回的记录数

让我们配合PPT看下 nscanned:7是怎么来的:


这是MongoDB的B-tree索引树,因为x>=4 && y='c',所以先选择左枝搜索,左枝搜索了4/g 和 5/c ,(5/c符合条件),然后搜索 右枝 搜索了 7/e, 6/a ,8/c, 9/f ,(8/c符合条件). 任何 符合的 x都要被check一下.

延伸:

看了PPT后到此结尾了, 真遇到这种情况,效率可不乐观,于是稍微思考了下, y 在 这颗树中只有两个节点含有,也就是说 既然是 '与'  那就只要先把 y 筛选出来 ,搜索次数就大大减半了 .  

我们在 y 上再进行Basic Indexes 的建立.

  1. db.good.ensureIndex({y:1})  
这样如果搜索时会先 搜索 y  ,也就只有2次搜索了.看下实际情况:

  1. > db.good.find({x:{$gte:4}, y:'c'}).explain()  
  2. {  
  3.     "cursor" : "BtreeCursor y_1",  
  4.     "nscanned" : 2,  
  5.     "nscannedObjects" : 2,  
  6.     "n" : 2,  
  7.     "millis" : 0,  
  8.     "nYields" : 0,  
  9.     "nChunkSkips" : 0,  
  10.     "isMultiKey" : false,  
  11.     "indexOnly" : false,  
  12.     "indexBounds" : {  
  13.         "y" : [  
  14.             [  
  15.                 "c",  
  16.                 "c"  
  17.             ]  
  18.         ]  
  19.     }  
  20. }  

正如预料的一样. 直接走 基本索引了.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值