[SERVER-14740] Query rewrite of special $or leaf case to rooted $or not working for nested expressions Created: 31/Jul/14 Updated: 10/Dec/14 Resolved: 07/Aug/14 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | Index Maintenance, Querying |
| Affects Version/s: | 2.4.2 |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Trivial - P5 |
| Reporter: | flavio alberti | Assignee: | Unassigned |
| Resolution: | Duplicate | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Operating System: | Linux | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Steps To Reproduce: | 1) create a testindex collection
3) create some indexes
4) optimized query
5) Incapsulate the or inside an other $or or $and
Mongodb is not able to rewrite the nested $or into the canonical rooted $or form, thus can't make use of the index union. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Participants: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Description |
These three queries are semantically the same, but only the 1st can perform over the indexes user_1_removed_1_objId_1 and user_1_removed_1_p.id_1 The nested $and / $or operators block the mongodb query rewrite optimization. |
| Comments |
| Comment by Thomas Rueckstiess [ 31/Jul/14 ] | |||
|
Hi Flavio, Your first (optimized) query falls into a specific category that had special-case code in 2.4 to deal with. A query of the shape
was internally rewritten to
making the query a root-level $or for which the union of the two individual query results can be calculated. Adding more $and / $or clauses to the query, for example
would semantically not change it, but the query optimizer would no longer consider this form for the rewrite. As we have rewritten the entire query engine for 2.6, it's unlikely that we can backport any further optimizations around this and it is unfortunately up to you to simplify the query to the simpler form. Please also be aware that due to the significant changes in 2.6, the special logic to rewrite { a:1, { $or: [b:1, c:1] } } to a root-level $or has not been re-implemented in an effort to keep the code clean and general and not add to many special cases. In order to get the same behavior in 2.6, you would have to transform the query yourself to move the $or to the root-level, as in { $or: [ {a:1, b:1}, {a:1, c:1} ] }. So far, to my knowledge we did not have any reports where this has been a problem, but we are tracking the issue under I hope this explains why you are seeing this behavior and our motivation for the change in 2.6. Regards, |