应该如何索引此查询以提高性能
How this query should be indexed to increase performance
我有一个MongoDB查询
Schema: Demo
{
a: String,
b: Number,
c: Bool,
d: Number
}
Query:
db.Demo.find({ a:'test', c: true }).sort({b:-1, d: -1}).limit(40).explain("executionStats")
我试过应用这些索引:
类型 1 指数
- db.Demo.createIndex({b-1, d:-1})
- db.Demo.createIndex({a:1})
- db.Demo.createIndex({c:1})
类型 2 指数
- db.Demo.createIndex({a:1, c:1, b:-1, d:-1})
MongoDB 始终忽略 TYPE 2 index
作为 拒绝的计划 并使用 TYPE 1 Index
。但是TYPE One比较耗时,我觉得可以再优化一下。
按类型 1 查询解释结果。
....
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 20,
"executionTimeMillis" : 351,
"totalKeysExamined" : 647,
"totalDocsExamined" : 647,
}
找到查询的完美索引:
这个博客已经解释过了
Optimizing MongoDB Compound Indexes
查询将是
db.Demo.createIndex({c:1, b:-1, d:-1, a:1})
我有一个MongoDB查询
Schema: Demo
{
a: String,
b: Number,
c: Bool,
d: Number
}
Query:
db.Demo.find({ a:'test', c: true }).sort({b:-1, d: -1}).limit(40).explain("executionStats")
我试过应用这些索引:
类型 1 指数
- db.Demo.createIndex({b-1, d:-1})
- db.Demo.createIndex({a:1})
- db.Demo.createIndex({c:1})
类型 2 指数
- db.Demo.createIndex({a:1, c:1, b:-1, d:-1})
MongoDB 始终忽略 TYPE 2 index
作为 拒绝的计划 并使用 TYPE 1 Index
。但是TYPE One比较耗时,我觉得可以再优化一下。
按类型 1 查询解释结果。
....
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 20,
"executionTimeMillis" : 351,
"totalKeysExamined" : 647,
"totalDocsExamined" : 647,
}
找到查询的完美索引:
这个博客已经解释过了
Optimizing MongoDB Compound Indexes
查询将是
db.Demo.createIndex({c:1, b:-1, d:-1, a:1})