MySQL 9.7.4
Source Code Documentation
row0pread-histogram.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2019, 2026, Oracle and/or its affiliates.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is designed to work with certain software (including
10but not limited to OpenSSL) that is licensed under separate terms,
11as designated in a particular file or component or in included license
12documentation. The authors of MySQL hereby grant you an additional
13permission to link the program and your derivative works with the
14separately licensed software that they have either included with
15the program or referenced in the documentation.
16
17This program is distributed in the hope that it will be useful, but WITHOUT
18ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20for more details.
21
22You should have received a copy of the GNU General Public License along with
23this program; if not, write to the Free Software Foundation, Inc.,
2451 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26*****************************************************************************/
27
28/** @file include/row0pread-histogram.h
29Parallel read histogram interface.
30
31Created 2019-04-20 by Darshan M N. */
32
33#ifndef row0pread_histogram_h
34#define row0pread_histogram_h
35
36#include <random>
37#include "row0pread.h"
38#include "ut0counter.h"
39
41 public:
42 /** Constructor.
43 @param[in] max_threads Maximum number of threads to use.
44 @param[in] sampling_seed seed to be used for sampling
45 @param[in] sampling_percentage percentage of sampling that needs to be done
46 @param[in] sampling_method sampling method to be used for sampling */
47 explicit Histogram_sampler(size_t max_threads, int sampling_seed,
48 double sampling_percentage,
49 enum_sampling_method sampling_method);
50
51 /** Destructor. */
53
54 /** Initialize the sampler context.
55 @param[in] trx Transaction used for parallel read.
56 @param[in] index clustered index.
57 @param[in] prebuilt prebuilt info
58 @retval true on success. */
59 bool init(trx_t *trx, dict_index_t *index, row_prebuilt_t *prebuilt);
60
61 /** Buffer next row.
62 @return error code */
64
65 /** End parallel read in case the reader thread is still active and wait for
66 its exit. This can happen if we're ending sampling prematurely. */
67 void buffer_end();
68
69 /** Set the buffer.
70 @param[in] buf buffer to be used to store the row converted to MySQL
71 format. */
72 void set(byte *buf) { m_buf = buf; }
73
74 /** Start the sampling process.
75 @return DB_SUCCESS or error code. */
76 dberr_t run();
77
78 /** @return the number of global parallel-read slots reserved. */
79 [[nodiscard]] size_t max_threads() const {
81 }
82
83 /** Check if the processing of the record needs to be skipped.
84 In case of record belonging to non-leaf page, we decide if the child page
85 pertaining to the record needs to be skipped.
86 In case of record belonging to leaf page, we read the page regardless.
87 @return true if it needs to be skipped, else false. */
88 bool skip();
89
90 private:
91 /** Wait till there is a request to buffer the next row. */
93
94 /** Wait till the buffering of the row is complete. */
96
97 /** Signal that the next row needs to be buffered. */
99
100 /** Signal that the buffering of the row is complete. */
102
103 /** Set the error state.
104 @param[in] err Error state to set to. */
106
107 /** @return true if in error state. */
108 [[nodiscard]] bool is_error_set() const { return (m_err != DB_SUCCESS); }
109
110 /** Each parallel reader thread's init function.
111 @param[in] reader_thread_ctx context information related to the thread
112 @return DB_SUCCESS or error code. */
113 [[nodiscard]] dberr_t start_callback(
114 Parallel_reader::Thread_ctx *reader_thread_ctx);
115
116 /** Each parallel reader thread's end function.
117 @param[in] reader_thread_ctx context information related to the thread
118 @return DB_SUCCESS or error code. */
119 [[nodiscard]] dberr_t finish_callback(
120 Parallel_reader::Thread_ctx *reader_thread_ctx);
121
122 /** Convert the row in InnoDB format to MySQL format and store in the buffer
123 for server to use.
124 @param[in] ctx Parallel read context.
125 @param[in] rec record that needs to be converted
126 @param[in] offsets offsets belonging to the record
127 @param[in] index index of the record
128 @param[in] prebuilt Row meta-data cache.
129 @return DB_SUCCESS or error code. */
130 dberr_t sample_rec(const Parallel_reader::Ctx *ctx, const rec_t *rec,
131 ulint *offsets, const dict_index_t *index,
132 row_prebuilt_t *prebuilt);
133
134 /** For each record in a non-leaf block at level 1 (if leaf level is 0)
135 check if the child page needs to be sampled and if so sample all the rows in
136 the child page.
137 @param[in] ctx Parallel read context.
138 @param[in] prebuilt Row meta-data cache.
139 @return error code */
140 [[nodiscard]] dberr_t process_non_leaf_rec(const Parallel_reader::Ctx *ctx,
141 row_prebuilt_t *prebuilt);
142
143 /** Process the record in the leaf page. This would happen only when the root
144 page is the leaf page and in such a case we process the page regardless of
145 the sampling percentage.
146 @param[in] ctx Parallel read context.
147 @param[in] prebuilt Row meta-data cache.
148 @return error code */
149 [[nodiscard]] dberr_t process_leaf_rec(const Parallel_reader::Ctx *ctx,
150 row_prebuilt_t *prebuilt);
151
152 private:
153 /** Buffer to store the sampled row which is in the MySQL format. */
154 byte *m_buf{nullptr};
155
156 /** Event to notify if the next row needs to be buffered. */
158
159 /** Event to notify if the next row has been buffered. */
161
162 /** Error code when the row was buffered. */
164
165 /** The parallel reader. */
167
168 /** Random generator engine used to provide us random uniformly distributed
169 values required to decide if the row in question needs to be sampled or
170 not. */
171 std::mt19937 m_random_generator;
172
173 /** Uniform distribution used by the random generator. */
174 static std::uniform_real_distribution<double> m_distribution;
175
176 /** Sampling method to be used for sampling. */
178
179 /** Sampling percentage to be used for sampling */
181
182 /** Sampling seed to be used for sampling */
184
185 /** Number of rows sampled */
186 std::atomic_size_t m_n_sampled;
187};
188
189#endif /* !row0pread_histogram_h */
Definition: row0pread-histogram.h:40
std::atomic_size_t m_n_sampled
Number of rows sampled.
Definition: row0pread-histogram.h:186
dberr_t process_leaf_rec(const Parallel_reader::Ctx *ctx, row_prebuilt_t *prebuilt)
Process the record in the leaf page.
Definition: row0pread-histogram.cc:362
int m_sampling_seed
Sampling seed to be used for sampling.
Definition: row0pread-histogram.h:183
size_t max_threads() const
Definition: row0pread-histogram.h:79
os_event_t m_end_buffer_event
Event to notify if the next row has been buffered.
Definition: row0pread-histogram.h:160
bool skip()
Check if the processing of the record needs to be skipped.
Definition: row0pread-histogram.cc:192
void set(byte *buf)
Set the buffer.
Definition: row0pread-histogram.h:72
Parallel_reader m_parallel_reader
The parallel reader.
Definition: row0pread-histogram.h:166
enum_sampling_method m_sampling_method
Sampling method to be used for sampling.
Definition: row0pread-histogram.h:177
void wait_for_end_of_buffering()
Wait till the buffering of the row is complete.
Definition: row0pread-histogram.cc:178
os_event_t m_start_buffer_event
Event to notify if the next row needs to be buffered.
Definition: row0pread-histogram.h:157
static std::uniform_real_distribution< double > m_distribution
Uniform distribution used by the random generator.
Definition: row0pread-histogram.h:174
dberr_t process_non_leaf_rec(const Parallel_reader::Ctx *ctx, row_prebuilt_t *prebuilt)
For each record in a non-leaf block at level 1 (if leaf level is 0) check if the child page needs to ...
Definition: row0pread-histogram.cc:286
double m_sampling_percentage
Sampling percentage to be used for sampling.
Definition: row0pread-histogram.h:180
dberr_t m_err
Error code when the row was buffered.
Definition: row0pread-histogram.h:163
bool is_error_set() const
Definition: row0pread-histogram.h:108
dberr_t run()
Start the sampling process.
Definition: row0pread-histogram.cc:250
dberr_t finish_callback(Parallel_reader::Thread_ctx *reader_thread_ctx)
Each parallel reader thread's end function.
Definition: row0pread-histogram.cc:115
void signal_start_of_buffering()
Signal that the next row needs to be buffered.
Definition: row0pread-histogram.cc:184
dberr_t sample_rec(const Parallel_reader::Ctx *ctx, const rec_t *rec, ulint *offsets, const dict_index_t *index, row_prebuilt_t *prebuilt)
Convert the row in InnoDB format to MySQL format and store in the buffer for server to use.
Definition: row0pread-histogram.cc:254
bool init(trx_t *trx, dict_index_t *index, row_prebuilt_t *prebuilt)
Initialize the sampler context.
Definition: row0pread-histogram.cc:141
byte * m_buf
Buffer to store the sampled row which is in the MySQL format.
Definition: row0pread-histogram.h:154
dberr_t start_callback(Parallel_reader::Thread_ctx *reader_thread_ctx)
Each parallel reader thread's init function.
Definition: row0pread-histogram.cc:97
Histogram_sampler(size_t max_threads, int sampling_seed, double sampling_percentage, enum_sampling_method sampling_method)
Constructor.
Definition: row0pread-histogram.cc:45
void set_error_state(dberr_t err)
Set the error state.
Definition: row0pread-histogram.h:105
std::mt19937 m_random_generator
Random generator engine used to provide us random uniformly distributed values required to decide if ...
Definition: row0pread-histogram.h:171
void buffer_end()
End parallel read in case the reader thread is still active and wait for its exit.
Definition: row0pread-histogram.cc:240
~Histogram_sampler()
Destructor.
Definition: row0pread-histogram.cc:90
dberr_t buffer_next()
Buffer next row.
Definition: row0pread-histogram.cc:222
void wait_for_start_of_buffering()
Wait till there is a request to buffer the next row.
Definition: row0pread-histogram.cc:172
void signal_end_of_buffering()
Signal that the buffering of the row is complete.
Definition: row0pread-histogram.cc:188
Parallel reader execution context.
Definition: row0pread.h:693
The core idea is to find the left and right paths down the B+Tree.These paths correspond to the scan ...
Definition: row0pread.h:101
size_t max_threads() const
Definition: row0pread.h:344
static char buf[MAX_BUF]
Definition: conf_to_src.cc:74
dberr_t
Definition: db0err.h:39
@ DB_SUCCESS
Definition: db0err.h:43
Definition: buf0block_hint.cc:30
bool index(const std::string &value, const String &search_for, uint32_t *idx)
Definition: contains.h:76
static Value err()
Create a Value object that represents an error condition.
Definition: json_binary.cc:943
byte rec_t
Definition: rem0types.h:41
Parallel read interface.
enum_sampling_method
Definition: handler.h:723
Thread related context information.
Definition: row0pread.h:215
Data structure for an index.
Definition: dict0mem.h:1067
InnoDB condition variable.
Definition: os0event.cc:63
A struct for (sometimes lazily) prebuilt structures in an Innobase table handle used within MySQL; th...
Definition: row0mysql.h:515
Definition: trx0trx.h:675
unsigned long int ulint
Definition: univ.i:406
Counter utility class.