#34811: Queryset filter Q order changed
-------------------------------------+-------------------------------------
               Reporter:             |          Owner:  nobody
  cosmoscalibur                      |
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  4.2
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:  filter, Q
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 Using filters in order explicitly expressed by user is important because
 take advantage of indexes.

 When apply this query


 {{{
 TaxInput.objects.all(
                 force_visibility=True
             ).filter(
                 Q(filling_id=pk, updated_at__gt=last_engine.created_at)
                 | Q(filling_id=pk, deleted__gt=last_engine.created_at),
             )
 }}}


 the MySQL generated query is


 {{{
 SELECT
   *
 FROM
   `taxobjects_taxinput`
 WHERE
   ( `taxobjects_taxinput` . `filling_id` = ?
     AND `taxobjects_taxinput` . `updated_at` > ? )
   OR ( `taxobjects_taxinput` . `deleted` > ?
     AND `taxobjects_taxinput` . `filling_id` = ? )
 LIMIT
   ?
 }}}


 But should be


 {{{
 SELECT
   *
 FROM
   `taxobjects_taxinput`
 WHERE
   ( `taxobjects_taxinput` . `filling_id` = ?
     AND `taxobjects_taxinput` . `updated_at` > ? )
   OR ( `taxobjects_taxinput` . `filling_id` > ?
     AND `taxobjects_taxinput` . `deleted` = ? )
 LIMIT
   ?
 }}}

 This change of order create a 10x factor in query time, because this table
 has index filing_id, and also index (filing_id, deleted).

 Using Django 4.2.1 and SafeDelete 1.3.1 (because this is used
 force_visibility=True in all manager).

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34811>
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 django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018a571c0873-85aff85e-c75b-4620-9de8-ec856edb17da-000000%40eu-central-1.amazonses.com.

Reply via email to