如何提高grails mongodb的数据库查找速度
以上是五月域 我已经在如何提高grails mongodb的数据库查找速度,mongodb,grails,Mongodb,Grails,以上是五月域 我已经在countryName上创建了一个索引,但当我查询它时,我认为它没有使用索引 例如在mongo shell中 import org.bson.types.ObjectId; class CountryInfo { ObjectId id long ipFrom long ipTo String countryCode String countryName String regionName String cityNam
countryName
上创建了一个索引,但当我查询它时,我认为它没有使用索引
例如在mongo shell中
import org.bson.types.ObjectId;
class CountryInfo {
ObjectId id
long ipFrom
long ipTo
String countryCode
String countryName
String regionName
String cityName
static constraints = {
countryName index:true
}
}
但在grails中,这需要太多的时间
我认为grails没有使用索引。有没有提高查找速度的方法?我想没有。这是优化器决定是否使用和使用哪个索引,而不是grails。您可以查看日志以验证从grails运行的查询是否使用了索引,我怀疑不是这样。速度慢的原因可能是您正在返回并且可能正在迭代57K个文档。您应该在查询中使用分页,这在服务器中被转换为
skip
,您可以在慢速查询中包含Grails代码的示例吗?mongo
shell的输出似乎使用了预期的索引。正如在前面的评论中所建议的,性能问题实际上可能与正在检索的文档数量有关(特别是在Grail中迭代57k对象时)。
db.CountryInfo.find({countryName:"CANADA"}).limit(57000).explain();
{
"cursor" : "BtreeCursor countryName_1",
"isMultiKey" : false,
"n" : 57000,
"nscannedObjects" : 57000,
"nscanned" : 57000,
"nscannedObjectsAllPlans" : 57000,
"nscannedAllPlans" : 57000,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 3,
"nChunkSkips" : 0,
"millis" : 2577,
"indexBounds" : {
"countryName" : [
[
"CANADA",
"CANADA"
]
]
},
"server" : "User"
}
db.CountryInfo.count()
1860189