diff options
| -rw-r--r-- | django/db/models/sql/compiler.py | 4 | ||||
| -rw-r--r-- | docs/releases/4.2.2.txt | 3 |
2 files changed, 6 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: diff --git a/docs/releases/4.2.2.txt b/docs/releases/4.2.2.txt index 7506b8a237..475531b531 100644 --- a/docs/releases/4.2.2.txt +++ b/docs/releases/4.2.2.txt @@ -22,3 +22,6 @@ Bugfixes * Fixed a bug in Django 4.2 where :option:`makemigrations --update` didn't respect the ``--name`` option (:ticket:`34568`). + +* Fixed a performance regression in Django 4.2 when compiling queries without + ordering (:ticket:`34580`). |
