Repository navigation
Prune unused columns from nested subqueries - #1671
Open
dimitri-yatsenko wants to merge 1 commit into
Open
dimitri-yatsenko wants to merge 1 commit into
dimitri-yatsenko wants to merge 1 commit into
Conversation
Nested subqueries now select only their primary key plus the attributes the enclosing query names. This avoids carrying unused columns, such as in-table blobs, through subqueries the database materializes (dj.Top, aggregations, unions). On MySQL with in-table blobs, queries containing these operators ran 26-560x faster in the benchmark reported in #1665. - QueryExpression.from_clause passes each source the fields it needs, derived from the words in the enclosing level's SQL text. - Aggregation keeps its HAVING aliases; without GROUP BY it keeps all attributes. - Union.make_sql accepts a field list and uses UNION ALL when the primary key and secondary attributes are selected. This also fixes restricting by a union and inserting from a union. - len() and bool() on aggregations and unions select only what they need. - progress() builds narrower subqueries. - New setting prune_subqueries (DJ_PRUNE_SUBQUERIES), on by default; set it to False to render subqueries with all attributes as before. - CI runs the integration suite once more with pruning off. Closes #1665
dimitri-yatsenko
requested review from
MilagrosMarin,
lum-agilyti and
ttngu207
November 10, 2026 19:32
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1665.
Nested subqueries now select only their primary key plus the attributes the enclosing query names. Before, every nested level selected its whole heading. That is harmless when the database folds the subquery into the outer query, but costly when it must copy the subquery into a temporary table (
dj.Top, aggregations, unions): every column gets copied, in-table blobs included. #1665 has the full benchmark. On MySQL with in-table blobs, queries containing these operators ran 26–560× faster (from 1–19 s down to 3–143 ms), and all 102 compared results matched master.Changes
Pruning (
expression.py)QueryExpression.from_clause(fields)builds this level's SQL text: the select list, WHERE, ORDER BY and USING lists, plus GROUP BY, HAVING and the left restriction for an aggregation. Each source then renders only its primary key and the attributes whose names appear as words in that text. The scan ignores case and keeps quoted text, so it may keep an extra column but never drops a needed one.make_sqlpasses its resolved fields down tofrom_clause.Aggregationkeeps its HAVING aliases. Without GROUP BY it keeps every attribute.Topkeeps every column its ORDER BY names.Always active, whatever the setting
Union.make_sql(fields)accepts a field list. It usesUNION ALLwhen the primary key and secondary attributes are selected; the two branches have disjoint keys, so no row can repeat. Key-only requests useUNIONover the keys. This fixes theTypeErrorraised when restricting by a union or inserting from one.len()on aggregations and unions, andbool()on aggregations, select only the primary key plus any columns HAVING or a Top's ORDER BY needs.bool()on a union is computed asbool(A) or bool(B).progress()builds narrower subqueries.Switch (
settings.py)prune_subqueries, set by the environment variableDJ_PRUNE_SUBQUERIESordj.config.override(prune_subqueries=...). It is on by default. Setting it toFalserenders subqueries with all attributes, as before.&is applied.Tests and CI
tests/integration/test_subquery_narrowing.py: 16 nested expressions must give the same result with pruning on and off. The file also covers dropping unused blobs, HAVING aliases, union operations, and aggregationlen/bool.tests/unit/test_settings.py: tests for the setting's default, environment variable and override.test.yaml: a newtest-no-pruningjob runs the suite withDJ_PRUNE_SUBQUERIES=0.Verification
DJ_PRUNE_SUBQUERIES=0.Not in this PR