#15819: Admin searches should use distinct, if query involves joins
-------------------------------------+-------------------------------------
     Reporter:  Adam Kochanowski     |                    Owner:  ryankask
  <aip@…>                            |                   Status:  reopened
         Type:  Bug                  |                  Version:  master
    Component:  contrib.admin        |               Resolution:
     Severity:  Normal               |             Triage Stage:  Accepted
     Keywords:                       |      Needs documentation:  0
    Has patch:  1                    |  Patch needs improvement:  0
  Needs tests:  0                    |                    UI/UX:  0
Easy pickings:  0                    |
-------------------------------------+-------------------------------------

Comment (by hcarvalhoalves):

 The problem, though, is that `DISTINCT` is '''painfully slow''' on most
 databases. A query like...

 {{{
 SELECT DISTINCT "mytable"."id", ... ORDER BY "mytable"."somefield" DESC
 LIMIT 100
 }}}

 ..will force a full table scan and query time will increase linearly with
 the number of rows. Django shouldn't ''guess'' when to use it internally
 without a way for client code to override it, ''specially'' if this logic
 is moved to the ORM.

 This currently happens on `contrib.admin` if you include a M2M relation in
 the `list_filters`, and makes the `changelist` views unusable if your
 database has more than a couple hundred queries. See ticket #18729 for
 that.

 More often than not, you want to optimize the query to use a subquery
 (giving hints to the query planner because it increases selectivity), or
 querying ''without'' `DISTINCT` and removing duplicates on Python side.
 Here's a case study on optimizing queries to avoid use of `DISTINCT`:
 http://www.databasejournal.com/features/postgresql/article.php/3437821
 /SELECT-DISTINCT-A-SQL-Case-Study.htm

-- 
Ticket URL: <https://code.djangoproject.com/ticket/15819#comment:19>
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 post to this group, send email to django-updates@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to