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