MySQL 9.5.0
Source Code Documentation
index_skip_scan_plan.h
Go to the documentation of this file.
1/* Copyright (c) 2000, 2025, 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_RANGE_OPTIMIZER_INDEX_SKIP_SCAN_PLAN_H_
25#define SQL_RANGE_OPTIMIZER_INDEX_SKIP_SCAN_PLAN_H_
26
27#include <sys/types.h>
28
29#include "my_base.h"
31
32class KEY;
33class KEY_PART_INFO;
35class RANGE_OPT_PARAM;
36class SEL_ARG;
37class SEL_ROOT;
38class SEL_TREE;
39struct MEM_ROOT;
40
41/*
42 This is an array of array of equality constants with length
43 eq_prefix_key_parts.
44
45 For example, an equality predicate like "a IN (1, 2) AND b IN (2, 3, 4)",
46 eq_prefixes will contain:
47
48 [
49 { eq_key_prefixes = array[1, 2], cur_eq_prefix = ... },
50 { eq_key_prefixes = array[2, 3, 4], cur_eq_prefix = ... }
51 ]
52 */
53struct EQPrefix {
55
56 /*
57 During skip scan, we will have to iterate through all possible
58 equality prefixes. This is the product of all the elements in
59 eq_prefix_elements. In the above example, there are 2 x 3 = 6 possible
60 equality prefixes.
61
62 To track which prefix we are on, we use cur_eq_prefix. For example,
63 if both EQPrefixes have the value 1 here, it indicates that the current
64 equality prefix is (2, 3).
65 */
66 unsigned cur_eq_prefix;
67};
68
69/**
70 Logically a part of AccessPath::index_skip_scan(), but is too large,
71 so split out into its own struct.
72 */
74 KEY *index_info; ///< The index chosen for data access
75 uint eq_prefix_len; ///< Length of the equality prefix
76 uint eq_prefix_key_parts; ///< Number of key parts in the equality prefix
77 EQPrefix *eq_prefixes; ///< Array of equality constants (IN list)
78 KEY_PART_INFO *range_key_part; ///< The key part matching the range condition
79 uint used_key_parts; ///< Number of index keys used for skip scan
80 double read_cost; ///< Total cost of read
81 uint index; ///< Position of chosen index
82
90
91 // The sub-tree corresponding to the range condition
92 // (on key part C - for more details see description of get_best_skip_scan()).
93 //
94 // Does not necessarily live as long as the AccessPath, so used for tracing
95 // only.
97
98 SEL_ROOT *index_range_tree; ///< The sub-tree corresponding to index_info
99 bool has_aggregate_function; ///< TRUE if there are aggregate functions.
100};
101
102/**
103 Manage cost-related info and cost calculation functions for index skip scans
104*/
106 private:
108 uint m_key;
113
115 uint num_groups{0};
118
119 /**
120 DESCRIPTION
121 This method computes the parameters used to calculate the access cost of
122 an INDEX_SKIP_SCAN access path and the number of rows returned.
123
124 NOTES
125 To estimate the size of the groups to read, index statistics
126 from rec_per_key is used. Each equality range decreases
127 number of the groups to read. The total number of processed
128 records from all the groups will be quick_prefix_records if
129 there are equality ranges else it will be the entire table.
130 Number of distinct group is calculated by dividing the
131 number of processed record by the number keys in a group.
132
133 Number of processed records is calculated using following formula:
134
135 records = number_of_distinct_groups * records_per_group * filtering_effect
136
137 where filtering_effect is filtering effect of the range condition.
138 */
139 void CalcCardinality();
140
141 public:
142 IndexSkipScanCost(TABLE *tab, uint cur_key, uint keyparts,
143 ha_rows prefix_records, Item *where,
144 Opt_trace_object *trace_idx)
145 : m_table(tab),
146 m_key(cur_key),
147 m_distinct_key_parts(keyparts),
148 m_quick_prefix_records(prefix_records),
150 m_trace(trace_idx) {
152 }
153 /**
154 DESCRIPTION
155 This method computes the access cost of an INDEX_SKIP_SCAN access path
156 and the number of rows returned for the old optimizer.
157
158 RETURN
159 Cost estimate
160 */
161 Cost_estimate GetCost() const;
162 /**
163 DESCRIPTION
164 This method computes the access cost of an INDEX_SKIP_SCAN access path
165 and the number of rows returned in hypergraph.
166
167 RETURN
168 Hypergraph cost value.
169 */
170 double GetCostForHypergraph() const;
171
173};
174
176 RANGE_OPT_PARAM *param,
177 SEL_TREE *tree,
178 enum_order order_direction,
179 bool skip_records_in_range,
180 bool force_skip_scan);
182 enum_order order_direction,
183 bool skip_records_in_range,
184 bool force_skip_scan);
185
187 const RANGE_OPT_PARAM *param,
188 Opt_trace_object *trace_object);
189
190#ifndef NDEBUG
191void dbug_dump_index_skip_scan(int indent, bool verbose,
192 const AccessPath *path);
193#endif
194
195#endif // SQL_RANGE_OPTIMIZER_INDEX_SKIP_SCAN_PLAN_H_
A wrapper class which provides array bounds checking.
Definition: sql_array.h:48
Used to store optimizer cost estimates.
Definition: handler.h:4028
Manage cost-related info and cost calculation functions for index skip scans.
Definition: index_skip_scan_plan.h:105
void CalcCardinality()
DESCRIPTION This method computes the parameters used to calculate the access cost of an INDEX_SKIP_SC...
Definition: index_skip_scan_plan.cc:683
Item * m_where_cond
Definition: index_skip_scan_plan.h:111
struct IndexSkipScanCost::IndexSkipScanCardinality m_cardinality
ha_rows GetNumRecords() const
Definition: index_skip_scan_plan.h:172
double GetCostForHypergraph() const
DESCRIPTION This method computes the access cost of an INDEX_SKIP_SCAN access path and the number of ...
Definition: index_skip_scan_plan.cc:779
IndexSkipScanCost(TABLE *tab, uint cur_key, uint keyparts, ha_rows prefix_records, Item *where, Opt_trace_object *trace_idx)
Definition: index_skip_scan_plan.h:142
TABLE * m_table
Definition: index_skip_scan_plan.h:107
Opt_trace_object * m_trace
Definition: index_skip_scan_plan.h:112
ha_rows m_quick_prefix_records
Definition: index_skip_scan_plan.h:110
uint m_key
Definition: index_skip_scan_plan.h:108
Cost_estimate GetCost() const
DESCRIPTION This method computes the access cost of an INDEX_SKIP_SCAN access path and the number of ...
Definition: index_skip_scan_plan.cc:752
uint m_distinct_key_parts
Definition: index_skip_scan_plan.h:109
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:928
Definition: key.h:57
Definition: key.h:113
A typesafe replacement for DYNAMIC_ARRAY.
Definition: mem_root_array.h:432
A JSON object (unordered set of key/value pairs).
Definition: opt_trace.h:802
Definition: range_opt_param.h:29
Definition: tree.h:466
A graph of (possible multiple) key ranges, represented as a red-black binary tree.
Definition: tree.h:69
Definition: tree.h:872
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
void trace_basic_info_index_skip_scan(THD *thd, const AccessPath *path, const RANGE_OPT_PARAM *param, Opt_trace_object *trace_object)
Definition: index_skip_scan_plan.cc:72
void dbug_dump_index_skip_scan(int indent, bool verbose, const AccessPath *path)
Definition: index_skip_scan_plan.cc:790
Mem_root_array< AccessPath * > get_all_skip_scans(THD *thd, RANGE_OPT_PARAM *param, SEL_TREE *tree, enum_order order_direction, bool skip_records_in_range, bool force_skip_scan)
Test if skip scan is applicable and if so, construct a new AccessPath for each candidate index skip s...
Definition: index_skip_scan_plan.cc:138
AccessPath * get_best_skip_scan(THD *thd, RANGE_OPT_PARAM *param, SEL_TREE *tree, enum_order order_direction, bool skip_records_in_range, bool force_skip_scan)
Test if skip scan is applicable and if so, construct a new AccessPath.
Definition: index_skip_scan_plan.cc:207
enum_order
Definition: key_spec.h:65
This file includes constants used by all storage engines.
my_off_t ha_rows
Definition: my_base.h:1217
unsigned char uchar
Definition: my_inttypes.h:52
static uint verbose
Definition: mysqlcheck.cc:66
static char * path
Definition: mysqldump.cc:150
static char * where
Definition: mysqldump.cc:153
Access paths are a query planning structure that correspond 1:1 to iterators, in that an access path ...
Definition: access_path.h:238
Definition: index_skip_scan_plan.h:53
Bounds_checked_array< uchar * > eq_key_prefixes
Definition: index_skip_scan_plan.h:54
unsigned cur_eq_prefix
Definition: index_skip_scan_plan.h:66
Definition: index_skip_scan_plan.h:114
ha_rows records
Definition: index_skip_scan_plan.h:116
uint num_groups
Definition: index_skip_scan_plan.h:115
Logically a part of AccessPath::index_skip_scan(), but is too large, so split out into its own struct...
Definition: index_skip_scan_plan.h:73
double read_cost
Total cost of read.
Definition: index_skip_scan_plan.h:80
uint eq_prefix_len
Length of the equality prefix.
Definition: index_skip_scan_plan.h:75
uchar * min_range_key
Definition: index_skip_scan_plan.h:83
uint range_cond_flag
Definition: index_skip_scan_plan.h:87
KEY * index_info
The index chosen for data access.
Definition: index_skip_scan_plan.h:74
bool has_aggregate_function
TRUE if there are aggregate functions.
Definition: index_skip_scan_plan.h:99
uint num_output_rows
Definition: index_skip_scan_plan.h:89
uint range_key_len
Definition: index_skip_scan_plan.h:88
uchar * max_search_key
Definition: index_skip_scan_plan.h:86
uchar * max_range_key
Definition: index_skip_scan_plan.h:84
SEL_ROOT * index_range_tree
The sub-tree corresponding to index_info.
Definition: index_skip_scan_plan.h:98
uint used_key_parts
Number of index keys used for skip scan.
Definition: index_skip_scan_plan.h:79
uint eq_prefix_key_parts
Number of key parts in the equality prefix.
Definition: index_skip_scan_plan.h:76
uchar * min_search_key
Definition: index_skip_scan_plan.h:85
KEY_PART_INFO * range_key_part
The key part matching the range condition.
Definition: index_skip_scan_plan.h:78
EQPrefix * eq_prefixes
Array of equality constants (IN list)
Definition: index_skip_scan_plan.h:77
const SEL_ARG * range_part_tracing_only
Definition: index_skip_scan_plan.h:96
uint index
Position of chosen index.
Definition: index_skip_scan_plan.h:81
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:83
Definition: table.h:1435