#34845: Slow query when using exclude
-------------------------------------+-------------------------------------
Reporter: Amin | Owner: nobody
Aminian |
Type: | Status: new
Cleanup/optimization |
Component: Database | Version: 4.2
layer (models, ORM) |
Severity: Normal | Keywords: exclude,filter
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Hi!
I have a code snippet like this:
{{{
qs = Order.objects.exclude(
payment__isnull=True
)
}}}
Which creates the following SQL:
{{{
SELECT "order"."id",
...
FROM "order"
WHERE NOT (EXISTS
(SELECT 1 AS "a"
FROM "order" U0
LEFT OUTER JOIN "payment" U1 ON (U0."id" = U1."payable_id"
AND
(U1."payable_type_id" = 30))
WHERE (U1."id" IS NULL
AND U0."id" = ("order"."id"))
LIMIT 1))
LIMIT 10
}}}
(This is part of original query)
In my large DB, this section increases response time about 10s.
By changing `exclude` to `filter`, we will not have subquery anymore:
{{{
qs = Order.objects.filter(
payment__isnull=False
)
}}}
And the SQL:
{{{
SELECT "order"."id",
...
FROM "order"
INNER JOIN "payment" ON ("order"."id" = "payment"."payable_id"
AND
("payment"."payable_type_id" = 30))
WHERE "payment"."id" IS NOT NULL
LIMIT 10
}}}
And with this, I don't face high response time anymore and everything is
fine.
Why Django uses subquery when using `exclude` ?
I just wanted to make sure it was intended.
--
Ticket URL: <https://code.djangoproject.com/ticket/34845>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/0107018aa37aa3db-e0b33ba0-642e-4710-9277-abb7f5a686ce-000000%40eu-central-1.amazonses.com.