MongoDB 2.6.4查询不是最佳的 - 需要过多的时间来返回结果

时间:2014-08-18 21:42:45

标签: mongodb mongodb-query mongodb-php

我们在亚马逊实例上使用MongoDB 2.6.4。我们的查询花费了过多的时间来返回结果。注意:我们正在使用10gen php驱动程序,但已提取原始查询以便对查询本身进行故障排除。

@db.instant_leads_properties.aggregate(

[
   {
      "$match":{
         "$or":[
            {
               "address":{
                  "$regex":"Louisiana",
                  "$options":"i" 
               }
            },
            {
               "notes":{
                  "$regex":"Louisiana",
                  "$options":"i" 
               }
            },
            {
               "city":"Louisiana" 
            },
            {
               "state":"louisiana" 
            },
            {
               "zip":"71230" 
            }
         ],
         "$and":[
            {
               "featured_expires":{
                  "$gt":{
                     "sec":1407957033,
                     "usec":0
                  }
               }
            }
         ]
      }
   },
   {
      "$sort":{
         "listing_date":-1
      }
   },
   {
      "$skip":0
   },
   {
      "$limit":10
   }
]
);

如果我们直接在mongodb服务器上运行查询,则需要20秒才能返回任何内容。

似乎导致问题的具体部分如下。如果删除了这个,那么我们会得到一个包含记录的结果。

"$and":[
   {
      "featured_expires":{
         "$gt":{
            "sec":1407957033,
            "usec":0
         }
      }
   }
]

我创建了索引,复合索引,并使用10gen优化的mongodb AMI配置了amazon ec2实例。这些尝试都没有产生任何影响。我们的db统计数据如下:

    {
      "serverUsed": "REMOVED",
      "db": "REMOVED",
      "collections": 10,
      "objects": 4719489,
      "avgObjSize": 1222.960180646676,
      "dataSize": 5.77174712E9,
      "storageSize": 6.340694016E9,
      "numExtents": 49,
      "indexes": 39,
      "indexSize": 6.506763312E9,
      "fileSize": 1.4958985216E10,
      "nsSizeMB": 16,
      "dataFileVersion": {
        "major": 4,
        "minor": 6
      },
      "extentFreeList": {
        "num": 0,
        "totalSize": 0
      },
      "ok": 1.0
    }

The stats for the collection being used (instant_leads_properies) are as follows:

{
  "serverUsed": "REMOVED",
  "ns": "staging.instant_leads_properties",
  "count": 2204568,
  "size": 5.447771456E9,
  "avgObjSize": 2471,
  "storageSize": 5.877628928E9,
  "numExtents": 21,
  "nindexes": 31,
  "lastExtentSize": 1532088320,
  "paddingFactor": 1.0040000000383855,
  "systemFlags": 0,
  "userFlags": 1,
  "totalIndexSize": 6.4251096E9,
  "indexSizes": {
    "_id_": 71564528,
    "listing_key_1": 120252608,
    "state_1_property_type_1_city_1_listing_date_1": 199379936,
    "state_1_property_type_1_zip_1_listing_date_1": 184082640,
    "state_1_property_type_1_county_1_listing_date_1": 192258640,
    "asking_price_1": 103418224,
    "living_area_1": 95144112,
    "living_area_1_asking_price_1": 117047616,
    "listing_date_1_living_area_1_asking_price_1": 137340448,
    "zip_1_living_area_1_asking_price_1": 142286928,
    "longitude_1_latitude_1": 98978656,
    "rem_id_1_created_at_-1": 57837024,
    "rem_id_1_listing_date_-1": 54501216,
    "point_2dsphere": 37740416,
    "property_type_1": 66650752,
    "address_1": 78890224,
    "notes_1": 861096320,
    "city_1": 60543280,
    "state_1": 59096128,
    "zip_1": 51582384,
    "property_type_1_featured_expired_1": 69046320,
    "property_type_1_featured_expires_1": 69054496,
    "city_1_state_1_zip_1": 100303168,
    "address_1_city_1_state_1_zip_1": 144159232,
    "address_1_city_1_state_1_zip_1_featured_expires_1": 146423984,
    "latitude_1": 112501760,
    "notes_text": 2.717898624E9,
    "featured_expires_1": 37724064,
    "sec_1": 37724064,
    "usec_1": 37724064,
    "address_1_city_1_state_1_zip_1_featured_expires_1_listing_date_-1": 162857744
  },
  "ok": 1.0
}

以及此集合的索引列表:

