【发布时间】:2018-04-12 07:19:35
【问题描述】:
我想创建一个应用程序,例如(例如)Tinder。所以我必须能够列出我周围所有符合某些标准(年龄、宗教等)的用户。
实际上所有的用户都存储在 mongoDB 中,但是 mongoDB 看起来做这样的查询很糟糕,例如我做的
db.runCommand( { dropDatabase: 1 } )
db.createCollection("users");
db.users.createIndex( { "locs.loc" : "2dsphere" } )
function randInt(n) { return parseInt(Math.random()*n); }
function randFloat(n) { return Math.random()*n; }
for(var j=0; j<10; j++) {
print("Building op "+j);
var bulkop=db.users.initializeOrderedBulkOp() ;
for (var i = 0; i < 1000000; ++i) {
bulkop.insert(
{
locs: [
{
loc : {
type: "Point",
coordinates: [ randFloat(180), randFloat(90) ]
}
},
{
loc : {
type: "Point",
coordinates: [ randFloat(180), randFloat(90) ]
}
}
]
}
)
};
print("Executing op "+j);
bulkop.execute();
}
然后
db.runCommand(
{
geoNear: "users",
near: { type: "Point", coordinates: [ 73.9667, 40.78 ] },
spherical: true,
query: { category: "xyz" }
}
)
我花了 4 分钟返回
"waitedMS" : NumberLong(0),
"results" : [ ],
"stats" : {
"nscanned" : 10018218,
"objectsLoaded" : 15000000,
"maxDistance" : 0,
"time" : 219873
},
"ok" : 1
所以我绝对必须使用其他东西但是什么?我很确定我需要一个像sphinx 这样的内存索引(所以只需将所有记录存储在内存中,并在每次查询时对所有行进行全面扫描)。实际上它工作得很好,但狮身人面像索引是面向索引文本文档,我不确定它是否适合我的需要。
【问题讨论】:
-
嗨洛基;请问您是否调查过为什么此查询运行缓慢?例如,其他类型的地理空间查询也运行缓慢;或者您是否考虑过在 category 和 loc 字段上创建 compound index?
标签: database mongodb indexing database-design sphinx