Opened 13 years ago

Closed 6 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)

16043-testcase.patch (2.1 KB ) - added by Aymeric Augustin 13 years ago.

Download all attachments as: .zip

Change History (14)

comment:1 by Aymeric Augustin, 13 years ago

Triage Stage: UnreviewedAccepted

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.

by Aymeric Augustin, 13 years ago

Attachment: 16043-testcase.patch added

comment:2 by Aymeric Augustin, 12 years ago

UI/UX: unset

Change UI/UX from NULL to False.

comment:3 by Aymeric Augustin, 10 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:4 by Tim Graham, 8 years ago

#25173 seems to be a duplicate and also has a test.

comment:5 by Paulo, 8 years ago

Owner: changed from nobody to Paulo
Status: newassigned

comment:6 by Paulo, 8 years ago

Has patch: set

comment:7 by Philip James, 8 years ago

I believe the PR czpython made for this ticket is https://github.com/django/django/pull/6721.

comment:8 by Paulo, 8 years ago

Has patch: unset

comment:9 by Paulo, 8 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:10 by Tim Graham <timograham@…>, 7 years ago

In bfb746f9:

Refs #16043 -- Refactored internal fields value cache.

  • Removed all hardcoded logic for _{fieldname}_cache.
  • Added an internal API for interacting with the field values cache.

Thanks carljm and MarkusH for support.

comment:11 by Paulo, 6 years ago

Resolution: fixed
Status: assignedclosed

comment:12 by Tim Graham, 6 years ago

Resolution: fixed
Status: closednew

I think a regression test should be added before closing this ticket.

comment:13 by Tim Graham, 6 years ago

Resolution: fixed
Status: newclosed

After investigation, I found that this was fixed by 78c5e7b90eee10067d39a8ba6588e6b53ba00d82 which did add an appropriate test.

Note: See TracTickets for help on using tickets.
Back to Top