0

我正在尝试优化此查询,但无法让它使用索引。只需$or查询,字段上的 3 个索引就$or可以正常工作。但是,如果我$and在那里添加它,那么它只使用actor._id索引。

{
  '$and': [
    {
      '$or': [
        {
          'object._id': ObjectId('512fbef8a989b28ddb5cdf93')
        },
        {
          'target._id': ObjectId('512fbef8a989b28ddb5cdf93')
        },
        {
          'target.user_id': ObjectId('512fbef8a989b28ddb5cdf93')
        }
      ]
    },
    {
      'actor._id': {
        '$ne': ObjectId('512fbef8a989b28ddb5cdf93')
      }
    }
  ]
}

有人可以帮助我更好地优化此查询或优化索引吗?

提前致谢

按照这里的要求,是每个的解释结果。

随着和

{
"cursor" : "BtreeCursor actor._id_1 multi",
"isMultiKey" : false,
"n" : 2464,
"nscannedObjects" : 1761201,
"nscanned" : 1761202,
"nscannedObjectsAllPlans" : 1761201,
"nscannedAllPlans" : 1761202,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 3,
"nChunkSkips" : 0,
"millis" : 6215,
"indexBounds" : {
    "actor._id" : [
        [
            {
                "$minElement" : 1
            },
            ObjectId("512fbef8a989b28ddb5cdf93")
        ],
        [
            ObjectId("512fbef8a989b28ddb5cdf93"),
            {
                "$maxElement" : 1
            }
        ]
    ]
},
"server" : "Michaels-MacBook-Pro.local:27017"

}

没有 $and

{
"clauses" : [
    {
        "cursor" : "BtreeCursor object._id_1",
        "isMultiKey" : false,
        "n" : 388,
        "nscannedObjects" : 388,
        "nscanned" : 388,
        "nscannedObjectsAllPlans" : 388,
        "nscannedAllPlans" : 388,
        "scanAndOrder" : false,
        "indexOnly" : false,
        "nYields" : 0,
        "nChunkSkips" : 0,
        "millis" : 4,
        "indexBounds" : {
            "object._id" : [
                [
                    ObjectId("512fbef8a989b28ddb5cdf93"),
                    ObjectId("512fbef8a989b28ddb5cdf93")
                ]
            ]
        }
    },
    {
        "cursor" : "BtreeCursor target._id_1",
        "isMultiKey" : false,
        "n" : 77,
        "nscannedObjects" : 375,
        "nscanned" : 375,
        "nscannedObjectsAllPlans" : 375,
        "nscannedAllPlans" : 375,
        "scanAndOrder" : false,
        "indexOnly" : false,
        "nYields" : 0,
        "nChunkSkips" : 0,
        "millis" : 3,
        "indexBounds" : {
            "target._id" : [
                [
                    ObjectId("512fbef8a989b28ddb5cdf93"),
                    ObjectId("512fbef8a989b28ddb5cdf93")
                ]
            ]
        }
    },
    {
        "cursor" : "BtreeCursor target.user_id_1",
        "isMultiKey" : false,
        "n" : 2554,
        "nscannedObjects" : 2554,
        "nscanned" : 2554,
        "nscannedObjectsAllPlans" : 2554,
        "nscannedAllPlans" : 2554,
        "scanAndOrder" : false,
        "indexOnly" : false,
        "nYields" : 0,
        "nChunkSkips" : 0,
        "millis" : 23,
        "indexBounds" : {
            "target.user_id" : [
                [
                    ObjectId("512fbef8a989b28ddb5cdf93"),
                    ObjectId("512fbef8a989b28ddb5cdf93")
                ]
            ]
        }
    }
],
"n" : 3019,
"nscannedObjects" : 3317,
"nscanned" : 3317,
"nscannedObjectsAllPlans" : 3317,
"nscannedAllPlans" : 3317,
"millis" : 31,
"server" : "Michaels-MacBook-Pro.local:27017"

}

4

1 回答 1

1

我不明白你为什么需要这个 $and

{
  '$and': [{
      '$or': [
        {'object._id': ObjectId('512fbef8a989b28ddb5cdf93')},
        {'target._id': ObjectId('512fbef8a989b28ddb5cdf93')},
        {'target.user_id': ObjectId('512fbef8a989b28ddb5cdf93')}
      ]
    },{
      'actor._id': {'$ne': ObjectId('512fbef8a989b28ddb5cdf93')}
    }
  ]
}

在我看来,你可以做到

{
  'actor._id': {'$ne': ObjectId('512fbef8a989b28ddb5cdf93')},
   $or': [
        {'object._id': ObjectId('512fbef8a989b28ddb5cdf93')},
        {'target._id': ObjectId('512fbef8a989b28ddb5cdf93')},
        {'target.user_id': ObjectId('512fbef8a989b28ddb5cdf93')}
      ]
}

另一件事是,在您通过$ne查询搜索的字段上使用索引不是一个好主意。所以我宁愿从那个字段中删除索引。

于 2013-11-12T03:32:09.060 回答