MongoDB聚合管道优化

时间:2016-05-09 07:10:49

标签: mongodb aggregation-framework

我有一个汇总管道,如下所述。考虑到我想要应用此管道的数十万个文档,我对如何优化它有疑问。以下是我的问题:

1。)匹配查询应用的最佳索引是什么。我应该有一个index = myArray.tags.tagKey,myArray.tags.tagValue,myArray.someRules.count,myArray.entryTime或者我应该为每个字段创建单独的索引。这样做的最佳方式是什么?

2。)如果你看到我的管道,我正在应用匹配运算符两次来过滤文件:一旦进入管道的开头,就可以获得我的索引的好处。其次,我在展开操作后使用它来过滤数组的特定数组元素" myArray"。有什么办法可以把它们结合起来。换句话说,可以使用$ match aggregation operator?

过滤数组元素

以下是我的文档示例:

{
"_id" : "8001409457639",
"myArray" : [ 
    {
        "entryID" : ObjectId("5101ab1116b12614fb083022"),
        "requestID" : "adb0d2asdfbe10",
        "owner" : "ABC",
        "version" : 1,
        "requestType" : "1",
        "someRules" : {
            "count" : 0,
            "somethingElse" : 11
        },
        "entryTime" : ISODate("2015-09-22T19:25:19.014Z"),
        "tags" : [ 
            {
                "tagKey" : "Owner",
                "tagValue" : "ABC"
            }, 
            {
                "tagKey" : "Request Type",
                "tagValue" : "1"
            }
        ]
    }, 
    {
        "entryID" : ObjectId("5101a111c6b12614fb083022"),
        "requestID" : "fc057asdf16480",
        "owner" : "ABC",
        "version" : 1,
        "requestType" : "1",
        "someRules" : {
            "count" : 10,
            "somethingElse" : 0
        },
        "entryTime" : ISODate("2015-09-22T19:44:26.558Z"),
        "tags" : [ 
            {
                "tagKey" : "Owner",
                "tagValue" : "ABC"
            }, 
            {
                "tagKey" : "Request Type",
                "tagValue" : "1"
            }
        ]
    }, 
    {
        "entryID" : ObjectId("5101b111c6b12614fb083011"),
        "requestID" : "40c7a0ads2dd8c2",
        "owner" : "ABC",
        "version" : 1,
        "requestType" : "1",
        "someRules" : {
            "count" : 10,
            "somethingElse" : 0
        },
        "entryTime" : ISODate("2015-09-22T20:24:15.347Z"),
        "tags" : [ 
            {
                "tagKey" : "Owner",
                "tagValue" : "ABC"
            }, 
            {
                "tagKey" : "Request Type",
                "tagValue" : "1"
            }
        ]
    }
],
"lockAcquiredBy" : "james",
"lockStartTime" : ISODate("2015-11-18T22:36:05.266Z")
}

这是我的聚合管道:

[{
    "$match": {
        "$and": [{
            "$or": [{
                "myArray.tags": {
                    "$elemMatch": {
                        "tagKey": "Owner",
                        "tagValue": "ABC"
                    }
                }
            }, {
                "myArray.tags": {
                    "$elemMatch": {
                        "tagKey": "Owner",
                        "tagValue": "DEF"
                    }
                }
            }]
        }, {
            "$or": [{
                "myArray.tags": {
                    "$elemMatch": {
                        "tagKey": "Request Type",
                        "tagValue": "4"
                    }
                }
            }, {
                "myArray.tags": {
                    "$elemMatch": {
                        "tagKey": "Request Type",
                        "tagValue": "Retry"
                    }
                }
            }]
        }],
        "myArray.someRules.count": 0,
        "myArray.entryTime": {
            "$gte": {
                "$date": "2016-01-05T01:59:07.763Z"
            }
        }
    }
}, {
    "$unwind": "$myArray"
}, {
    "$match": {
        "$and": [{
            "$or": [{
                "myArray.tags": {
                    "$elemMatch": {
                        "tagKey": "Owner",
                        "tagValue": "ABC"
                    }
                }
            }, {
                "myArray.tags": {
                    "$elemMatch": {
                        "tagKey": "Owner",
                        "tagValue": "DEF"
                    }
                }
            }]
        }, {
            "$or": [{
                "myArray.tags": {
                    "$elemMatch": {
                        "tagKey": "Request Type",
                        "tagValue": "4"
                    }
                }
            }, {
                "myArray.tags": {
                    "$elemMatch": {
                        "tagKey": "Request Type",
                        "tagValue": "Retry"
                    }
                }
            }]
        }],
        "myArray.someRules.count": 0,
        "myArray.entryTime": {
            "$gte": {
                "$date": "2016-01-05T01:59:07.763Z"
            }
        }
    }
}
// More steps in the pipeline.
]

0 个答案:

没有答案