0

因此,如果我有一个执行以下操作的查询(在伪代码中)

find(a 附近的 x, b > y).sort(c)

其中a是地理列,b是long类型,c也是long类型

(a:2d, b:1, c:1) 上的复合索引会起作用并建议吗?

4

1 回答 1

2

地理空间查询有自己的索引类别(正如您提到的),并且地理哈希大大提高了第一个键查找的索引性能 - 如果您可以正确设置它,它比范围更好。无论如何,我认为您的策略会奏效:关键是将 $maxDistance 设置为相当小的值。

我插入了 1000 万条随机地理记录以匹配您的描述,如下所示:

{ "_id" : ObjectId("4f28e1cffc90631d239f8b5a"), "a" : [ 46, 47 ], "b" : ISODate("2012-02-01T06:53:25.543Z"), "c" : 19 }
{ "_id" : ObjectId("4f28e1bdfc90631d239c4272"), "a" : [ 54, 48 ], "b" : ISODate("2012-02-01T06:53:32.699Z"), "c" : 20 }
{ "_id" : ObjectId("4f28e206fc90631d23aac59d"), "a" : [ 46, 52 ], "b" : ISODate("2012-02-01T06:55:14.103Z"), "c" : 22 }
{ "_id" : ObjectId("4f28e1a7fc90631d23995700"), "a" : [ 54, 52 ], "b" : ISODate("2012-02-01T06:52:33.312Z"), "c" : 27 }
{ "_id" : ObjectId("4f28e1d7fc90631d23a0e9e7"), "a" : [ 52, 46 ], "b" : ISODate("2012-02-01T06:53:11.315Z"), "c" : 31 }

当 maxDistance 低于 10 时,性能非常好。

db.test13.find({a:{$near:[50,50], $maxDistance:4}, b:{$gt:d}}).sort({c:1}).explain();
{
"cursor" : "GeoSearchCursor",
"nscanned" : 100,
"nscannedObjects" : 100,
"n" : 100,
"scanAndOrder" : true,
"millis" : 25,
"nYields" : 0,
"nChunkSkips" : 0,
"isMultiKey" : false,
"indexOnly" : false,
"indexBounds" : {

}
}

If you leave out maxDistance it starts to suffer quite a bit. Some of the queries took up to 60 seconds to run. The secondary range parameter doesn't seem to help much, even if the range is fairly narrow--it seems to be all about the maxDistance.

I recommend you play around with it to get a feel for how the geospatial index performs. Here is my test insert loop. You can try limiting the bits as well for less resolution

function getRandomTime() {
   return new Date(new Date() - Math.floor(Math.random()*1000000));
}

function getRandomGeo() {
   return [Math.floor(Math.random()*360-180),Math.floor(Math.random()*360-180)];
}

function initialInsert() {
   for(var i = 0; i < 10000000; i++) {
      db.test13.save({
         a:getRandomGeo(),
         b:getRandomTime(),
         c:Math.floor(Math.random()*1000)
      });
   }
}
于 2012-02-01T05:42:17.840 回答