首页 > 解决方案 > 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 } },

但有时计算错误或执行时间过长。请你能给出一些想法如何以某种方式优化它吗?提前谢谢了!

标签: mongodboptimizationmatchaggregation

解决方案


如果您使用的是更新版本的 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,这可能会在服务器运行时减慢执行速度忙碌的。


推荐阅读