我有一个带有200,000+文档的集合,因为我使用了几个where子句进行过滤。
这是我在“猫鼬”里的疑问:
const assets = await GpsLog
.where('gps_time')
.gte(first)
.lte(last)
.where('location')
.within()
.geometry(geoFence);
其中geofence
是一个GeoJSON多边形。
我正在运行的shell查询是:
db.gps_log.find({
'gps_time': {
$gte: first,
$lte: last
},
'location': {
'$geoIntersects': {
'$geometry': {
'type': 'Polygon',
'coordinates': // polygon
}
}
}
})
Mongoose查询在5-11秒内完成,而shell查询则在0.5秒内完成。
我将shell查询翻译成在Mongoose中执行如下:
const res = await GpsLog.find({
'gps_time': {
$gte: 1539648000,
$lte: 1539820800
},
'location': {
'$geoIntersects': {
'$geometry': geoFence
}
}
}).lean().exec();
但它仍然需要4+秒才能执行。
我是不是漏掉了速度差的东西?
发布于 2018-10-18 10:39:09
当您执行查询时,Mongo将返回一个游标并读取(最多)20个结果。在此之后,您需要通过各种方法之一耗尽游标(读取所有结果)。
因此,基本上您要测试的是,在Mongo shell中返回一个光标并读取其中的20个结果需要多长时间。在你的例子中,那需要0.5秒。
但是,默认情况下,Mongoose会读取所有结果。因此,如果您想要对猫鼬和shell进行公平的比较,您也应该读取shell中的所有结果,例如在游标上使用toArray
方法:
db.gps_log.find({ ... }).toArray()
或者,你可以让猫鼬去返回光标,而不是立即阅读所有的结果:
const cursor = GpsLog.find({ ... }).lean().cursor();
cursor.on('data', doc => { ... }).on('close', () => { ... });
https://stackoverflow.com/questions/52871428
复制相似问题