mongodb 查询效率优化 集合加索引(简单)

样例

  • 集合名:zhai
  • 文档:
{
    "_id": ObjectId("5f9d8a005752840001a20804"),
    "data": {
        "orderNo": "123456789",
   }
}

加索引

db.zhai.createIndex({"data.orderNo":1})

后台加索引(优化)

db.zhai.createIndex({"data.orderNo":1}, {background: true})

验证索引

db.zhai.find({"data.orderNo":"123456789"}).explain("executionStats")

执行结果分析

重点关注以下字段
  • executionStats:执行状态
  • totalDocsExamined:查询总行数
  • totalKeysExamined:总索引数量
  • nReturned:返回行数
  • executionTimeMillis:执行时长
  • indexName:索引名字 (删除索引时会用到)

删除指定索引

db.zhai.dropIndex("data.orderNo_1")

删除所有索引(慎重)

db.zhai.dropIndexes()

猜你喜欢

转载自blog.csdn.net/u010318957/article/details/109465734