Opened 6 years ago

Closed 17 months ago

#11320 closed Cleanup/optimization (fixed)

Over aggressive join promotion with exclude()

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

Description

Currently filter() optimizes it so that when you create a query such that a relation must exist it uses an INNER JOIN, since that's faster than an OUTER JOIN. However with exclude() a query such as:

class MyModel(models.Model):
    venue = models.ForeignKey("Venue", null=True)

MyModel.objects.exclude(venue=None).exclude(venue__name='a')

For this query it should be using an INNER JOIN but instead uses a LEFT OUTER JOIN.

Attachments (1)

exclude-none-join.diff (2.0 KB) - added by Alex 6 years ago.
First stab at a patch, I think it fails in nested disjunctives and probably other edge cases though.

Download all attachments as: .zip

Change History (9)

Changed 6 years ago by Alex

First stab at a patch, I think it fails in nested disjunctives and probably other edge cases though.

comment:1 Changed 6 years ago by Alex

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Actually, upon further review my patch is even worse than the comment indicates. The issue is that anything n an exlcude() gets unconditionally promoted to an OUTER JOIN, so my current patch would fail for something like Event.objects.filter(venue__isnull=False).exlcude(venue__name='a') , because the exclude join promotion doesn't respect anything.

comment:2 Changed 6 years ago by Alex

  • Triage Stage changed from Unreviewed to Accepted

comment:3 Changed 4 years ago by julien

  • Severity set to Normal
  • Type set to Cleanup/optimization

comment:4 Changed 3 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:5 Changed 3 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

comment:6 Changed 19 months ago by timo

Confirmed this is still an issue on master.

comment:7 Changed 17 months ago by akaariai

  • Has patch set

comment:8 Changed 17 months ago by Anssi Kääriäinen <akaariai@…>

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

In e7b61e571788b7d3efc1ba704dd908f828340255:

Fixed #11320 -- exclude() too aggressive in join promotion

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