Skip to content

Prune unused columns from nested subqueries - #1671

Open
dimitri-yatsenko wants to merge 1 commit into
nextfrom
perf/subquery-narrowing
Open

dimitri-yatsenko wants to merge 1 commit into
nextfrom
perf/subquery-narrowing

Conversation

@dimitri-yatsenko

Copy link
Copy Markdown
Member

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_sql passes its resolved fields down to from_clause.
  • Aggregation keeps its HAVING aliases. Without GROUP BY it keeps every attribute.
  • Top keeps every column its ORDER BY names.

Always active, whatever the setting

  • Union.make_sql(fields) accepts a field list. It uses UNION ALL when the primary key and secondary attributes are selected; the two branches have disjoint keys, so no row can repeat. Key-only requests use UNION over the keys. This fixes the TypeError raised when restricting by a union or inserting from one.
  • len() on aggregations and unions, and bool() on aggregations, select only the primary key plus any columns HAVING or a Top's ORDER BY needs. bool() on a union is computed as bool(A) or bool(B).
  • progress() builds narrower subqueries.

Switch (settings.py)

  • New setting prune_subqueries, set by the environment variable DJ_PRUNE_SUBQUERIES or dj.config.override(prune_subqueries=...). It is on by default. Setting it to False renders subqueries with all attributes, as before.
  • It takes effect for queries built after the change, because restriction subqueries are rendered when & is applied.
  • The plan is to keep the switch for one release as a fallback and then remove it.

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 aggregation len/bool.
  • tests/unit/test_settings.py: tests for the setting's default, environment variable and override.
  • test.yaml: a new test-no-pruning job runs the suite with DJ_PRUNE_SUBQUERIES=0.

Verification

  • The full suite passes both ways: 1296 passed and 14 skipped with pruning on (the default), and again with DJ_PRUNE_SUBQUERIES=0.
  • pre-commit passes (ruff, mypy, codespell, actionlint).

Not in this PR

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

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Indicates new improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Subquery pruning: drop unused columns from nested subqueries (benchmark report and proposal)

1 participant