1. 程式人生 > >在MongoDB中組合使用$or、sort()或$or、$and時,查詢效能可能會很差

在MongoDB中組合使用$or、sort()或$or、$and時,查詢效能可能會很差

單獨使用$or、$and、sort()時,查詢效能均可,但$or與$and或sort()組合查詢,其時長會增加幾倍!!!

將來會修改,今天先記錄一下。

論壇回覆:

I believe the query optimizer is choosing to use the name index and 
walk it backwards. As it goes through the index it compares the user 
and owner attributes to your parameters and collects them in sorted 
order. As a result the nscanned objects is much larger than in the 
other two cases. 

I would vote up the issue to prioritize it. 

-Tyler

注:如果查詢中使用了$or,並且查詢元素有索引的話,那麼也會使用該元素的索引的。換句話說,如果查詢中只有$or時(無sort時),不會出現這裡的問題。另外在使用$and和$or的組合時也發現了類似的問題。