MongoDb 聚合查询性能下降问题

时间:2021-07-13 04:00:24

标签: mongodb indexing nosql aggregation-framework query-optimization

db.coll.aggregate([{
        $match: {
            $expr: {
                $eq: [{
                        $toString: {
                            "format": "%d/%m/%y",
                            "date": "$valueDate"
                        }
                    },
                    "02/06/2021"
                ]
            }
        }
    },
    {
        $lookup: {
            from: 'coll2',
            let: {
                'account': '$accountData',
                'cur': '$currency',
                'valueDate': '$valuDate',
                'transref': '$transrefId',
                'amount': '$amount'
            },
            'pipeline': [{
                $match: {
                    $expr: {
                        '$and': [{
                                $eq: ['$acc', '$$account']
                            },
                            {
                                $eq: ['$currency', '$$cur']
                            },
                            {
                                $eq: ['$valueDate', '$$valueDate']
                            },
                            {
                                $eq: ['$transref', '$$transref']
                            },
                            {
                                $eq: ['$amount', '$$amount']
                            },
                        ]
                    }
                }
            }],
            as: 'derived'
        }
    },
    {
        $match: {
            "derivied.0": {
                $exists: True
            }
        }
    },
    {
        $project: {
            BRK_DESCRIPTION: "Partial Match",
            BRK_IND: "P",
            BRK_ID: "_id",
            BRK_CURR: "$curreny",
            BRK_INT_ACC: {
                $arrayElemAt: ["derived.acc", 0]
            },
            BRK_INT_Valuedate: {
                $arrayElemAt: ["derived.valuedate", 0]
            },
        }
    },
    {
        $out: 'temp_coll3'
    }

])

我正在处理 Mongodb 聚合查询,它的性能很慢。我需要对我的数据产生一些见解,因为我在相同的序列中使用了 $match、$lookup、$unwind、$match、$project、$out 等操作。一天之内,这个查询必须处理大约 40 万条或更多的记录。这些集合包含超过 5000 万条记录。但是当涉及到最终的 $out 操作时,它的性能非常慢。

索引创建:

我有 5 个字段 - $accout、$amount、$currency、$date、$referenceId。

逻辑 - 这 4 个字段应该与另一个集合中的数据完全匹配,并且 $referenceId 不应该匹配。所以我在 ($account, $amount, $currency, $date) 上创建了一个复合索引。创建索引时序列是否重要。这是我查询的正确索引吗?

0 个答案:

没有答案
相关问题