mongo中hint的使用

mongo官方文档对hint的解释如下:

The $hint operator forces the query optimizer to use a specific index to fulfill the query. Specify the index either by the index name or by document.

hint的命令行用法:

db.users.find().hint()

mongo中使用explain来确定查询使用的索引方案,对比explain的结果,可以反向校验hint的使用。explain的命令行用法:

db.collection.explain()

github.com/globalsign/mgo下对hint方法的注释如下:

// Hint will include an explicit "hint" in the query to force the server
// to use a specified index, potentially improving performance in some
// situations.  The provided parameters are the fields that compose the
// key of the index to be used.  For details on how the indexKey may be
// built, see the EnsureIndex method.

hint参数列表是用作索引的字段,更多细节请参考EnsureIndex方法。

// To obtain an index with a descending order,
// the field name should be prefixed by a dash

组合索引

mongo中组合索引称为Compound Index,不同于MySQL,它可以指定索引字段排列顺序。对于下面的索引:

{ userid: 1, score: -1 }

索引先通过userid进行排序,如果user_id相同,再根据score的倒序排。这中形式,对查询有什么影响呢?

mongo官方文档的例子来尝试理解,索引{ a: 1, b: -1 }支持{ a: 1, b: -1 }{ a: -1, b: 1 }的排序操作,但不支持{ a: -1, b: -1 } {a: 1, b: 1}的操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值