适用于mLab Mongo数据库查询的索引

时间:2018-03-01 21:29:35

标签: mongodb mongodb-query mlab

我有一个在mLab上运行的mongo数据库。某些查询(通过parse-server)失败,出现以下错误:

OperationFailed: Sort operation used more than the maximum 33554432 
bytes of RAM [duplicate]

我在SO上看到了与此类似的其他问题(such as this),我已按照他们的建议添加索引。但是,此错误仍然存​​在。我很好奇我的索引是否正在做我需要他们做的事情,或者我是否需要添加更多/更好的索引或寻求其他解决方案。

以下是我的索引:

INDEXED FIELD(S)    UNIQUE  SPARSE  TTL SIZE

{ "_created_at": -1, "email": 1 }   (false) (false) -   268.00 KB   
{ "_id": 1 }    (true)  (false) -   216.00 KB   
{ "appName": 1, "email": 1, "isDeleted": 1, "_rperm": 1, "reportDate": -1 } (false) (false) -   140.00 KB   
{ "appname": 1, "_rperm": 1 }   (false) (false) -   60.00 KB    
{ "email": 1, "_created_at": -1 }   (false) (false) -   132.00 KB   
{ "email": 1 }  (false) (false) -   88.00 KB    
{ "isDeleted": -1, "_created_at": -1 }  (false) (false) -   116.00 KB
{ "reportDate": -1 }    (false) (false) -   4.00 KB

这是来自mLab探查器的违规查询:

{
"op": "query",
"ns": "REDACTED.Farm",
"query": {
    "find": "Farm",
    "filter": {
        "$or": [
            {
                "appName": "REDACTED",
                "email": "REDACTED@REDACTED.com",
                "isDeleted": false,
                "_rperm": {
                    "$in": [
                        null,
                        "*",
                        "REDACTED"
                    ]
                }
            },
            {
                "appName": "any",
                "email": "REDACTED",
                "isDeleted": false,
                "_rperm": {
                    "$in": [
                        null,
                        "*",
                        "REDACTED"
                    ]
                }
            }
        ]
    },
    "sort": {
        "reportDate": -1
    },
    "projection": {
        "name": 1,
        "propertyCount": 1,
        "reportDate": 1,
        "turnoverRate": 1,
        "_id": 1,
        "_created_at": 1,
        "_updated_at": 1
    },
    "limit": 100
},
"numYield": 0,
"locks": {
    "Global": {
        "acquireCount": {
            "r": 2
        }
    },
    "Database": {
        "acquireCount": {
            "r": 1
        }
    },
    "Collection": {
        "acquireCount": {
            "r": 1
        }
    }
},
"responseLength": 249,
"protocol": "op_query",
"millis": 7,
"planSummary": "IXSCAN { appName: 1, email: 1, isDeleted: 1, _rperm: 1, reportDate: -1 }, IXSCAN { appName: 1, email: 1, isDeleted: 1, _rperm: 1, reportDate: -1 }",
"ts": {
    "$date": "2018-03-01T16:50:25.550Z"
},
"client": "54.166.17.32",
"allUsers": [
    {
        "user": "REDACTED",
        "db": "REDACTED"
    }
],
"user": "MY_REDACTED_USER"
}

1 个答案:

答案 0 :(得分:2)

即使查询使用包含排序字段的索引({ "appName": 1, "email": 1, "isDeleted": 1, "_rperm": 1, "reportDate": -1 }),MongoDB仍在执行内存中排序。索引中的字段顺序阻止MongoDB使用索引进行排序。

使用$in的查询字段应该在您排序的字段之后包含在索引中。因此_rperm应该在reportedDate之后。请尝试使用此索引:

{ "appName": 1, "email": 1, "isDeleted": 1, "reportDate": -1, "_rperm": 1 }

您可以在此处详细了解如何在索引中订购字段:http://docs.mlab.com/indexing/#determining-the-order-of-fields