#28211: Inefficient query produced when using OR'ed Q objects
-------------------------------------+-------------------------------------
               Reporter:  Tom        |          Owner:  nobody
                   Type:             |         Status:  new
  Cleanup/optimization               |
              Component:  Database   |        Version:  1.10
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 When using OR'ed Q objects in a `.filter()` call Django could be more
 intelligent about the query it produces.

 For example, the following two queries both produce `LEFT OUTER JOIN`:

 `SomeModel.objects.filter(Q(related_objects__field=1) |
 Q(related_objects__other_field=1)`

 `SomeModel.objects.filter(Q(related_objects__field=1) | Q())`

 In the case of the second query it could be reduced to a `INNER JOIN` as
 the `OR` is redundant and it is functionally the same as a
 `.filter(related_objects__field=1)`.

 It is also quite a common pattern to do:

 ```
 filters = Q()
 if condition:
    filters |= Q(x=1)
 if other_condition:
    filters |= Q(y=2)
 ```

 And with the current implementation it will always produce a query that
 assumes `filters` is a valid `OR`. Django should/could be more intelligent
 and detect if there is only one `OR` condition, and reduce it.

--
Ticket URL: <https://code.djangoproject.com/ticket/28211>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/046.7a95efadc693c7bdbb7ab3e06c10ed47%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to