【发布时间】:2019-06-10 15:00:28
【问题描述】:
我最近在研究聚合管道的性能优化。在决定要索引哪个时,我遇到了这个问题。我为 {'receiverId': 1,'type': 1 } 设置了一个复合索引。
我原来的管道是这样的。我正在考虑是否索引isRead
也是必要的。我之所以没有创建三字段复合索引是因为其他查询只使用了recieverId和type。
notifications.aggregate([{
$match: {
'recieverId': ObjectId(xxx),
'type': xxx,
'isRead': false
}
},
{
...
},
], (err, result) => {
return res.status(200).json(result);
});
所以,我在下面改了这个。想知道这样的管道在过滤recieverId 和type 后是否可以节省一些计算成本。 isRead, 第二个 $match 是否会从第一个 $match 的结果中过滤?
notifications.aggregate([{
$match: {
'recieverId': ObjectId(xxx),
'type': xxx,
}
},
{
$match: {
'isRead': false
}
},
{
...
},
], (err, result) => {
return res.status(200).json(result);
});
【问题讨论】:
标签: mongodb aggregation-framework query-optimization