Skip to content

MDEV-40439 Evaluate cheap subconditions before expensive ones in WHER… - #6241

Open
Olernov wants to merge 1 commit into
11.8from
11.8-MDEV-40439-cond-rearr
Open

Olernov wants to merge 1 commit into
11.8from
11.8-MDEV-40439-cond-rearr

Conversation

@Olernov

@Olernov Olernov commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

…E and ON conditions

The arguments of AND and OR are evaluated left to right with short-circuiting, in the order written in the query. An expensive subcondition written first is therefore evaluated even for rows that a cheap one would reject (AND) or accept (OR).

Reorder the arguments of each AND/OR so that the expensive ones come after the cheap ones, keeping the relative order within each group. The walk goes down into nested AND/OR and into trigger conditions. A subcondition is expensive if it contains a subquery, a stored function call, or an item that marks itself as expensive through is_expensive_processor() (a UDF, MATCH()).

An AND/OR that has a non-deterministic argument (one with RAND_TABLE_BIT) keeps its order, to prevent unwanted change of behavior. Nested lists are still reordered.

The reordering is applied to:

  • the conditions attached to tables (select_cond, select->cond, on_precond), at the end of make_join_select() and before the attached_conditions_summary trace, so the trace shows the final order;
  • BNL/BNLH scan filters, in make_scan_filter(). They are built from the original WHERE/ON, and check_join_cache_usage() may build them after make_join_select();
  • outer_ref_cond, the condition a subquery checks once per execution before running its join. The steps that later build new conditions from these (index condition pushdown, removal of duplicate BNL conditions) take subsets and keep the order.

The fix is OFF by default and enabled by setting REORDER_EXPENSIVE_CONDS flag in @@new_mode.

Not covered yet: HAVING, exec_const_cond, terms added after make_join_select() (the index condition remainder rejected by an engine, HAVING moved to a table's condition). Reordering HAVING would need care with Item_is_not_null_test and Item_ref_null_helper, which record NULLs for IN->EXISTS.

…E and ON conditions

The arguments of AND and OR are evaluated left to right with
short-circuiting, in the order written in the query. An expensive
subcondition written first is therefore evaluated even for rows that
a cheap one would reject (AND) or accept (OR).

Reorder the arguments of each AND/OR so that the expensive ones come
after the cheap ones, keeping the relative order within each group.
The walk goes down into nested AND/OR and into trigger conditions.
A subcondition is expensive if it contains a subquery, a stored
function call, or an item that marks itself as expensive through
is_expensive_processor() (a UDF, MATCH()).

An AND/OR that has a non-deterministic argument (one with RAND_TABLE_BIT)
keeps its order, to prevent unwanted change of behavior. Nested lists
are still reordered.

The reordering is applied to:
- the conditions attached to tables (select_cond, select->cond,
  on_precond), at the end of make_join_select() and before the
  attached_conditions_summary trace, so the trace shows the final
  order;
- BNL/BNLH scan filters, in make_scan_filter(). They are built from
  the original WHERE/ON, and check_join_cache_usage() may build them
  after make_join_select();
- outer_ref_cond, the condition a subquery checks once per execution
  before running its join.
The steps that later build new conditions from these (index condition
pushdown, removal of duplicate BNL conditions) take subsets and keep
the order.

The fix is OFF by default and enabled by setting REORDER_EXPENSIVE_CONDS
flag in @@new_mode.

Not covered yet: HAVING, exec_const_cond, terms added after
make_join_select() (the index condition remainder rejected by an
engine, HAVING moved to a table's condition). Reordering HAVING would
need care with Item_is_not_null_test and Item_ref_null_helper, which
record NULLs for IN->EXISTS.
@Olernov

Olernov commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

This feature is backported to 11.8 from #6066 and protected with @new_mode flag.

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

Development

Successfully merging this pull request may close these issues.

1 participant