我有一个MongoDB集合,当被查询时需要8-10秒。
这里设置了一些索引,下面是db.notif.getIndexes();的输出
db.notif.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "testing.notif"
},
{
"v" : 2,
"unique" : true,
"key" : {
"id" : 1
},
"name" : "id_1",
"ns" : "testing.notif",
"background" : true
},
{
"v" : 2,
"key" : {
"deleted" : 1
},
"name" : "deleted_1",
"ns" : "testing.notif",
"background" : true
},
{
"v" : 2,
"key" : {
"__ttl" : 1
},
"name" : "__ttl_1",
"ns" : "testing.notif",
"background" : true
}
]当我执行这个查询时
db.notif.find({
"mobile": "6281388756078",
"paytest.type": { "$in": ["IKLAN","INFO","WEB"] }
}).explain("executionStats")这需要8-9秒。我是否需要增强查询或添加进一步的索引?
发布于 2019-06-06 16:53:39
在查询中,您将筛选两个字段- "mobile“和"paytest.type”。您已经创建了4个索引,但它们都不在这两个字段上。
在其中一个字段上添加索引(基数更好),或者在两个字段上添加复合索引。
从一般的角度来看,移动字段看起来是更好的索引选择。试试这个:
db.notif.createIndex({ "mobile": 1 });https://stackoverflow.com/questions/56470085
复制相似问题