使用“$ in”的MongoDB“count”变得太慢了

时间:2017-06-14 10:36:31

标签: mongodb mongodb-query

我有一个大集合(大约3000万条记录),我以不同的方式查询它,其中一些非常好用:

// Query #1
db.getCollection('my_collection').find({ "parent_uuid": "77796c50-7dc3-0134-21f1-0a81e8b09a82" }).count()
// => 415262 (in less than one second)

// Query #2
db.getCollection('my_collection').find({ "parent_uuid": "35529cc0-330a-0135-3ba3-0a901406a434" }).count()
// => 1 (in less than one second)

然后我请求它并且它得到堆栈

// Query #3
db.getCollection('my_collection').find({
  "parent_uuid": { "$in": ["77796c50-7dc3-0134-21f1-0a81e8b09a82", "35529cc0-330a-0135-3ba3-0a901406a434"] }
}).count()

索引

在其他索引中我有这个:

{
    "v" : 1,
    "key" : {
        "parent_uuid" : 1
    },
    "name" : "parent_uuid_1",
    "ns" : "my_database.my_collection"
}

我需要使用$in选项和几个UUID,我做错了什么?

更新1:解释

1 个答案:

答案 0 :(得分:2)

My hacky solution (and it's still not as fast as it should be) is to use aggregate and group, then sum the results:

db.getCollection('my_collection').aggregate([
  {
    "$match": {
      "parent_uuid": {
         $in: ["77796c50-7dc3-0134-21f1-0a81e8b09a82", "35529cc0-330a-0135-3ba3-0a901406a434"] 
        }
      }
  },
  {
    "$group": {
      "_id" : "$parent_uuid",
      "initial_count": { "$sum": 1 }
    }
  },
  {
    "$group": {
      "_id" : null,
      "count": { "$sum": "$initial_count" }
    }
  }
}])

will result in:

// less than a second
/* 1 */
{
  "_id" : null,
  "count" : 416175.0
}