从数据集中选择count(*),其中actors.lastName =“ Smith”和 actors.roles =“旅行者”
我在actors.lastName
上创建了复合索引,并且actors.roles
count(*)
非常慢。如何迫使mongo使用复合索引并提高性能?
我没有在此集合上创建其他索引。 MongoDB查询:
use test;
db.getCollection("dataset").aggregate(
[
{
"$match" : {
"actors.lastName" : "Smith",
"actors.roles" : "TRAVELER"
}
},
{
"$group" : {
"_id" : {
},
"COUNT(*)" : {
"$sum" : NumberInt(1)
}
}
},
{
"$project" : {
"COUNT(*)" : "$COUNT(*)",
"_id" : NumberInt(0)
}
}
],
{
"allowDiskUse" : true
}
);
如果我测试
select count(*) from dataset where actors.lastname="Smith"
count(*)快速,它使用在actors.lastname
上创建的索引。
mongodb版本4.0.9
count(*)非常慢。如何迫使mongo使用复合索引并提高性能?