优化mongo查询以获得更好的响应

时间:2015-02-13 07:29:37

标签: mongodb aggregation-framework

我正在尝试优化mongodb查询以获得更好的响应

db.myReports.find({
"CheckInDate": {
    "$gte" : ISODate("2015-01-12T00:00:00Z"),
    "$lte" : ISODate("2015-03-31T00:00:00Z")
},
"SubscriberPropertyId":  NumberLong(47984),
"ChannelId": {
    "$in": [701, 8275]
},
"PropertyId": {
    "$in": [47984, 3159, 5148, 61436, 66251, 70108]
},
"LengthOfStay": 1
},     {
    "CheckInDate": 1,
   "SubscriberPropertyId": 1,
    "ChannelId": 1,
    "PropertyId": 1
});

目前,只需3分钟即可查找300万条记录中的数据。

收集的一份文件

{
  "_id" : ObjectId("54dba46c320caf5a08473074"),
    "OptimisationId" : NumberLong(1),
    "ScheduleLogId" : NumberLong(3),
      "ReportId" : NumberLong(4113235),
   "SubscriberPropertyId" : NumberLong(10038),
   "PropertyId" : NumberLong(18166),
   "ChannelId" : 701,
  "CheckInDate" : ISODate("2014-09-30T18:30:00Z"),
  "LengthOfStay" : 1,
 "OccupancyIndex" : 1.0,
 "CreatedDate" : ISODate("2014-09-11T06:31:08Z"),
  "ModifiedDate" : ISODate("2014-09-11T06:31:08Z"),

 }

创建的INDEX是:

db.myReports.getIndexes();
[
        {
            "v" : 1,
            "key" : {
                    "_id" : 1
            },
            "name" : "_id_",
            "ns" : "db.myReports"
    },
    {
            "v" : 1,
            "key" : {
                    "CheckInDate" : 1,
                    "SubscriberPropertyId" : 1,
                    "ReportId" : 1,
                    "ChannelId" : 1,
                    "PropertyId" : 1
            },
            "name" :      
 "CheckInDate_1_SubscriberPropertyId_1_ReportId_1_Channe

 lId_1_PropertyId_1",
            "ns" : "db.myReports"
    },
    {
            "v" : 1,
            "key" : {
                    "CheckInDate" : 1
            },
            "name" : "CheckInDate_1",
            "ns" : "db.myReports"
    }
]

我已经在可能的实体上创建了索引

1 个答案:

答案 0 :(得分:1)

首先放置等式查询,然后放置范围查询:

db.myReports.find({
  "SubscriberPropertyId":  NumberLong(47984),
  "ChannelId": {
    "$in": [701, 8275]
  },
  "PropertyId": {
    "$in": [47984, 3159, 5148, 61436, 66251, 70108]
  },
  "CheckInDate": {
    "$gte" : ISODate("2015-01-12T00:00:00Z"),
    "$lte" : ISODate("2015-03-31T00:00:00Z")
  },
  "LengthOfStay": 1 // low selectivity, move to the end
}, {
  "CheckInDate": 1,
  "SubscriberPropertyId": 1,
  "ChannelId": 1,
  "PropertyId": 1
});

确保索引适合,即制作索引SubscriberPropertyIdChannelIdPropertyIdCheckInDateLengthOfStay可能选择性太低而无法在索引中显示,取决于您的数据。

这应该会显着减少nscanned,但获得300k的结果会花费时间(实际上是读它们,我的意思)