mongodb慢查询$ near和其他条件

时间:2014-06-03 17:49:30

标签: mongodb indexing 2d geospatial

我有一个名为rooms的mongodb集合,它有一个字段location的2d索引。我这样问过:

db.rooms.find( { "location" : { "$near" : { "latitude" : 37.3356135, "longitude" : 127.12383030000001 } }, "status": "open", "updated" : { "$gt" : ISODate("2014-06-03T15:34:22.213Z") }}).explain()

结果:

{
    "cursor" : "GeoSearchCursor",
    "isMultiKey" : false,
    "n" : 7,
    "nscannedObjects" : 143247,
    "nscanned" : 143247,
    "nscannedObjectsAllPlans" : 143247,
    "nscannedAllPlans" : 143247,
    "scanAndOrder" : false,
    "indexOnly" : false,
    "nYields" : 0,
    "nChunkSkips" : 0,
    "millis" : 1457,
    "indexBounds" : {

    },
    "server" : "ip-10-162-39-56:27017",
    "filterSet" : false
}

有时需要超过2000毫秒。但是,如果我删除$gt字段的updated条件,则查询速度很快,约为5~30毫秒。

> db.rooms.find( { "location" : { "$near" : { "latitude" : 37.3356135, "longitude" : 127.12383030000001 } }, "status": "open"}).explain()
{
    "cursor" : "GeoSearchCursor",
    "isMultiKey" : false,
    "n" : 100,
    "nscannedObjects" : 1635,
    "nscanned" : 2400,
    "nscannedObjectsAllPlans" : 1635,
    "nscannedAllPlans" : 2400,
    "scanAndOrder" : false,
    "indexOnly" : false,
    "nYields" : 0,
    "nChunkSkips" : 0,
    "millis" : 22,
    "indexBounds" : {

    },
    "server" : "ip-10-162-39-56:27017",
    "filterSet" : false
}

我为{location:"2d", updated: -1}尝试了复合索引,但它没有用。如何更快地进行此查询?

0 个答案:

没有答案