mongodb - MongoDB:$match 与 $or 聚合优化
问题描述
我遇到了如何优化这个 $match 的问题。目前我有这个代码
{ $match: {
$or: [
{ 'card': { $eq: 'card1'},
$and: [
{ 'createdAt': { $gte: three_months }},
{ 'createdAt': { $lte: createdAt }}
]
},
{ 'client': { $eq: 'client1'},
$and: [
{ 'client': { $ne: null }},
{ 'createdAt': { $gte: three_months }},
{ 'createdAt': { $lte: createdAt }}
]
},
{ 'destination_card': { $eq: 'destination_card1'},
$and: [
{ 'createdAt': { $gte: three_months }},
{ 'createdAt': { $lte: createdAt }}
]
},
]
}
{ $limit: 300},
{ $sort: { createdAt: -1 } },
但有时计算错误或执行时间过长。请你能给出一些想法如何以某种方式优化它吗?提前谢谢了!
解决方案
如果您使用的是更新版本的 MongoDB(我认为是 3.6+),查询计划器可以使用多个索引来处理$or
. 每个分支在 上使用相同的标准createdAt
,因此您可以将它们收集在一起以仅列出一次,例如:
{$match: {
{'createdAt':{$gte: three_months, $lte: createdAt}},
{$or:[
{'card': 'card1'},
{'client': 'client1'},
{'destination_card': 'destination_card1
]}
}}
如果您创建 3 个索引,每个索引都有一个完全匹配字段和 createdAt,这样{card:1, createdAt:1}
会改进选择并减少检查的文档数量,这可能有助于缩短执行时间。
注意 using$or
通常会阻止查询执行器使用索引来执行排序,每次运行该查询时都需要进行内存排序,这会增加执行该查询所需的 RAM,这可能会在服务器运行时减慢执行速度忙碌的。
推荐阅读
- typescript - 带有拖放/调整大小元素的赛普拉斯问题
- java - 在选择下拉蚂蚁设计中更改检查图标的颜色
- angular - 如何在角度 10 中使用 FromControl 初始化值 mat-selection-list
- javascript - 使用 nodemailer 页面发送邮件后开始加载而不实际更改元素/页面
- mysql - 根据 COUNT 参数查询不使用索引
- mongodb - MongoDB聚合使用字段参考
- javascript - 在Javascript中分配2个数组键值?
- php - 由于 rethinkdb 问题,尝试合并 2 个数组
- visual-studio - 未触发 Visual Studio / MSBuild 构建自定义
- php - Monolog MongoDBHandler 在 Laravel 8 中出现 BindingResolutionException 错误