summaryrefslogtreecommitdiff
path: root/django/db/models/sql/compiler.py
diff options
context:
space:
mode:
Diffstat (limited to 'django/db/models/sql/compiler.py')
-rw-r--r--django/db/models/sql/compiler.py4
1 files changed, 3 insertions, 1 deletions
diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py
index a7a8c98b99..b28dc925ba 100644
--- a/django/db/models/sql/compiler.py
+++ b/django/db/models/sql/compiler.py
@@ -331,7 +331,9 @@ class SQLCompiler:
default_order, _ = ORDER_DIR["DESC"]
selected_exprs = {}
- if select := self.select:
+ # Avoid computing `selected_exprs` if there is no `ordering` as it's
+ # relatively expensive.
+ if ordering and (select := self.select):
for ordinal, (expr, _, alias) in enumerate(select, start=1):
pos_expr = PositionRef(ordinal, alias, expr)
if alias: