【问题】$or中的数据作为索引的一些问题

由于满足 "101 <= C2 <= 200"这个关系的document只有5998条,因此很希望能够C2作为索引:

db.test.find({C1:{$lte:20120306235959},$or:[{C2:{$gte:101,$lte:200}}]}).hint({C2:1}).sort({C16:-1}).limit(23).explain()
        "cursor" : "BtreeCursor C2_1",
        "nscanned" : 2317450,
        "nscannedObjects" : 5998,
        "n" : 23,
        "scanAndOrder" : true,
        "millis" : 5094,
        "nYields" : 6,
        "nChunkSkips" : 0,
        "isMultiKey" : false,
        "indexOnly" : false,
        "indexBounds" : {
                "C2" : [
                        [
                                {
                                        "$minElement" : 1
                                },
                                {
                                        "$maxElement" : 1
                                }
                        ]
                ]
        }
       
2317450是document总的大小,还是把所有的数据都扫描了一遍

如果去掉$or

db.test.find({C1:{$lte:20120306235959}, C2:{$gte:101,$lte:200}}).hint({C2:1}).sort({C16:-1}).limit(23).explain()
        "cursor" : "BtreeCursor C2_1",
        "nscanned" : 5998,
        "nscannedObjects" : 5998,
        "n" : 23,
        "scanAndOrder" : true,
        "millis" : 63,
        "nYields" : 0,
        "nChunkSkips" : 0,
        "isMultiKey" : false,
        "indexOnly" : false,
        "indexBounds" : {
                "C2" : [
                        [
                                101,
                                200
                        ]
                ]
        }
这个才是希望的结果

但由于业务的需求,$or是需要的,不能去掉。

这个问题正在询问中。。。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值