#24702 closed Cleanup/optimization (needsinfo)
Cache Query objects when possible
Reported by: | Omer Katz | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | yes |
Easy pickings: | no | UI/UX: | no |
Description
Currently Django constructs the Query objects every time you query the database.
This is often unnecessary and can be avoided.
I started memoizing some of the Query objects that the manager creates but I encountered some test failures related to prefetch_related querysets.
This is the memoized manager implementation https://github.com/thedrow/django/blob/feature/memoized-manager/django/db/models/manager.py#L244
I'm not sure it should be in a separate class but I created another one because of some of the required API changes.
Can anyone explain the test failures?
Change History (5)
comment:1 by , 10 years ago
comment:2 by , 10 years ago
Haven't looked in the tests code, so I can only guess that the tests fail because you're getting the memoized "all" query instead of one that should be limited by a FK (for the prefetched query).
This hints at a flaw in your design: You can't memoize based only on the call from the manager. You need to take account of how the manager was constructed. Also, on first reading your code, I thought it odd that you'd choose to put memoization on the manager class rather than the queryset class.
comment:3 by , 10 years ago
Has patch: | set |
---|---|
Patch needs improvement: | set |
Triage Stage: | Unreviewed → Accepted |
Version: | 1.8 → master |
comment:4 by , 9 years ago
Resolution: | → needsinfo |
---|---|
Status: | new → closed |
I guess it's not so clear how to proceed here. @thedrow, feel free to reopen if you return to this and get something working.
comment:5 by , 9 years ago
My approach doesn't work and I don't have an alternative.
I think this should go back to the drawing board.
Any thoughts on how this should be implemented?
These are the test failures: