单独使用$or、$and、sort()时,查询性能均可,但$or与$and或sort()组合查询,其时长会增加几倍!!!
后来在官方论坛中提问,得知有一个bug:https://jira.mongodb.org/browse/SERVER-1205
将来会修改,今天先记录一下。
论坛回复:
I believe the issue you are running into is expressed in this JIRA
ticket: https://jira.mongodb.org/browse/SERVER-1205
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的组合时也发现了类似的问题。