| | 8 | |
| | 9 | |
| | 10 | {{{#!diff |
| | 11 | diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py |
| | 12 | index a7a8c98b99..b28dc925ba 100644 |
| | 13 | --- a/django/db/models/sql/compiler.py |
| | 14 | +++ b/django/db/models/sql/compiler.py |
| | 15 | @@ -331,7 +331,9 @@ def _order_by_pairs(self): |
| | 16 | default_order, _ = ORDER_DIR["DESC"] |
| | 17 | |
| | 18 | selected_exprs = {} |
| | 19 | - if select := self.select: |
| | 20 | + # Avoid computing `selected_exprs` if there is no `ordering` as it's |
| | 21 | + # relatively expensive. |
| | 22 | + if ordering and (select := self.select): |
| | 23 | for ordinal, (expr, _, alias) in enumerate(select, start=1): |
| | 24 | pos_expr = PositionRef(ordinal, alias, expr) |
| | 25 | if alias: |
| | 26 | }}} |
| | 27 | |
| | 28 | Is there any way to confirm that the above patch addresses the regression? Can be run performance tests against the baseline from a particular PR? |