1、查询索引
db.getCollection('diskHistory').getIndexes()
2、创建索引
db.getCollection('diskHistory').ensureIndex({"uid":1,"fid":1}) 联合索引
db.getCollection('diskHistory').ensureIndex({"uid":1}) 当地索引
有其他条件的可以加其他索引约束条件
3、删除索引
db.getCollection('diskHistory').dropIndex("uid_1_fid_1");
4、查询计划
db.getCollection('diskHistory').find({"fid":"e760aef6-47d3-4f3a-85e0-6f59637ac15b","uid":"1245"}).explain(true)
5、优先匹配查询条件一直的索引按最大包含来使用,fid,uid,version都存在,则使用uid_fid_version索引,没有就使用uid_fid,没有就 单独索引uid或者fid
例子:db.getCollection('diskHistory').find({"fid":"e760aef6-47d3-4f3a-85e0-6f59637ac15b","uid":"1245"}).explain(true) 使用fid_uid联合索引
6、如果只有fid_uid的联合索引,查询确实单个条件则不使用索引和Mysql差距
只有同时匹配才去使用联合索引
例子:db.getCollection('diskHistory').find({"fid":"e760aef6-47d3-4f3a-85e0-6f59637ac15b"})
7、删除联合索引,保留uid和fid索引,看条件,使用最优索引
db.getCollection('diskHistory').find({"uid":"1245","version":0,"fid":"e760aef6-47d3-4f3a-85e0-6f59637ac15b"}).explain(true)(使用fid)
db.getCollection('diskHistory').find({"fid":"e760aef6-47d3-4f3a-85e0-6f59637ac15b","uid":"1245","version":0}).explain(true)(使用fid)