v.21.8Improvement

Add optimize_move_to_prewhere_if_final Setting for FINAL Query Optimization

Add setting optimize_move_to_prewhere_if_final. If query has FINAL, the optimization move_to_prewhere will be enabled only if both optimize_move_to_prewhere and optimize_move_to_prewhere_if_final are enabled. Closes #8684. #25940 (Kseniia Sumarokova).
Introduces the setting optimize_move_to_prewhere_if_final that controls enabling the move_to_prewhere optimization when queries contain the FINAL modifier.

Why it matters

Previously, the move_to_prewhere optimization could interfere with queries using the FINAL modifier. This new setting allows users to enable or disable the optimization specifically for FINAL queries, providing finer control and potentially improving query performance without unexpected side effects.

How to use it

To enable the optimization for queries with FINAL, set both optimize_move_to_prewhere and optimize_move_to_prewhere_if_final to 1 (or true). For example, in your session or configuration:

SET optimize_move_to_prewhere = 1;
SET optimize_move_to_prewhere_if_final = 1;


This ensures that the move_to_prewhere optimization applies only when the query has the FINAL keyword and both settings are enabled.