Opened 14 years ago
Closed 7 years ago
#16043 closed Cleanup/optimization (fixed)
Specialization cache should be filled/shared with parent object cache (multitable inheritance)
Reported by: | Piotr Czachur | Owned by: | Paulo |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Here is some multi-table inheritance example:
class Owner(models.Model): nick = models.CharField(max_length=100) class Car(models.Model): owner = models.ForeignKey(Owner) some_id = models.CharField(max_length=100) class MiniMorris(Car): mini_type = models.CharField(max_length=1)
- model: auto.Owner pk: 1 fields: nick: Homer - model: auto.Car pk: 1 fields: owner: 1 some_id: 'minii A' - model: auto.MiniMorris pk: 1 fields: mini_type: 'e'
>>> c = Car.objects.select_related('owner').get(pk=1) >>> # SELECT "auto_car"."id", "auto_car"."owner_id", "auto_car"."some_id", "auto_owner"."id", "auto_owner"."nick" FROM "auto_car" INNER JOIN "auto_owner" ON ("auto_car"."owner_id" = "auto_owner"."id") WHERE "auto_car"."id" = 1 >>> c.owner <Owner: Owner object> >>> # No SQL, cache hit. >>> m = c.minimorris >>> # SELECT "auto_car"."id", "auto_car"."owner_id", "auto_car"."some_id", "auto_minimorris"."car_ptr_id", "auto_minimorris"."mini_type" FROM "auto_minimorris" INNER JOIN "auto_car" ON ("auto_minimorris"."car_ptr_id" = "auto_car"."id") WHERE "auto_minimorris"."car_ptr_id" = 1 >>> # SQL is fine, we need to obtain this specialization >>> m.owner <Owner: Owner object> >>> # SELECT "auto_owner"."id", "auto_owner"."nick" FROM "auto_owner" WHERE "auto_owner"."id" = 1 >>> # Redundant SQL. This could be obtained from cache.
SQL issued by "m.owner" is redundant as owner is already cached in parent object.
Attachments (1)
Change History (14)
comment:1 by , 14 years ago
Triage Stage: | Unreviewed → Accepted |
---|
by , 14 years ago
Attachment: | 16043-testcase.patch added |
---|
comment:3 by , 11 years ago
The issue still exists. However, it can be reproduced without select_related. The problem is really in model inheritance. So the test case I wrote two years ago is inappropriate.
comment:5 by , 9 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:6 by , 9 years ago
Has patch: | set |
---|
comment:7 by , 9 years ago
I believe the PR czpython made for this ticket is https://github.com/django/django/pull/6721.
comment:8 by , 9 years ago
Has patch: | unset |
---|
comment:9 by , 9 years ago
Removed the has patch set flag as PR 6721 is only the base for this.
The fix will be in a separate PR.
comment:11 by , 7 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
comment:12 by , 7 years ago
Resolution: | fixed |
---|---|
Status: | closed → new |
I think a regression test should be added before closing this ticket.
comment:13 by , 7 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
After investigation, I found that this was fixed by 78c5e7b90eee10067d39a8ba6588e6b53ba00d82 which did add an appropriate test.
This looks a lot like #15250, but it's not exactly the same bug. At least, the patch currently available on #15250 does not fix this issue.
I'm attaching a test case in the form of a patch.