﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
31792	Use `EXISTS(SELECT 1 ...)` for subqueries	w0rp	Simon Charette	"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."	Cleanup/optimization	closed	Database layer (models, ORM)	dev	Normal	fixed			Ready for checkin	1	0	0	0	0	0
