MySQL 8.0.39
Source Code Documentation
group_index_skip_scan.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_RANGE_OPTIMIZER_GROUP_INDEX_SKIP_SCAN_H_
25#define SQL_RANGE_OPTIMIZER_GROUP_INDEX_SKIP_SCAN_H_
26
27#include <sys/types.h>
28
29#include "m_string.h"
30#include "my_alloc.h"
31#include "my_base.h"
32#include "my_bitmap.h"
33#include "my_inttypes.h"
34#include "sql/field.h"
35#include "sql/key.h"
37#include "sql/sql_const.h"
38#include "sql_string.h"
39
40class Cost_estimate;
41class Item_sum;
42class JOIN;
44class SEL_ARG;
45struct TABLE;
46template <class T>
47class List;
48template <class T>
49class List_iterator;
50
51/*
52 Index scan for GROUP-BY queries with MIN/MAX aggregate functions.
53
54 This class provides a specialized index access method for GROUP-BY queries
55 of the forms:
56
57 SELECT A_1,...,A_k, [B_1,...,B_m], [MIN(C)], [MAX(C)]
58 FROM T
59 WHERE [RNG(A_1,...,A_p ; where p <= k)]
60 [AND EQ(B_1,...,B_m)]
61 [AND PC(C)]
62 [AND PA(A_i1,...,A_iq)]
63 GROUP BY A_1,...,A_k;
64
65 or
66
67 SELECT DISTINCT A_i1,...,A_ik
68 FROM T
69 WHERE [RNG(A_1,...,A_p ; where p <= k)]
70 [AND PA(A_i1,...,A_iq)];
71
72 where all selected fields are parts of the same index.
73 The class of queries that can be processed by this quick select is fully
74 specified in the description of get_best_group_min_max().
75
76 The Read() method directly produces result tuples, thus obviating the
77 need to use AggregateIterator, because all grouping is already done inside
78 Read().
79
80 Since one of the requirements is that all select fields are part of the same
81 index, this class produces only index keys, and not complete records.
82*/
83
85 private:
86 uint index; /* Index this quick select uses */
87 KEY *index_info; /* The index chosen for data access */
88 uchar *group_prefix; /* Key prefix consisting of the GROUP fields. */
89 const uint group_prefix_len; /* Length of the group prefix. */
90 uint group_key_parts; /* A number of keyparts in the group prefix */
91 uchar *last_prefix; /* Prefix of the last group for detecting EOF. */
92 bool have_agg_distinct; /* aggregate_function(DISTINCT ...). */
93 bool seen_first_key; /* Denotes whether the first key was retrieved.*/
94 KEY_PART_INFO *min_max_arg_part; /* The keypart of the only argument field */
95 /* of all MIN/MAX functions. */
96 uint min_max_arg_len; /* The length of the MIN/MAX argument field */
97 bool min_max_keypart_asc; /* TRUE if min_max key part is ascending. */
99 // Total length of first used_key_parts parts of the key.
101 // The current infix range position (in key_infix_ranges) used for row
102 // retrieval.
104 // Indicates if all infix ranges have been used to retrieve rows (all ranges
105 // in key_infix_ranges)
107
111
112 const Quick_ranges
113 *min_max_ranges; /* Array of range ptrs for the MIN/MAX field. */
115 *key_infix_ranges; /* Array of key infix range arrays. */
116 uint real_prefix_len; /* Length of key prefix extended with key_infix. */
117 uint real_key_parts; /* A number of keyparts in the above value. */
120 /*
121 Use index scan to get the next different key instead of jumping into it
122 through index read
123 */
127 int next_prefix();
128 int get_next_prefix(uint prefix_length, uint group_key_parts,
129 uchar *cur_prefix);
130 bool append_next_infix();
131 void reset_group();
132 int next_min_in_range();
133 int next_max_in_range();
134 int next_min();
135 int next_max();
136 void update_min_result(bool *reset);
137 void update_max_result(bool *reset);
138
139 public:
146 uint real_key_parts, uint max_used_key_length_arg,
147 KEY *index_info, uint use_index,
148 uint key_infix_len, MEM_ROOT *return_mem_root,
149 bool is_index_scan,
154 bool Init() override;
155 int Read() override;
156 bool is_agg_distinct() const { return have_agg_distinct; }
157};
158
159#endif // SQL_RANGE_OPTIMIZER_GROUP_INDEX_SKIP_SCAN_H_
Used to store optimizer cost estimates.
Definition: handler.h:3706
Definition: group_index_skip_scan.h:84
GroupIndexSkipScanIterator(THD *thd, TABLE *table_arg, const Mem_root_array< Item_sum * > *min_functions, const Mem_root_array< Item_sum * > *max_functions, bool have_agg_distinct, KEY_PART_INFO *min_max_arg_part, uint group_prefix_len, uint group_key_parts, uint real_key_parts, uint max_used_key_length_arg, KEY *index_info, uint use_index, uint key_infix_len, MEM_ROOT *return_mem_root, bool is_index_scan, const Quick_ranges *prefix_ranges, const Quick_ranges_array *key_infix_ranges, const Quick_ranges *min_max_ranges)
Definition: group_index_skip_scan.cc:80
const Quick_ranges * prefix_ranges
Definition: group_index_skip_scan.h:108
const Quick_ranges_array * key_infix_ranges
Definition: group_index_skip_scan.h:115
bool min_max_keypart_asc
Definition: group_index_skip_scan.h:97
int next_prefix()
Definition: group_index_skip_scan.cc:487
bool append_next_infix()
Definition: group_index_skip_scan.cc:597
bool have_agg_distinct
Definition: group_index_skip_scan.h:92
bool seen_first_key
Definition: group_index_skip_scan.h:93
const Mem_root_array< Item_sum * > * min_functions
Definition: group_index_skip_scan.h:118
const uint group_prefix_len
Definition: group_index_skip_scan.h:89
void update_max_result(bool *reset)
Definition: group_index_skip_scan.cc:1047
uint index
Definition: group_index_skip_scan.h:86
int get_next_prefix(uint prefix_length, uint group_key_parts, uchar *cur_prefix)
Definition: group_index_skip_scan.cc:536
uchar * group_prefix
Definition: group_index_skip_scan.h:88
uint min_max_arg_len
Definition: group_index_skip_scan.h:96
bool seen_all_infix_ranges
Definition: group_index_skip_scan.h:106
KEY * index_info
Definition: group_index_skip_scan.h:87
uint max_used_key_length
Definition: group_index_skip_scan.h:100
unsigned cur_prefix_range_idx
Definition: group_index_skip_scan.h:109
const Quick_ranges * min_max_ranges
Definition: group_index_skip_scan.h:113
void reset_group()
Definition: group_index_skip_scan.cc:645
KEY_PART_INFO * min_max_arg_part
Definition: group_index_skip_scan.h:94
uchar * last_prefix
Definition: group_index_skip_scan.h:91
uint real_key_parts
Definition: group_index_skip_scan.h:117
bool Init() override
Initialize or reinitialize the iterator.
Definition: group_index_skip_scan.cc:143
int next_max()
Definition: group_index_skip_scan.cc:441
MEM_ROOT * mem_root
Definition: group_index_skip_scan.h:126
int next_min()
Definition: group_index_skip_scan.cc:358
int next_max_in_range()
Definition: group_index_skip_scan.cc:904
const Mem_root_array< Item_sum * > * max_functions
Definition: group_index_skip_scan.h:119
uint cur_infix_range_position[MAX_REF_PARTS]
Definition: group_index_skip_scan.h:103
uint group_key_parts
Definition: group_index_skip_scan.h:90
bool is_index_scan
Definition: group_index_skip_scan.h:124
int Read() override
Read a single row.
Definition: group_index_skip_scan.cc:220
QUICK_RANGE * last_prefix_range
Definition: group_index_skip_scan.h:110
uint key_infix_len
Definition: group_index_skip_scan.h:98
int next_min_in_range()
Definition: group_index_skip_scan.cc:776
uint real_prefix_len
Definition: group_index_skip_scan.h:116
bool m_seen_eof
Definition: group_index_skip_scan.h:125
~GroupIndexSkipScanIterator() override
Definition: group_index_skip_scan.cc:117
void update_min_result(bool *reset)
Definition: group_index_skip_scan.cc:1013
bool is_agg_distinct() const
Definition: group_index_skip_scan.h:156
Definition: index_range_scan.h:61
Class Item_sum is the base class used for special expressions that SQL calls 'set functions'.
Definition: item_sum.h:399
Definition: sql_optimizer.h:133
Definition: key.h:57
Definition: key.h:113
Definition: sql_list.h:573
Definition: sql_list.h:434
Definition: range_optimizer.h:69
THD * thd() const
Definition: row_iterator.h:228
Definition: tree.h:465
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:34
Definition: row_iterator.h:234
This file follows Google coding style, except for the name MEM_ROOT (which is kept for historical rea...
This file includes constants used by all storage engines.
Some integer typedefs for easier portability.
unsigned char uchar
Definition: my_inttypes.h:52
std::string HARNESS_EXPORT reset()
get 'reset attributes' ESC sequence.
Definition: vt100.cc:37
File containing constants that can be used throughout the server.
constexpr const unsigned int MAX_REF_PARTS
Definition: sql_const.h:47
Our own string classes, used pervasively throughout the executor.
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:83
Definition: table.h:1399
unsigned int uint
Definition: uca9-dump.cc:75