Skip to content
/ server Public

MDEV-38877: Unnecessary filesort on derived table materialization#4722

Open
OmarGamal10 wants to merge 1 commit intoMariaDB:12.3from
OmarGamal10:mdev-38877
Open

MDEV-38877: Unnecessary filesort on derived table materialization#4722
OmarGamal10 wants to merge 1 commit intoMariaDB:12.3from
OmarGamal10:mdev-38877

Conversation

@OmarGamal10
Copy link

@OmarGamal10 OmarGamal10 commented Mar 2, 2026

Why it happens?

The optimizer flags outer references for subqueries as constants, so that for every re-execution for the subquery, index is skipped, as filtering on a constant makes all rows have the same value already.

  • Consider this example
    SELECT * FROM t2 JOIN (SELECT groups_20, MAX(b) FROM t1 GROUP BY groups_20) DT ON t2.a = groups_20;

  • After hours of investigation, I found that the index is bypassed because table->const_key_parts incorrectly flags the [GROUP/ORDER] BY column as a constant. This optimization is correct for Nested Loop / Lateral joins since the subquery is re-executed for each outer row, the join column is a literal constant in this context, making index usage/sorting redundant.
    However, if the optimizer decides to materialize the subquery, the subquery is executed once to build a table. In this context, the column is a variable, not a constant.

    • The constant flag tricks the optimizer into assuming the data is already sorted.
    • The optimizer skips the index.
    • The optimizer finds out that data needs grouping/sorting and is not constant, falling back to a full table scan and a filesort.
  • The fix is a guard condition to prevent treating an outer reference as a constant in case of derived tables.

  • Before

Before
  • After
After

Fixes unnecessary filesort on derived tables when ordered/grouped by a
field in the key. The data is inherently sorted, wrapping the result set
in a filesort is redundant.
@grooverdan grooverdan added the External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements. label Mar 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements.

Development

Successfully merging this pull request may close these issues.

2 participants