Opened 3 years ago

Closed 3 years ago

#19500 closed Bug (fixed)

Join reuse regression

Reported by: akaariai Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords:
Cc: charette.s@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

The commit 68847135bc9acb2c51c2d36797d0a85395f0cd35 for ticket #18748 caused a severe regression in join reuse. Chained filters for direct foreign keys do not reuse existing joins at all. For example

qs.filter(fk__somecol__gt=1).filter(fk__somecol__lt=10)

causes two joins instead of one.

The patch at https://github.com/akaariai/django/commit/688ec5aca2ca387774016aef78560c199d480b6c fixes the issue and adds various test for join reuse. It actually simplifies the code a little.

As a bonus we can now correctly reuse joins in chained filter() calls when using reverse o2o fields.

I think I will commit the patch soon as currently the code isn't behaving nicely.

Change History (3)

comment:1 Changed 3 years ago by charettes

  • Cc charette.s@… added
  • Patch needs improvement set

Added some comments on the patch over github.

comment:3 Changed 3 years ago by Anssi Kääriäinen <akaariai@…>

  • Resolution set to fixed
  • Status changed from new to closed

In 3dcd435a0eb4380a3846e9c65140df480975687e:

Fixed #19500 -- Solved a regression in join reuse

The ORM didn't reuse joins for direct foreign key traversals when using
chained filters. For example:

qs.filter(fksomefield=1).filter(fksomefield=2))

produced two joins.

As a bonus, reverse onetoone filters can now reuse joins correctly

The regression was caused by the join() method refactor in commit
68847135bc9acb2c51c2d36797d0a85395f0cd35

Thanks for Simon Charette for spotting some issues with the first draft
of the patch.

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