Admin changelist view defaults to `DISTINCT`, unusable on reasonably sized databases
|Reported by:||hcarvalhoalves||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 2 years ago by tswicegood
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
- Resolution set to duplicate
- Status changed from new to closed
- Type changed from Uncategorized to Bug
comment:2 Changed 10 months ago by AlexMalek
- Resolution duplicate deleted
- Status changed from closed to new
comment:3 Changed 9 months ago by AlexMalek
- Owner changed from nobody to AlexMalek
- Status changed from new to assigned