我正在建立一个聚合管道,该聚合管道具有以下阶段
[
{ $match: {field1: "ABC"} },
{
$project: {
date: true,
state: true,
}
},
{
$group: {
_id: {
formattedDate: { $dateToString: { format: "%Y-%m-%d", date: "$date" } },
status: "$Status"
},
count: { $sum: 1 }
}
},
{
$project: {
date: "$_id.formattedDate",
state: "$_id.state",
_id: false,
count: true
}
}
]
请注意,查询{field1:“ XYZ”}是可选的,因此对于给定的管道,查询可能存在或不存在。因此,我在汇总查询中添加了提示,以始终使用field1作为目标索引。
现在是实际问题: 该馆藏有超过一百万份文档,我在field1上有一个索引。以下是我观察到的结果
查询{field1:“ XYZ”}
nReturned: 12,
docsExamined: 12,
totalKeysExamined: 22,
totalDocsExamined: 12,
executionTimeMillisEstimate: 0,
executionTimeMillis: 1
查询{field1:“ ABC”}
nReturned: 100023,
docsExamined: 100023,
totalKeysExamined: 100023,
totalDocsExamined: 100023,
executionTimeMillisEstimate: 9,
executionTimeMillis: 368
无需查询{}
nReturned: 1378246,
docsExamined: 1378246,
totalKeysExamined: 1378246,
totalDocsExamined: 1378246,
executionTimeMillisEstimate: 313,
executionTimeMillis: 6350
在所有三种情况下,Mongo都在执行IXSCAN,但最后一条管道大约需要7秒钟。我该怎么做才能提高性能?
非常感谢任何建议。