MySQL 8.4.2
Source Code Documentation
sql_optimizer_internal.h
Go to the documentation of this file.
1/* Copyright (c) 2000, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef SQL_OPTIMIZER_INTERNAL_INCLUDED
25#define SQL_OPTIMIZER_INTERNAL_INCLUDED
26
27#include "sql/sql_optimizer.h"
28
29#include "my_inttypes.h"
30#include "sql/item.h"
31
32class JOIN;
33class THD;
34
35struct SARGABLE_PARAM;
36
37/**
38 @defgroup RefOptimizerModule Ref Optimizer
39
40 @{
41
42 This module analyzes all equality predicates to determine the best
43 independent ref/eq_ref/ref_or_null index access methods.
44
45 The 'ref' optimizer determines the columns (and expressions over them) that
46 reference columns in other tables via an equality, and analyzes which keys
47 and key parts can be used for index lookup based on these references. The
48 main outcomes of the 'ref' optimizer are:
49
50 - A bi-directional graph of all equi-join conditions represented as an
51 array of Key_use elements. This array is stored in JOIN::keyuse_array in
52 table, key, keypart order. Each JOIN_TAB::keyuse points to the
53 first Key_use element with the same table as JOIN_TAB::table.
54
55 - The table dependencies needed by the optimizer to determine what
56 tables must be before certain table so that they provide the
57 necessary column bindings for the equality predicates.
58
59 - Computed properties of the equality predicates such as null_rejecting
60 and the result size of each separate condition.
61
62 Updates in JOIN_TAB:
63 - JOIN_TAB::keys Bitmap of all used keys.
64 - JOIN_TAB::const_keys Bitmap of all keys that may be used with quick_select.
65 - JOIN_TAB::keyuse Pointer to possible keys.
66*/
67
68/**
69 A Key_field is a descriptor of a predicate of the form (column @<op@> val).
70 Currently 'op' is one of {'=', '<=>', 'IS [NOT] NULL', 'arg1 IN arg2'},
71 and 'val' can be either another column or an expression (including constants).
72
73 Key_field's are used to analyze columns that may potentially serve as
74 parts of keys for index lookup. If 'field' is part of an index, then
75 add_key_part() creates a corresponding Key_use object and inserts it
76 into the JOIN::keyuse_array which is passed by update_ref_and_keys().
77
78 The structure is used only during analysis of the candidate columns for
79 index 'ref' access.
80*/
81struct Key_field {
83 bool eq_func, bool null_rejecting, bool *cond_guard,
84 uint sj_pred_no)
86 val(val),
87 level(level),
93 Item_field *item_field; ///< Item representing the column
94 Item *val; ///< May be empty if diff constant
95 uint level;
96 uint optimize; ///< KEY_OPTIMIZE_*
97 bool eq_func;
98 /**
99 If true, the condition this struct represents will not be satisfied
100 when val IS NULL.
101 @sa Key_use::null_rejecting .
102 */
104 bool *cond_guard; ///< @sa Key_use::cond_guard
105 uint sj_pred_no; ///< @sa Key_use::sj_pred_no
106};
107
108bool add_key_fields(THD *thd, JOIN *join, Key_field **key_fields,
109 uint *and_level, Item *cond, table_map usable_tables,
110 SARGABLE_PARAM **sargables);
111
112#endif // SQL_OPTIMIZER_INTERNAL_INCLUDED
Definition: item.h:4342
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:936
Definition: sql_optimizer.h:133
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
bool * cond_guard
Definition: sql_optimizer_internal.h:104
uint sj_pred_no
Definition: sql_optimizer_internal.h:105
Item_field * item_field
Item representing the column.
Definition: sql_optimizer_internal.h:93
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)
Definition: sql_optimizer_internal.h:82
uint optimize
KEY_OPTIMIZE_*.
Definition: sql_optimizer_internal.h:96
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.
Definition: sql_optimizer.cc:7474
Item * val
May be empty if diff constant.
Definition: sql_optimizer_internal.h:94
uint level
Definition: sql_optimizer_internal.h:95
bool eq_func
Definition: sql_optimizer_internal.h:97
bool null_rejecting
If true, the condition this struct represents will not be satisfied when val IS NULL.
Definition: sql_optimizer_internal.h:103
Some integer typedefs for easier portability.
uint64_t table_map
Definition: my_table_map.h:30
std::string join(Container cont, const std::string &delim)
join elements of an container into a string separated by a delimiter.
Definition: string.h:151
Classes used for query optimizations.
A Key_field is a descriptor of a predicate of the form (column <op> val).
Definition: sql_optimizer_internal.h:81
Definition: sql_optimizer.h:83