Opened 5 years ago

Last modified 8 months ago

#16043 new Cleanup/optimization

Specialization cache should be filled/shared with parent object cache (multitable inheritance)

Reported by: zimnyx Owned by: nobody
Component: Database layer (models, ORM) Version: master
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


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
    nick: Homer

- model: auto.Car
  pk: 1
    owner: 1
    some_id: 'minii A'
- model: auto.MiniMorris
  pk: 1
    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 aaugustin 5 years ago.

Download all attachments as: .zip

Change History (5)

comment:1 Changed 5 years ago by aaugustin

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

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.

Changed 5 years ago by aaugustin

comment:2 Changed 4 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:3 Changed 3 years ago by aaugustin

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 Changed 8 months ago by timgraham

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

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