我收藏了大约200K文件。三个属性是verifed
(布尔值),location
(地理坐标)和visited
(日期)。这是一个例子:
{
"verified": true,
"location": [ -79.4, 43.6 ],
"visited": ISODate("2013-08-30T00:00:00Z")
... other properties
}
我有以下索引:
db.coll.ensureIndex({
verified: 1,
location: '2dsphere',
visited: -1
});
此索引具有以下大小:
> db.coll.stats()
{
"ns" : "mydb.coll",
"count" : 181511,
"size" : 2407550688,
"avgObjSize" : 13263,
"storageSize" : 2833022944,
"numExtents" : 22,
"nindexes" : 5,
"lastExtentSize" : 536600560,
"paddingFactor" : 1,
"systemFlags" : 0,
"userFlags" : 1,
"totalIndexSize" : 45507616,
"indexSizes" : {
"_id_" : 5347104,
"verified_1_location_2dsphere_visited_-1" : 11658976,
"TextIndex" : 28501536,
},
"ok" : 1
}
现在,当我运行以下查询时,需要50s(!)才能执行:
db.coll.find({
'verified': true,
location: {
$near: {
$geometry: {
type: 'Point',
coordinates: [-79.5, 43.6]
},
$maxDistance: 2000
}
}
}).sort({
visited: -1
});
同时运行mongostat
表明问题是页面错误:
14:36:49 me@server ~> mongostat -n 200
connected to: 127.0.0.1
... lots of rows
insert query update delete getmore command flushes mapped vsize res faults locked db idx miss % qr|qw ar|aw netIn netOut conn time
*0 *0 *0 *0 0 1|0 0 3.73g 7.69g 135m 83 mydb:0.0% 0 0|0 1|0 62b 3k 12 14:41:20
*0 *0 *0 *0 0 1|0 0 3.73g 7.69g 136m 73 mydb:0.0% 0 0|0 1|0 62b 3k 12 14:41:21
*0 *0 *0 *0 0 1|0 0 3.73g 7.69g 136m 60 mydb:0.0% 0 0|0 1|0 62b 3k 12 14:41:22
1 *0 *0 *0 0 2|0 0 3.73g 7.69g 137m 38 mydb:0.0% 0 0|0 0|0 742b 35k 17 14:41:23
*0 *0 *0 *0 0 1|0 0 3.73g 7.69g 137m 0 mydb:0.0% 0 0|0 0|0 62b 3k 17 14:41:24
*0 *0 *0 *0 0 1|0 0 3.73g 7.69g 132m 0 .:0.3% 0 0|0
我非常感谢任何帮助解释这些统计数据。我的服务器功能不是很强大(1GB RAM),但考虑到索引只有45M,我真的不明白为什么整个索引都无法适应内存。