首先看下同一个接口优化前后查询时间对比
Mongodb数据库查询40s优化到600ms其实只差一个索引
使用下面命令给t_invetory_quantity表的repositoryId加上索引
db.t_invetory_quantity.createIndex({repositoryId:1})
加入索引后使用EXPLAIN命令查看优化器如何决定执行查询
db.getCollection('t_invetory_quantity').find({"repositoryId":12}).explain("executionStats")
发现原来totalKeysExamined为0现在变为了24465,说明索引扫描条目从0变为了24465,从而缩短了查询时间。nReturned表示始加返回的数据数。
当遇到多个查询条件时使用组合索引
db.t_invetory_quantity.createIndex({'repositoryId':1,'createTime':1},{name:'index_nas'})
查询索引是否创建成功
db.t_invetory_quantity.getIndexes()
删除索引
db.t_invetory_quantity.dropIndexes()
Mongodb详细索引介绍查看https://blog.csdn.net/weixin_33446857/article/details/83085018