mLab Mongo 数据库查询的正确索引

Proper Indexes for mLab Mongo DB Query

我在 mLab 上有一个 mongo db 运行。某些查询(通过解析服务器)失败并出现以下错误:

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

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

这是我的索引:

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"
}

即使查询正在使用包含排序字段的索引 ({ "appName": 1, "email": 1, "isDeleted": 1, "_rperm": 1, "reportDate": -1 }),MongoDB 仍在执行 in-memory 排序。索引中的字段顺序导致 MongoDB 无法使用索引进行排序。

使用 $in 的查询字段应该包含在您的索引中,在您排序的字段之后。所以 _rperm 应该在 reportedDate 之后。试试这个索引:

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

您可以在此处阅读有关如何对索引中的字段进行排序的更多信息:http://docs.mlab.com/indexing/#determining-the-order-of-fields