适用于mLab Mongodb查询的索引
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了适用于mLab Mongodb查询的索引相关的知识,希望对你有一定的参考价值。
我有一个在mLab上运行的mongo db。某些查询(通过parse-server)失败,出现以下错误:
OperationFailed: Sort operation used more than the maximum 33554432
bytes of RAM [duplicate]
我已经看到了类似于此的其他问题(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"
}
答案
即使查询使用包含排序字段的索引({ "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
以上是关于适用于mLab Mongodb查询的索引的主要内容,如果未能解决你的问题,请参考以下文章