spring整合mongo給出的mongoTemplate在進行count的時候是不使用索引的(find能夠)!!!!!雖然mongo執行語句時會自動幫咱們進行優化,但可能並非咱們想要使用的那個索引spring
表字段性能
加上索引優化
使用客戶端進行查詢 db.play_following.find({"obj_type":"game","channel":"hao"}).hint("channel_index").count()
(hint表示使用某一個索引) 查詢結果(1.6s內返回) code
使用mongoTemplate進行查詢(60s返回,機器性能也有關係) 跟蹤源碼能夠看到在count的時候先進行了query轉化爲DBObject的動做,可是咱們的hint並無被其加以轉化,因此咱們索引並無被使用blog
解決辦法:使用mongo原生的驅動(如下爲參考): mongoTemplate.getDb().getCollection(PlayerFollowingPO.class.getAnnotation(Document.class).collection()).count(countDBObject, new DBCollectionCountOptions().hintString(index))
索引