mongo explain 查组合索引的使用情况

版权声明:博主原创文章,欢迎大家转载! https://blog.csdn.net/happycxz/article/details/78919017

附原文链接: http://www.happycxz.com/m/?p=194

查某个请求是否使用了组合索引

mongo 3.2版本之前,用:

db.olamitvplayoplog.find({changelist:2}).explain()

mongo 3.2版本之后,用:

db.olamitvplayoplog.explain("executionStats").find({changelist:2})

老版本、大部分资料:db.collection.method().explain()
当前的API:db.collection.explain().method()

看结果中的 executionStages.inputStage.indexName 即可,若无此字段,表明查询没有使用到索引。
看 executionStages.inputStage.indexBounds 可以查索引的使用情况。

相关文章,参考:
http://blog.csdn.net/qq_17475155/article/details/53876834
http://blog.csdn.net/louisliaoxh/article/details/51556609

组合索引

跟其它数据库产品一样,MongoDB 也是有组合索引的,下面我们将在addr.city 和addr.state上建立组合索引。当创建组合索引时,字段后面的1 表示升序,-1 表示降序,是用1 还是用-1 主要是跟排序的时候或指定范围内查询 的时候有关的。另外,升序和降序的顺序不同都会产生不同的索引

db.factories.ensureIndex( { "addr.city" : 1, "addr.state" : 1 } );

// 下面的查询都用到了这个索引
db.factories.find( { "addr.city" : "Beijing", "addr.state" : "BJ" } );
db.factories.find( { "addr.city" : "Beijing" } );
db.factories.find().sort( { "addr.city" : 1, "addr.state" : 1 } );
db.factories.find().sort( { "addr.city" : 1 } )

//下面是没用上索引的
db.factories.find( { "addr.state" : "BJ" } );

只要查找的条件字段在索引中有,顺序最好与索引中一致,但如果索引中的第一个字段没有出现在条件中,则不会用索引

猜你喜欢

转载自blog.csdn.net/happycxz/article/details/78919017
今日推荐