我被这个简单的前缀查询卡住了。虽然Mongo文档中提到,通过使用前缀正则表达式格式(/^a/),您可以获得相当不错的性能,但是当我尝试对结果进行排序时,查询速度非常慢:
940毫秒
db.posts.find({hashtags: /^noticias/ }).limit(15).sort({rank : -1}).hint('hashtags_1_rank_-1').explain()
{
"cursor" : "BtreeCursor hashtags_1_rank_-1 multi",
"isMultiKey" : true,
"n" : 15,
"nscannedObjects" : 142691,
"nscanned" : 142692,
"nscannedObjectsAllPlans" : 142691,
"nscannedAllPlans" : 142692,
"scanAndOrder" : true,
"indexOnly" : false,
"nYields" : 1,
"nChunkSkips" : 0,
"millis" : 934,
"indexBounds" : {
"hashtags" : [
[
"noticias",
"noticiat"
],
[
/^noticias/,
/^noticias/
]
],
"rank" : [
[
{
"$maxElement" : 1
},
{
"$minElement" : 1
}
]
]
},
"server" : "XRTZ048.local:27017"
}
然而,同一个查询的未排序版本非常快:
0 毫秒
db.posts.find({hashtags: /^noticias/ }).limit(15).hint('hashtags_1_rank_-1').explain()
{
"cursor" : "BtreeCursor hashtags_1_rank_-1 multi",
"isMultiKey" : true,
"n" : 15,
"nscannedObjects" : 15,
"nscanned" : 15,
"nscannedObjectsAllPlans" : 15,
"nscannedAllPlans" : 15,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {
"hashtags" : [
[
"noticias",
"noticiat"
],
[
/^noticias/,
/^noticias/
]
],
"rank" : [
[
{
"$maxElement" : 1
},
{
"$minElement" : 1
}
]
]
},
"server" : "XRTZ048.local:27017"
如果我去掉正则表达式和排序,查询也会变得很快:
0毫秒
db.posts.find({hashtags: 'noticias' }).limit(15).sort({rank : -1}).hint('hashtags_1_rank_-1').explain()
{
"cursor" : "BtreeCursor hashtags_1_rank_-1",
"isMultiKey" : true,
"n" : 15,
"nscannedObjects" : 15,
"nscanned" : 15,
"nscannedObjectsAllPlans" : 15,
"nscannedAllPlans" : 15,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {
"hashtags" : [
[
"noticias",
"noticias"
]
],
"rank" : [
[
{
"$maxElement" : 1
},
{
"$minElement" : 1
}
]
]
},
"server" : "XRTZ048.local:27017"
似乎同时使用正则表达式和排序会使Mongo扫描大量记录。 但是,如果不使用正则表达式,仅进行排序,则只会扫描15个记录。 这里出了什么问题?