#31792: Use `EXISTS(SELECT 1 ...)` for subqueries
-----------------------------------------+------------------------
               Reporter:  w0rp           |          Owner:  nobody
                   Type:  Uncategorized  |         Status:  new
              Component:  Uncategorized  |        Version:  3.0
               Severity:  Normal         |       Keywords:
           Triage Stage:  Unreviewed     |      Has patch:  0
    Needs documentation:  0              |    Needs tests:  0
Patch needs improvement:  0              |  Easy pickings:  0
                  UI/UX:  0              |
-----------------------------------------+------------------------
 If you write a QuerySet call like so in Django...

 {{{
 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.

-- 
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/047.8ff3fb97489b6c61125e5209405ef27b%40djangoproject.com.

Reply via email to