{{{
MyModel.objects.all().exists()
}}}
the query run will be like so.
{{{
SELECT 1 AS "a" FROM "myapp_mymodel" LIMIT 1;
}}}
If you use the `Exists()` function to filter with a subquery like so...
{{{
MyModel.objects.filter(Exists(MyOtherModel.objects.all()))
}}}
The subquery will be run like so.
{{{
... WHERE EXISTS(SELECT "myapp_myothermodel"."id", ... FROM
"myapp_myothermodel");
}}}
It would be nice if the queries generated for `Exists()` used `SELECT 1`
like `.exists()` does, where possible. In an app I work on, I have one
query in particular that is 15KB in size, but only around 8KB if I apply
`.annotate(_1=Value(1, output_field=IntegerField())).values_list('_1')` to
all of the subqueries. That change alone is enough to make my queries much
easier to debug.
--
Ticket URL: <https://code.djangoproject.com/ticket/31792>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* type: Uncategorized => Cleanup/optimization
* stage: Unreviewed => Accepted
Comment:
I could swear there was another ticket about that but I cannot find it.
The logic to clear the column is a bit more complex than what you've
described here but it's all detailed in `Query.has_results`
[https://github.com/django/django/blob/156a2138db20abc89933121e4ff2ee2ce56a173a/django/db/models/sql/query.py#L526-L535
(link)] and `SQLCompiler.has_results`
[https://github.com/django/django/blob/156a2138db20abc89933121e4ff2ee2ce56a173a/django/db/models/sql/compiler.py#L1129-L1130
(link)] so it could be added to `Exists.as_sql`.
Ideally the logic would be encapsulated in a `Query` method so it doesn't
have to be duplicated in `Query.has_results` and `Exists.as_sql` so both
could path could benefit from optimizations such as #24296.
--
Ticket URL: <https://code.djangoproject.com/ticket/31792#comment:1>
* component: Uncategorized => Database layer (models, ORM)
--
Ticket URL: <https://code.djangoproject.com/ticket/31792#comment:2>
* owner: nobody => Akhil Gandu
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/31792#comment:3>
* owner: Akhil Gandu => (none)
* status: assigned => new
--
Ticket URL: <https://code.djangoproject.com/ticket/31792#comment:4>
* owner: (none) => Harpreet Sharma
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/31792#comment:5>
* owner: Harpreet Sharma => (none)
* status: assigned => new
--
Ticket URL: <https://code.djangoproject.com/ticket/31792#comment:6>
* owner: (none) => Simon Charette
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/31792#comment:3>
* has_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/31792#comment:4>
* version: 3.0 => master
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/31792#comment:5>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"51297a92324976a704279b567ec4f80bb92d7b60" 51297a9]:
{{{
#!CommitTicketReference repository=""
revision="51297a92324976a704279b567ec4f80bb92d7b60"
Fixed #31792 -- Made Exists() reuse QuerySet.exists() optimizations.
The latter is already optimized to limit the number of results, avoid
selecting unnecessary fields, and drop ordering if possible without
altering the semantic of the query.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/31792#comment:6>
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):
In [changeset:"45f4282149e13a2c1548a579f60d098e397a33d7" 45f4282]:
{{{
#!CommitTicketReference repository=""
revision="45f4282149e13a2c1548a579f60d098e397a33d7"
Refs #31792 -- Updated SQL example in Exists() docs.
Follow up to 51297a92324976a704279b567ec4f80bb92d7b60.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/31792#comment:7>