summaryrefslogtreecommitdiff
path: root/django/db/models/sql/query.py
diff options
context:
space:
mode:
authorDavid-Wobrock <david.wobrock@gmail.com>2020-11-11 23:16:32 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-11-16 20:29:13 +0100
commitba42569d5c01b58b2999e393f097b530e538ec41 (patch)
tree7bdcbc103ef4d5cd26a5e83b552e382e4928d5a8 /django/db/models/sql/query.py
parent7b42d346465abb560d1b774bdf17d094fad75571 (diff)
Fixed #31507 -- Added QuerySet.exists() optimizations to compound queries.
Diffstat (limited to 'django/db/models/sql/query.py')
-rw-r--r--django/db/models/sql/query.py13
1 files changed, 10 insertions, 3 deletions
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index 5a7dc25fd9..b7f8053cb1 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -525,7 +525,7 @@ class Query(BaseExpression):
def has_filters(self):
return self.where
- def exists(self):
+ def exists(self, using, limit=True):
q = self.clone()
if not q.distinct:
if q.group_by is True:
@@ -534,14 +534,21 @@ class Query(BaseExpression):
# SELECT clause which is about to be cleared.
q.set_group_by(allow_aliases=False)
q.clear_select_clause()
+ if q.combined_queries and q.combinator == 'union':
+ limit_combined = connections[using].features.supports_slicing_ordering_in_compound
+ q.combined_queries = tuple(
+ combined_query.exists(using, limit=limit_combined)
+ for combined_query in q.combined_queries
+ )
q.clear_ordering(True)
- q.set_limits(high=1)
+ if limit:
+ q.set_limits(high=1)
q.add_extra({'a': 1}, None, None, None, None, None)
q.set_extra_mask(['a'])
return q
def has_results(self, using):
- q = self.exists()
+ q = self.exists(using)
compiler = q.get_compiler(using=using)
return compiler.has_results()