我坚持使用这个简单的前缀查询。尽管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 个。这里有什么问题?