MySQL 8.4.0
Source Code Documentation
opt_costmodel.h
Go to the documentation of this file.
1#ifndef OPT_COSTMODEL_INCLUDED
2#define OPT_COSTMODEL_INCLUDED
3
4/*
5 Copyright (c) 2014, 2024, Oracle and/or its affiliates.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License, version 2.0,
9 as published by the Free Software Foundation.
10
11 This program is designed to work with certain software (including
12 but not limited to OpenSSL) that is licensed under separate terms,
13 as designated in a particular file or component or in included license
14 documentation. The authors of MySQL hereby grant you an additional
15 permission to link the program and your derivative works with the
16 separately licensed software that they have either included with
17 the program or referenced in the documentation.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License, version 2.0, for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
27
28#include <assert.h>
29#include <stddef.h>
30#include <sys/types.h>
31
33
34/*
35 For sequential disk seeks the cost formula is:
36 DISK_SEEK_BASE_COST + DISK_SEEK_PROP_COST * #blocks_to_skip
37
38 The cost of average seek
39 DISK_SEEK_BASE_COST + DISK_SEEK_PROP_COST*BLOCKS_IN_AVG_SEEK =1.0.
40
41 The methods using these constants scale them by IO_BLOCK_READ_COST.
42*/
43constexpr const double DISK_SEEK_BASE_COST{0.9};
44constexpr const int BLOCKS_IN_AVG_SEEK{128};
45constexpr const double DISK_SEEK_PROP_COST{0.1 / BLOCKS_IN_AVG_SEEK};
46
47struct TABLE;
48
49/**
50 API for getting cost estimates for server operations that are not
51 directly related to a table object.
52*/
53
55 public:
56 /**
57 Temporary table types that the cost model differentiate between.
58 */
60
63#if !defined(NDEBUG)
64 m_initialized = false;
65#endif
66 }
67
68 /**
69 Destructor for Cost_model_server objects.
70
71 @note This is declared virtual in order to make it easier to implement
72 stubs for this class for use in unit tests.
73 */
74
75 virtual ~Cost_model_server();
76
77 /**
78 Initialize the cost model object for a query.
79
80 This function must be called before calling any cost estimation
81 functions for a query. It should also be called when starting
82 optimization of a new query in case any cost estimate constants
83 have changed.
84
85 @param optimizer The type of optimizer to initialize the cost model for.
86 */
87 void init(Optimizer optimizer);
88
89 /**
90 Cost of processing a number of records and evaluating the query condition
91 on the records.
92
93 @param rows number of rows to evaluate
94
95 @return Cost of evaluating the records
96 */
97
98 double row_evaluate_cost(double rows) const {
99 assert(m_initialized);
100 assert(rows >= 0.0);
102 }
103
104 /**
105 Cost of doing a number of key compare operations.
106
107 @param keys number of key compare operations
108
109 @return Cost of comparing the keys
110 */
111
112 double key_compare_cost(double keys) const {
113 assert(m_initialized);
114 assert(keys >= 0.0);
115
117 }
118
119 private:
120 /**
121 Cost of creating a temporary table in the memory storage engine.
122 */
123
126 }
127
128 /**
129 Cost of storing or retrieving a row using the memory storage engine.
130 */
131
134 }
135
136 /**
137 Cost of creating a temporary table using a disk based storage engine.
138 */
139
142 }
143
144 /**
145 Cost of storing or retrieving a row using a disk based storage engine.
146 */
147
148 double disk_tmptable_row_cost() const {
150 }
151
152 /**
153 Cost estimate for a row operation (insert, read) on a temporary table.
154
155 @param tmptable_type storage type for the temporary table
156
157 @return The estimated cost
158 */
159
160 double tmptable_row_cost(enum_tmptable_type tmptable_type) const {
161 if (tmptable_type == MEMORY_TMPTABLE) return memory_tmptable_row_cost();
162 return disk_tmptable_row_cost();
163 }
164
165 public:
166 /**
167 Cost estimate for creating a temporary table.
168
169 @param tmptable_type storage type for the temporary table
170
171 @return Cost estimate
172 */
173
174 double tmptable_create_cost(enum_tmptable_type tmptable_type) const {
175 assert(m_initialized);
176
177 if (tmptable_type == MEMORY_TMPTABLE) return memory_tmptable_create_cost();
179 }
180
181 /**
182 Cost estimate for inserting and reading records from a
183 temporary table.
184
185 @param tmptable_type storage type for the temporary table
186 @param write_rows number of rows that will be written to table
187 @param read_rows number of rows that will be read from table
188
189 @return The estimated cost
190 */
191
193 double write_rows, double read_rows) const {
194 assert(m_initialized);
195 assert(write_rows >= 0.0);
196 assert(read_rows >= 0.0);
197
198 return (write_rows + read_rows) * tmptable_row_cost(tmptable_type);
199 }
200
201 protected:
202 friend class Cost_model_table;
203 /**
204 Return a pointer to the object containing the current cost constants.
205
206 @return Cost constants
207 */
208
210 assert(m_initialized);
211
212 return m_cost_constants;
213 }
214
215 private:
216 /// Cost constants to use in cost calculations
218
219 protected: // To be able make a gunit fake sub class
220 /*
221 Cost constants for the server operations. The purpose for this is
222 to have direct access to these instead of having to go through the
223 cost constant set each time these are needed.
224 */
226
227#if !defined(NDEBUG)
228 /**
229 Used for detecting if this object is used without having been initialized.
230 */
232#endif
233};
234
235/**
236 API for getting cost estimates for operations on table data.
237
238 @note The initial implementation mostly has functions for accessing
239 cost constants for basic operations.
240*/
241
243 public:
248#if !defined(NDEBUG)
249 m_initialized = false;
250#endif
251 }
252
253 /**
254 Initializes the cost model object.
255
256 This function must be called before calling any cost estimation
257 functions for a query. It should also be called when starting
258 optimization of a new query in case any cost estimate constants
259 have changed.
260
261 @param cost_model_server the main cost model object for this query
262 @param table the table the cost model should be used for
263 */
264
265 void init(const Cost_model_server *cost_model_server, const TABLE *table);
266
267 /**
268 Cost of processing a number of records and evaluating the query condition
269 on the records.
270
271 @param rows number of rows to evaluate
272
273 @return Cost of evaluating the records
274 */
275
276 double row_evaluate_cost(double rows) const {
277 assert(m_initialized);
278 assert(rows >= 0.0);
279
281 }
282
283 /**
284 Cost of doing a number of key compare operations.
285
286 @param keys number of key compare operations
287
288 @return Cost of comparing the keys
289 */
290
291 double key_compare_cost(double keys) const {
292 assert(m_initialized);
293 assert(keys >= 0.0);
294
296 }
297
298 /**
299 Cost of reading a number of random blocks from a table.
300
301 @param blocks number of blocks to read
302
303 @return Cost estimate
304 */
305
306 double io_block_read_cost(double blocks) const {
307 assert(m_initialized);
308 assert(blocks >= 0.0);
309
310 return blocks * m_se_cost_constants->io_block_read_cost();
311 }
312
313 /**
314 Cost of reading a number of blocks from the storage engine when the
315 block is already in a memory buffer
316
317 @param blocks number of blocks to read
318
319 @return Cost estimate
320 */
321
322 double buffer_block_read_cost(double blocks) const {
323 assert(m_initialized);
324 assert(blocks >= 0.0);
325
327 }
328
329 /**
330 Cost of reading a number of random pages from a table.
331
332 @param pages number of pages to read
333
334 @return Cost estimate
335 */
336
337 double page_read_cost(double pages) const;
338
339 /**
340 Cost of reading a number of random pages from an index.
341
342 @param index the index number
343 @param pages number of pages to read
344
345 @return Cost estimate
346 */
347
348 double page_read_cost_index(uint index, double pages) const;
349
350 /**
351 The fixed part of the cost for doing a sequential seek on disk.
352
353 For a harddisk, this corresponds to half a rotation (see comment
354 for get_sweep_read_cost() in handler.cc).
355 */
356
357 double disk_seek_base_cost() const {
358 assert(m_initialized);
359
361 }
362
363 private:
364 /**
365 The cost for seeking past one block in a sequential seek.
366
367 For a harddisk, this represents the cost of having to move the
368 disk head to the correct cylinder.
369
370 @todo Check that the BLOCKS_IN_AV_SEEK is correct to include in the
371 DISK_SEEK_PROP_COST
372 */
373
374 double disk_seek_prop_cost() const {
376 }
377
378 public:
379 /**
380 Cost estimate for a sequential disk seek where a given number of blocks
381 are skipped.
382
383 @param seek_blocks number of blocks to seek past
384
385 @return The cost estimate for the seek operation
386 */
387
388 double disk_seek_cost(double seek_blocks) const {
389 assert(seek_blocks >= 0.0);
390 assert(m_initialized);
391
392 const double cost =
393 disk_seek_base_cost() + disk_seek_prop_cost() * seek_blocks;
394 return cost;
395 }
396
397 protected: // To be able make a gunit fake sub class
398 /**
399 Pointer to the cost model for the query. This is used for getting
400 cost estimates for server operations.
401 */
403
404 /**
405 Cost constants for the storage engine that stores the table.
406 */
408
409#if !defined(NDEBUG)
410 /**
411 Used for detecting if this object is used without having been initialized.
412 */
414#endif
415
416 private:
417 /// The table that this is the cost model for
419};
420
421#endif /* OPT_COSTMODEL_INCLUDED */
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:251
Set of all cost constants used by the server and all storage engines.
Definition: opt_costconstants.h:424
API for getting cost estimates for server operations that are not directly related to a table object.
Definition: opt_costmodel.h:54
double disk_tmptable_create_cost() const
Cost of creating a temporary table using a disk based storage engine.
Definition: opt_costmodel.h:140
enum_tmptable_type
Temporary table types that the cost model differentiate between.
Definition: opt_costmodel.h:59
@ DISK_TMPTABLE
Definition: opt_costmodel.h:59
@ MEMORY_TMPTABLE
Definition: opt_costmodel.h:59
void init(Optimizer optimizer)
Initialize the cost model object for a query.
Definition: opt_costmodel.cc:45
bool m_initialized
Used for detecting if this object is used without having been initialized.
Definition: opt_costmodel.h:231
double tmptable_create_cost(enum_tmptable_type tmptable_type) const
Cost estimate for creating a temporary table.
Definition: opt_costmodel.h:174
virtual ~Cost_model_server()
Destructor for Cost_model_server objects.
Definition: opt_costmodel.cc:36
Cost_model_server()
Definition: opt_costmodel.h:61
double key_compare_cost(double keys) const
Cost of doing a number of key compare operations.
Definition: opt_costmodel.h:112
double disk_tmptable_row_cost() const
Cost of storing or retrieving a row using a disk based storage engine.
Definition: opt_costmodel.h:148
double row_evaluate_cost(double rows) const
Cost of processing a number of records and evaluating the query condition on the records.
Definition: opt_costmodel.h:98
double memory_tmptable_create_cost() const
Cost of creating a temporary table in the memory storage engine.
Definition: opt_costmodel.h:124
double tmptable_readwrite_cost(enum_tmptable_type tmptable_type, double write_rows, double read_rows) const
Cost estimate for inserting and reading records from a temporary table.
Definition: opt_costmodel.h:192
const Server_cost_constants * m_server_cost_constants
Definition: opt_costmodel.h:225
double memory_tmptable_row_cost() const
Cost of storing or retrieving a row using the memory storage engine.
Definition: opt_costmodel.h:132
const Cost_model_constants * m_cost_constants
Cost constants to use in cost calculations.
Definition: opt_costmodel.h:217
const Cost_model_constants * get_cost_constants() const
Return a pointer to the object containing the current cost constants.
Definition: opt_costmodel.h:209
double tmptable_row_cost(enum_tmptable_type tmptable_type) const
Cost estimate for a row operation (insert, read) on a temporary table.
Definition: opt_costmodel.h:160
API for getting cost estimates for operations on table data.
Definition: opt_costmodel.h:242
const TABLE * m_table
The table that this is the cost model for.
Definition: opt_costmodel.h:418
double io_block_read_cost(double blocks) const
Cost of reading a number of random blocks from a table.
Definition: opt_costmodel.h:306
const Cost_model_server * m_cost_model_server
Pointer to the cost model for the query.
Definition: opt_costmodel.h:402
double buffer_block_read_cost(double blocks) const
Cost of reading a number of blocks from the storage engine when the block is already in a memory buff...
Definition: opt_costmodel.h:322
double page_read_cost(double pages) const
Cost of reading a number of random pages from a table.
Definition: opt_costmodel.cc:86
double page_read_cost_index(uint index, double pages) const
Cost of reading a number of random pages from an index.
Definition: opt_costmodel.cc:102
const SE_cost_constants * m_se_cost_constants
Cost constants for the storage engine that stores the table.
Definition: opt_costmodel.h:407
Cost_model_table()
Definition: opt_costmodel.h:244
double disk_seek_base_cost() const
The fixed part of the cost for doing a sequential seek on disk.
Definition: opt_costmodel.h:357
double row_evaluate_cost(double rows) const
Cost of processing a number of records and evaluating the query condition on the records.
Definition: opt_costmodel.h:276
void init(const Cost_model_server *cost_model_server, const TABLE *table)
Initializes the cost model object.
Definition: opt_costmodel.cc:68
bool m_initialized
Used for detecting if this object is used without having been initialized.
Definition: opt_costmodel.h:413
double disk_seek_cost(double seek_blocks) const
Cost estimate for a sequential disk seek where a given number of blocks are skipped.
Definition: opt_costmodel.h:388
double disk_seek_prop_cost() const
The cost for seeking past one block in a sequential seek.
Definition: opt_costmodel.h:374
double key_compare_cost(double keys) const
Cost of doing a number of key compare operations.
Definition: opt_costmodel.h:291
Cost constants for a storage engine.
Definition: opt_costconstants.h:202
double memory_block_read_cost() const
Cost of reading one random block from an in-memory database buffer.
Definition: opt_costconstants.h:237
double io_block_read_cost() const
Cost of reading one random block from disk.
Definition: opt_costconstants.h:243
Cost constants for operations done by the server.
Definition: opt_costconstants.h:65
double row_evaluate_cost() const
Cost for evaluating the query condition on a row.
Definition: opt_costconstants.h:103
double disk_temptable_row_cost() const
Cost for retrieving or storing a row in an internal disk resident temporary table.
Definition: opt_costconstants.h:137
double memory_temptable_create_cost() const
Cost for creating an internal temporary table in memory.
Definition: opt_costconstants.h:113
double memory_temptable_row_cost() const
Cost for retrieving or storing a row in an internal temporary table stored in memory.
Definition: opt_costconstants.h:121
double disk_temptable_create_cost() const
Cost for creating an internal temporary table in a disk resident storage engine.
Definition: opt_costconstants.h:129
double key_compare_cost() const
Cost for comparing two keys.
Definition: opt_costconstants.h:108
static uint keys
Definition: hp_test2.cc:49
static PFS_engine_table_share_proxy table
Definition: pfs.cc:61
Optimizer
Definition: opt_costconstants.h:38
constexpr const double DISK_SEEK_BASE_COST
Definition: opt_costmodel.h:43
constexpr const double DISK_SEEK_PROP_COST
Definition: opt_costmodel.h:45
constexpr const int BLOCKS_IN_AVG_SEEK
Definition: opt_costmodel.h:44
Definition: table.h:1405