2017-07-25 95 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