Opened 15 years ago

Closed 10 years ago

#11320 closed Cleanup/optimization (fixed)

Over aggressive join promotion with exclude()

Reported by: Alex Gaynor Owned by: nobody
Component: Database layer (models, ORM) Version: dev
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 Gaynor 15 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)

by Alex Gaynor, 15 years ago

Attachment: exclude-none-join.diff added

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

comment:1 by Alex Gaynor, 15 years ago

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 by Alex Gaynor, 15 years ago

Triage Stage: UnreviewedAccepted

comment:3 by Julien Phalip, 13 years ago

Severity: Normal
Type: Cleanup/optimization

comment:4 by Aymeric Augustin, 12 years ago

UI/UX: unset

Change UI/UX from NULL to False.

comment:5 by Aymeric Augustin, 12 years ago

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:6 by Tim Graham, 11 years ago

Confirmed this is still an issue on master.

comment:7 by Anssi Kääriäinen, 10 years ago

Has patch: set

comment:8 by Anssi Kääriäinen <akaariai@…>, 10 years ago

Resolution: fixed
Status: newclosed

In e7b61e571788b7d3efc1ba704dd908f828340255:

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

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