summaryrefslogtreecommitdiff
path: root/django/db/models/sql/compiler.py
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2023-05-21 16:59:56 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-05-22 07:57:05 +0200
commite1c00f8b361bf89deb68733bbc5189734bbcd6b5 (patch)
treed9d2806b9315561a54d4591dd549067c98794fcb /django/db/models/sql/compiler.py
parentcdd970ae22303e6c58b5c1f3ba695c470a811b56 (diff)
[4.2.x] Fixed #34580 -- Avoided unnecessary computation of selected expressions in SQLCompiler.
Performance regression in 278881e37619278789942513916acafaa88d26f3. Co-authored-by: David Smith <smithdc@gmail.com> Backport of 98f6ada0e2058d67d91fb6c16482411ec2ca0967 from main
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 1c16217ae7..3b438a1de6 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: