mongodb - 应如何索引此查询以提高性能
问题描述
我有一个 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,
}
解决方案
推荐阅读
- javascript - VueJS 点击在移动设备上不起作用,未检测到点击监听器
- angular - 带管道的角度条件类
- javascript - 如果 URL 包含某个列表字符串,则显示 DIV
- javascript - chrome调试器打开时过滤简单数据表时出错
- sql - SQL 计算从当月的第一天到指定日期的差异
- bash - bash 中的勾股定理作为函数
- oauth - 其他人登录我的网站后,我如何在他们的 Google 帐户中远程创建活动?
- postgresql - Use functions or expressions in Postgres alias names - possible?
- python - Flask SQLAlchemy,通过类构造函数添加数据库记录时,'__init__ 只需要一个参数'
- javascript - Drawing objects on canvas created by eventlisteners()