2017-07-25 47 views
0

我有以下收集geo用以下指標:MongoDB的查詢超時

> db.geo.getIndexes(); 
[ 
    { 
     "v" : 1, 
     "key" : { 
      "_id" : 1 
     }, 
     "name" : "_id_", 
     "ns" : "geospatial.geo" 
    }, 
    { 
     "v" : 1, 
     "key" : { 
      "checkin_id" : -1 
     }, 
     "name" : "checkin_id_-1", 
     "ns" : "geospatial.geo" 
    }, 
    { 
     "v" : 1, 
     "key" : { 
      "loc" : "2dsphere", 
      "created_at" : -1, 
      "user_id" : 1 
     }, 
     "name" : "loc_2dsphere_created_at_-1_user_id_1", 
     "ns" : "geospatial.geo", 
     "2dsphereIndexVersion" : 2 
    }, 
    { 
     "v" : 1, 
     "key" : { 
      "created_at" : -1 
     }, 
     "name" : "created_at_-1", 
     "ns" : "geospatial.geo", 
     "expireAfterSeconds" : 31557600 
    }, 
    { 
     "v" : 1, 
     "key" : { 
      "loc" : "2dsphere", 
      "created_at" : 1 
     }, 
     "name" : "loc_2dsphere_created_at_1", 
     "ns" : "geospatial.geo", 
     "background" : true, 
     "2dsphereIndexVersion" : 2 
    } 
] 

但運行此查詢:

db.geo.find(
    { 
    loc: 
     { $near : 
      { 
      $geometry: { type: "Point", coordinates: [ -73.9667, 40.78 ] }, 
      $minDistance: 1000, 
      $maxDistance: 5000 
      } 
     }, 
     created_at: { $gte : new ISODate("2017-06-23T00:00:00Z") } 
    } 
) 

OR

db.geo.distinct(
    "user_id", 
    { 
    loc: 
     { $near : 
      { 
      $geometry: { type: "Point", coordinates: [ -73.9667, 40.78 ] }, 
      $maxDistance: 16093.4 
      } 
     }, 
    created_at: { $gte : new ISODate("2017-06-23T00:00:00Z") } 
} 

).length 

它只是超時,並從不處理。任何想法,我在這裏失蹤與索引?這個系列中有74m +文件。

樣本文檔:

{ 
    "_id" : ObjectId("59740de9e5bfa822388b4567"), 
    "checkin_id" : XXXXX, 
    "user_id" : XXXX, 
    "created_at" : ISODate("2017-07-23T02:44:36.000Z"), 
    "loc" : { 
     "type" : "Point", 
     "coordinates" : [ 
      -88.2165, 
      42.3718 
     ] 
    } 
} 

UPDATE: 這裏是executionStats結果:

{ 
    "cursor" : "S2NearCursor", 
    "isMultiKey" : false, 
    "n" : 57740, 
    "nscannedObjects" : 77737, 
    "nscanned" : 200823, 
    "nscannedObjectsAllPlans" : 123823, 
    "nscannedAllPlans" : 300660, 
    "scanAndOrder" : false, 
    "indexOnly" : false, 
    "nYields" : 0, 
    "nChunkSkips" : 0, 
    "millis" : 1514, 
    "indexBounds" : { 

    }, 
    "allPlans" : [ 
     { 
      "cursor" : "S2NearCursor", 
      "isMultiKey" : false, 
      "n" : 57740, 
      "nscannedObjects" : 77737, 
      "nscanned" : 200823, 
      "scanAndOrder" : false, 
      "indexOnly" : false, 
      "nChunkSkips" : 0, 
      "indexBounds" : { 

      } 
     }, 
     { 
      "cursor" : "S2NearCursor", 
      "isMultiKey" : false, 
      "n" : 101, 
      "nscannedObjects" : 46086, 
      "nscanned" : 99837, 
      "scanAndOrder" : false, 
      "indexOnly" : false, 
      "nChunkSkips" : 0, 
      "indexBounds" : { 

      } 
     } 
    ], 
    "server" : "ut-geobrain-01:27017", 
    "filterSet" : false, 
    "stats" : { 
     "type" : "GEO_NEAR_2DSPHERE", 
     "works" : 258567, 
     "yields" : 0, 
     "unyields" : 0, 
     "invalidates" : 0, 
     "advanced" : 57740, 
     "needTime" : 0, 
     "needFetch" : 0, 
     "isEOF" : 1, 
     "children" : [ ] 
    } 
} 
+0

您是否嘗試運行您用'.explain(「executionStats」)'查詢來嘗試和數字o發生了什麼事? – Kdawg

+0

**多個**'「2dsphere」'索引不是一個好主意。例如,集合'$ geoNear'不支持在集合上存在多於**一個**地理空間索引,因爲實際上沒有辦法指定哪個字段在該操作中用於地理空間查詢。所以你應該重新考慮多個索引,並提出一個**,它適合你最常見的需求。 –

+0

我在這裏對你在這裏和你對現有答案的評論中顯示的結果有點困惑,因爲這兩個查詢似乎在兩秒鐘內完成(在你的問題中爲1514毫秒,.1483毫秒在你的評論中),除非我沒有正確記得如何解釋這些結果。你只是從shell運行這些命令而沒有收回任何東西?另外,你運行的是什麼版本的MongoDB? – Kdawg

回答

0

設置查詢的處理時間,通過maxTimeMS

db.geo.find(...).maxTimeMS(1000 * 1000 * 1000); 
+0

是的 - 但這不好,查詢應該通過索引更快? – gregavola

+0

'''{ \t 「光標」: 「S2NearCursor」, \t 「isMultiKey」:假, \t 「N」:57643, \t 「nscannedObjects」:77609, \t 「nscanned」:200493, \t「 nscannedObjectsAllPlans」:123606, \t 「nscannedAllPlans」:300166, \t 「scanAndOrder」:假, \t 「indexOnly」:假, \t 「nYields」:0, \t 「nChunkSkips」:0, \t 「米利斯」:1483, \t 「indexBounds」:{ \t}, \t 「服務器」: 「UT-geobrain-01:27017」, \t 「filterSet」:假 }''' – gregavola

+0

它掃描2004年的743萬。這些點是否包裝在一起?你可以嘗試不同的查詢嗎? – bsd