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