mongoTemplate count index(索引)

spring整合mongo给出的mongoTemplate在进行count的时候是不使用索引的(find能够)!!!!!虽然mongo执行语句时会自动帮咱们进行优化,但可能并非咱们想要使用的那个索引spring

  1. 表字段性能

  2. 加上索引优化

  3. 使用客户端进行查询 db.play_following.find({"obj_type":"game","channel":"hao"}).hint("channel_index").count() (hint表示使用某一个索引) 查询结果(1.6s内返回) code

  4. 使用mongoTemplate进行查询(60s返回,机器性能也有关系) 跟踪源码能够看到在count的时候先进行了query转化为DBObject的动做,可是咱们的hint并无被其加以转化,因此咱们索引并无被使用blog

  5. 解决办法:使用mongo原生的驱动(如下为参考): mongoTemplate.getDb().getCollection(PlayerFollowingPO.class.getAnnotation(Document.class).collection()).count(countDBObject, new DBCollectionCountOptions().hintString(index))索引

相关文章
相关标签/搜索