Opened 7 years ago
Last modified 7 years ago
#28933 closed Cleanup/optimization
Optimize the queries for ModelAdmin.date_hierarchy — at Version 3
Reported by: | hakib | Owned by: | nobody |
---|---|---|---|
Component: | contrib.admin | Version: | 2.0 |
Severity: | Normal | Keywords: | date_hierarchy |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | yes |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
The predicate generated by ModelAdmin.date_hierarchy
makes it very difficult for databases to optimize the query.
The following date hierarchy:
/admin/app/model?created__year=2017&created__month=12&created__day=16
generates the following WHERE clause (PostgreSQL):
WHERE created between '2017-01-01' and '2017-31-12' and EXTRACT('month', created) = 12 and EXTRACT('day', created) = 16
The query above will not be able to utilize range based indexes on the date hierarchy column - on big tables this has a significant performance impact.
The current implementation of date hierarchy is relying on the "default" filtering mechanism used by the admin. I propose implementing custom filtering for that will better utilize it's hierarchical nature and make it more database friendly.
Instead of the query above the date hierarchy would generate the following predicates for different levels of the hierarchy:
/admin/app/model?created__year=2017&created__month=12&created__day=16 WHERE created >= '2017-12-16' and created < '2017-12-17'
/admin/app/model?created__year=2017&created__month=12 WHERE created >= '2017-12-01' and created < '2018-01-01'
/admin/app/model?created__year=2017 WHERE created >= '2017-01-01' and created < '2018-01-01'
I already wrote about this issue and published a package that implements the above as a custom SimpleListFilter
.
I already have a PR ready - I'm following proper protocol here so please let me know if this is acceptable.
Change History (3)
comment:1 by , 7 years ago
Has patch: | set |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:3 by , 7 years ago
Description: | modified (diff) |
---|---|
Patch needs improvement: | set |
Summary: | Implement a range-based filter for Django Admin date_hierarchy → Optimize the queries for ModelAdmin.date_hierarchy |
Triage Stage: | Ready for checkin → Accepted |
Type: | New feature → Cleanup/optimization |
I left some comments for improvement.
PR