MySQL 8.4.2
Source Code Documentation
|
This module analyzes all equality predicates to determine the best independent ref/eq_ref/ref_or_null index access methods. More...
Classes | |
struct | Key_field |
A Key_field is a descriptor of a predicate of the form (column <op> val). More... | |
Functions | |
Key_field::Key_field (Item_field *item_field, Item *val, uint level, uint optimize, bool eq_func, bool null_rejecting, bool *cond_guard, uint sj_pred_no) | |
bool | add_key_fields (THD *thd, JOIN *join, Key_field **key_fields, uint *and_level, Item *cond, table_map usable_tables, SARGABLE_PARAM **sargables) |
The guts of the ref optimizer. More... | |
Variables | |
Item_field * | Key_field::item_field |
Item representing the column. More... | |
Item * | Key_field::val |
May be empty if diff constant. More... | |
uint | Key_field::level |
uint | Key_field::optimize |
KEY_OPTIMIZE_*. More... | |
bool | Key_field::eq_func |
bool | Key_field::null_rejecting |
If true, the condition this struct represents will not be satisfied when val IS NULL. More... | |
bool * | Key_field::cond_guard |
uint | Key_field::sj_pred_no |
This module analyzes all equality predicates to determine the best independent ref/eq_ref/ref_or_null index access methods.
The 'ref' optimizer determines the columns (and expressions over them) that reference columns in other tables via an equality, and analyzes which keys and key parts can be used for index lookup based on these references. The main outcomes of the 'ref' optimizer are:
Updates in JOIN_TAB:
|
inline |
bool add_key_fields | ( | THD * | thd, |
JOIN * | join, | ||
Key_field ** | key_fields, | ||
uint * | and_level, | ||
Item * | cond, | ||
table_map | usable_tables, | ||
SARGABLE_PARAM ** | sargables | ||
) |
The guts of the ref optimizer.
This function, along with the other add_key_* functions, make up a recursive procedure that analyzes a condition expression (a tree of AND and OR predicates) and does many things.
thd | session context | |
join | The query block involving the condition. | |
[in,out] | key_fields | Start of memory buffer, see below. |
[in,out] | and_level | Current 'and level', see below. |
cond | The conditional expression to analyze. | |
usable_tables | Tables not in this bitmap will not be examined. | |
[in,out] | sargables | End of memory buffer, see below. |
This documentation is the result of reverse engineering and may therefore not capture the full gist of the procedure, but it is known to do the following:
The Key_field objects are marked with something called an 'and_level', which does not correspond to their nesting depth within the expression tree. It is rather a tag to group conjunctions together. For instance, in the conditional expression
two Key_field's are produced, both having an and_level of 0.
In an expression such as
three Key_field's are produced, the first two corresponding to 'a = 0' and 'b = 0', respectively, both with and_level 0. The third one corresponds to 'a = 1' and has an and_level of 1.
A separate function, merge_key_fields() performs ref access validation on the Key_field array on the recursice ascent. If some Key_field's cannot be used for ref access, the key_fields pointer is rolled back. All other modifications to the query plan remain.
bool* Key_field::cond_guard |
bool Key_field::eq_func |
Item_field* Key_field::item_field |
Item representing the column.
uint Key_field::level |
bool Key_field::null_rejecting |
If true, the condition this struct represents will not be satisfied when val IS NULL.
uint Key_field::optimize |
KEY_OPTIMIZE_*.
uint Key_field::sj_pred_no |
Item* Key_field::val |
May be empty if diff constant.