Admin changelist view defaults to `DISTINCT`, unusable on reasonably sized databases
|Reported by:||hcarvalhoalves||Owned by:||nobody|
|Severity:||Normal||Keywords:||admin sql distinct slow performance|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
The default behavior on the admin is not usable for reasonably sized tables (a couple thousand tuples is enough for >1s queries).
Right now, it always includes a DISTINCT clause if you add a M2M field on list_filter, even if you're not applying any filters. Use of DISTINCT is known to be painfully slow, and even more so without a WHERE clause, because it forces SQL databases into a full table scan. The behavior is not easily overridden because the logic is scattered on a couple ChangeList methods (https://github.com/django/django/blob/master/django/contrib/admin/views/main.py#L299).
The only solution right now is not including a M2M field on list_filter and limiting functionality. It would be better if that behavior could be overridden (a use_distinct parameter on get_query_set?) or, optimally, moving this logic somewhere else. Ticket #15819 already includes a patch for that.