MongoDB 覆盖索引查询

覆盖查询:查询与投影的字段全部在索引里,无需回表读文档,explain 显示 IXSCAN 且 totalDocsExamined=0。

Advanced
Key PointsMongoDB,覆盖索引,covered query,IXSCAN

讲解

覆盖查询:查询与投影的字段全部在索引里,无需回表读文档,explain 显示 IXSCAN 且 totalDocsExamined=0。

示例

JavaScript
db.site.createIndex({ title: 1, alexa: 1 })
db.site.find({ title: "Google" }, { _id: 0, alexa: 1 })   // 只取索引内字段

db.site.find({ title: "Google" }, { alexa: 1 }).explain("executionStats")
Output
executionStats: { totalDocsExamined: 0, totalKeysExamined: 1 }

Friends & Partners