{ "v" : 1 , "key" : { "_id" : 1} , "ns" : "staging.instant_leads_properties" , "name" : "_id_"}
{ "v" : 1 , "key" : { "listing_key" : 1} , "unique" : true , "ns" : "staging.instant_leads_properties" , "background" : true , "name" : "listing_key_1"}
{ "v" : 1 , "key" : { "state" : 1 , "property_type" : 1 , "city" : 1 , "listing_date" : 1} , "ns" : "staging.instant_leads_properties" , "background" : true , "name" : "state_1_property_type_1_city_1_listing_date_1"}
{ "v" : 1 , "key" : { "state" : 1 , "property_type" : 1 , "zip" : 1 , "listing_date" : 1} , "ns" : "staging.instant_leads_properties" , "background" : true , "name" : "state_1_property_type_1_zip_1_listing_date_1"}
{ "v" : 1 , "key" : { "state" : 1 , "property_type" : 1 , "county" : 1 , "listing_date" : 1} , "ns" : "staging.instant_leads_properties" , "background" : true , "name" : "state_1_property_type_1_county_1_listing_date_1"}
{ "v" : 1 , "key" : { "asking_price" : 1} , "ns" : "staging.instant_leads_properties" , "name" : "asking_price_1" , "background" : true}
{ "v" : 1 , "key" : { "living_area" : 1} , "ns" : "staging.instant_leads_properties" , "name" : "living_area_1" , "background" : true}
{ "v" : 1 , "key" : { "living_area" : 1 , "asking_price" : 1} , "ns" : "staging.instant_leads_properties" , "name" : "living_area_1_asking_price_1" , "background" : true}
{ "v" : 1 , "key" : { "listing_date" : 1 , "living_area" : 1 , "asking_price" : 1} , "ns" : "staging.instant_leads_properties" , "name" : "listing_date_1_living_area_1_asking_price_1" , "background" : true}
{ "v" : 1 , "key" : { "zip" : 1 , "living_area" : 1 , "asking_price" : 1} , "ns" : "staging.instant_leads_properties" , "name" : "zip_1_living_area_1_asking_price_1" , "background" : true}
{ "v" : 1 , "key" : { "longitude" : 1.0 , "latitude" : 1.0} , "ns" : "staging.instant_leads_properties" , "name" : "longitude_1_latitude_1"}
{ "v" : 1 , "key" : { "rem_id" : 1.0 , "created_at" : -1.0} , "ns" : "staging.instant_leads_properties" , "name" : "rem_id_1_created_at_-1"}
{ "v" : 1 , "key" : { "rem_id" : 1.0 , "listing_date" : -1.0} , "ns" : "staging.instant_leads_properties" , "name" : "rem_id_1_listing_date_-1"}
{ "v" : 1 , "key" : { "point" : "2dsphere"} , "ns" : "staging.instant_leads_properties" , "name" : "point_2dsphere"}
{ "v" : 1 , "key" : { "property_type" : 1.0} , "ns" : "staging.instant_leads_properties" , "name" : "property_type_1"}
{ "v" : 1 , "key" : { "address" : 1.0} , "ns" : "staging.instant_leads_properties" , "name" : "address_1"}
{ "v" : 1 , "key" : { "notes" : 1.0} , "ns" : "staging.instant_leads_properties" , "name" : "notes_1"}
{ "v" : 1 , "key" : { "city" : 1.0} , "ns" : "staging.instant_leads_properties" , "name" : "city_1"}
{ "v" : 1 , "key" : { "state" : 1.0} , "ns" : "staging.instant_leads_properties" , "name" : "state_1"}
{ "v" : 1 , "key" : { "zip" : 1.0} , "ns" : "staging.instant_leads_properties" , "name" : "zip_1"}
{ "v" : 1 , "key" : { "property_type" : 1.0 , "featured_expired" : 1.0} , "ns" : "staging.instant_leads_properties" , "name" : "property_type_1_featured_expired_1"}
{ "v" : 1 , "key" : { "property_type" : 1.0 , "featured_expires" : 1.0} , "ns" : "staging.instant_leads_properties" , "name" : "property_type_1_featured_expires_1"}
{ "v" : 1 , "key" : { "city" : 1.0 , "state" : 1.0 , "zip" : 1.0} , "name" : "city_1_state_1_zip_1" , "ns" : "staging.instant_leads_properties"}
{ "v" : 1 , "key" : { "address" : 1.0 , "city" : 1.0 , "state" : 1.0 , "zip" : 1.0} , "name" : "address_1_city_1_state_1_zip_1" , "ns" : "staging.instant_leads_properties"}
{ "v" : 1 , "key" : { "address" : 1.0 , "city" : 1.0 , "state" : 1.0 , "zip" : 1.0 , "featured_expires" : 1.0} , "name" : "address_1_city_1_state_1_zip_1_featured_expires_1" , "ns" : "staging.instant_leads_properties"}
{ "v" : 1 , "key" : { "latitude" : 1.0} , "name" : "latitude_1" , "ns" : "staging.instant_leads_properties" , "background" : true}
{ "v" : 1 , "key" : { "_fts" : "text" , "_ftsx" : 1} , "name" : "notes_text" , "ns" : "staging.instant_leads_properties" , "weights" : { "notes" : 1} , "default_language" : "english" , "language_override" : "language" , "textIndexVersion" : 2}
{ "v" : 1 , "key" : { "featured_expires" : 1.0} , "name" : "featured_expires_1" , "ns" : "staging.instant_leads_properties"}
{ "v" : 1 , "key" : { "sec" : 1.0} , "name" : "sec_1" , "ns" : "staging.instant_leads_properties"}
{ "v" : 1 , "key" : { "usec" : 1.0} , "name" : "usec_1" , "ns" : "staging.instant_leads_properties"}
{ "v" : 1 , "key" : { "address" : 1.0 , "city" : 1.0 , "state" : 1.0 , "zip" : 1.0 , "featured_expires" : 1.0 , "listing_date" : -1.0} , "name" : "address_1_city_1_state_1_zip_1_featured_expires_1_listing_date_-1" , "ns" : "staging.instant_leads_properties"}

请告诉我其他信息可以提供哪些信息。

感谢 杰森

1 个答案:

答案 0 :(得分:0)

我的建议是在addressnotescitystate上创建文字索引:

db.instant_lead_properties.ensureIndex({address:"text",notes:"text",city:"text", state:"text"})

并按如下方式调整聚合的匹配部分:

[...]
 $match:
 {
   $text: { $search: "Louisiana" },
   "featured_expires": { "$gt": { "sec":1407957033,"usec":0 } }
 }
[...]

通过这种方式,您阻止了$or$and的链接,根据我的经验,这会对性能产生影响。

加权结果等整洁的功能也随之而来。

此外,这两个字段的复合索引可能有所帮助,而不是为secusec提供两个单独的索引。

如果没有详细了解MMS监控并且不知道服务器的确切配置,我就可以从这里看到更多信息。