Admin changelist view defaults to `DISTINCT`, unusable on reasonably sized databases
|Reported by:||Henrique C. Alves||Owned by:||AlexMalek|
|Severity:||Normal||Keywords:||admin sql distinct slow performance|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||yes|
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.
Change History (7)
comment:1 Changed 4 years ago by
|Patch needs improvement:||unset|
|Status:||new → closed|
|Type:||Uncategorized → Bug|
comment:2 Changed 2 years ago by
|Status:||closed → new|