#31940: Order of WHERE clauses do not match the order of the arguments to
.filter().
-------------------------------------+-------------------------------------
     Reporter:  Tommy Li             |                    Owner:  nobody
         Type:                       |                   Status:  closed
  Cleanup/optimization               |
    Component:  Database layer       |                  Version:  3.0
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:  wontfix
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by felixxm):

 * keywords:  orm =>
 * status:  new => closed
 * type:  Bug => Cleanup/optimization
 * resolution:   => wontfix


Comment:

 Thanks for this ticket.

 > After some experimentation, it looks like the WHERE clauses are ordered
 alphabetically by column name. Is that explicitly intended, or is it just
 a side effect of some implementation detail?

 `QuerySet.filter()` uses `Q()` objects. We added sorting to `kwargs` to
 make `Q.deconstruct()` deterministic on Python 3.5, see #29125. Django
 3.0+ supports only Python 3.6+, so we could theoretically revert this fix,
 but this will generate migrations for existing project. I think it isn't
 worth.

 > I would expect the order of the clauses in the WHERE statement to match
 the order of arguments passed to the filter statement. In my case, b_field
 has a much higher cardinality than a_field and I want the query to use an
 index I have on (b_field, a_field)

 Clauses ordering should not affect indexes, as far as I'm aware databases
 will use a `(b, a)`-index in both cases `b=1 AND a=2` and `a=2 AND b=1`.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/31940#comment:2>
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/066.f8910b9a449780c02da4d8e5e684be78%40djangoproject.com.

Reply via email to