MySQL 8.0.39
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*/
41constexpr const double DISK_SEEK_BASE_COST{0.9};
42constexpr const int BLOCKS_IN_AVG_SEEK{128};
43constexpr const double DISK_SEEK_PROP_COST{0.1 / BLOCKS_IN_AVG_SEEK};
44
45struct TABLE;
46
47/**
48 API for getting cost estimates for server operations that are not
49 directly related to a table object.
50*/
51
53 public:
54 /**
55 Temporary table types that the cost model differentiate between.
56 */
58
61#if !defined(NDEBUG)
62 m_initialized = false;
63#endif
64 }
65
66 /**
67 Destructor for Cost_model_server objects.
68
69 @note This is declared virtual in order to make it easier to implement
70 stubs for this class for use in unit tests.
71 */
72
73 virtual ~Cost_model_server();
74
75 /**
76 Initialize the cost model object for a query.
77
78 This function must be called before calling any cost estimation
79 functions for a query. It should also be called when starting
80 optimization of a new query in case any cost estimate constants
81 have changed.
82 */
83
84 void init();
85
86 /**
87 Cost of processing a number of records and evaluating the query condition
88 on the records.
89
90 @param rows number of rows to evaluate
91
92 @return Cost of evaluating the records
93 */
94
95 double row_evaluate_cost(double rows) const {
96 assert(m_initialized);
97 assert(rows >= 0.0);
98
100 }
101
102 /**
103 Cost of doing a number of key compare operations.
104
105 @param keys number of key compare operations
106
107 @return Cost of comparing the keys
108 */
109
110 double key_compare_cost(double keys) const {
111 assert(m_initialized);
112 assert(keys >= 0.0);
113
115 }
116
117 private:
118 /**
119 Cost of creating a temporary table in the memory storage engine.
120 */
121
124 }
125
126 /**
127 Cost of storing or retrieving a row using the memory storage engine.
128 */
129
132 }
133
134 /**
135 Cost of creating a temporary table using a disk based storage engine.
136 */
137
140 }
141
142 /**
143 Cost of storing or retrieving a row using a disk based storage engine.
144 */
145
146 double disk_tmptable_row_cost() const {
148 }
149
150 /**
151 Cost estimate for a row operation (insert, read) on a temporary table.
152
153 @param tmptable_type storage type for the temporary table
154
155 @return The estimated cost
156 */
157
158 double tmptable_row_cost(enum_tmptable_type tmptable_type) const {
159 if (tmptable_type == MEMORY_TMPTABLE) return memory_tmptable_row_cost();
160 return disk_tmptable_row_cost();
161 }
162
163 public:
164 /**
165 Cost estimate for creating a temporary table.
166
167 @param tmptable_type storage type for the temporary table
168
169 @return Cost estimate
170 */
171
172 double tmptable_create_cost(enum_tmptable_type tmptable_type) const {
173 assert(m_initialized);
174
175 if (tmptable_type == MEMORY_TMPTABLE) return memory_tmptable_create_cost();
177 }
178
179 /**
180 Cost estimate for inserting and reading records from a
181 temporary table.
182
183 @param tmptable_type storage type for the temporary table
184 @param write_rows number of rows that will be written to table
185 @param read_rows number of rows that will be read from table
186
187 @return The estimated cost
188 */
189
191 double write_rows, double read_rows) const {
192 assert(m_initialized);
193 assert(write_rows >= 0.0);
194 assert(read_rows >= 0.0);
195
196 return (write_rows + read_rows) * tmptable_row_cost(tmptable_type);
197 }
198
199 protected:
200 friend class Cost_model_table;
201 /**
202 Return a pointer to the object containing the current cost constants.
203
204 @return Cost constants
205 */
206
208 assert(m_initialized);
209
210 return m_cost_constants;
211 }
212
213 private:
214 /// Cost constants to use in cost calculations
216
217 protected: // To be able make a gunit fake sub class
218 /*
219 Cost constants for the server operations. The purpose for this is
220 to have direct access to these instead of having to go through the
221 cost constant set each time these are needed.
222 */
224
225#if !defined(NDEBUG)
226 /**
227 Used for detecting if this object is used without having been initialized.
228 */
230#endif
231};
232
233/**
234 API for getting cost estimates for operations on table data.
235
236 @note The initial implementation mostly has functions for accessing
237 cost constants for basic operations.
238*/
239
241 public:
246#if !defined(NDEBUG)
247 m_initialized = false;
248#endif
249 }
250
251 /**
252 Initializes the cost model object.
253
254 This function must be called before calling any cost estimation
255 functions for a query. It should also be called when starting
256 optimization of a new query in case any cost estimate constants
257 have changed.
258
259 @param cost_model_server the main cost model object for this query
260 @param table the table the cost model should be used for
261 */
262
263 void init(const Cost_model_server *cost_model_server, const TABLE *table);
264
265 /**
266 Cost of processing a number of records and evaluating the query condition
267 on the records.
268
269 @param rows number of rows to evaluate
270
271 @return Cost of evaluating the records
272 */
273
274 double row_evaluate_cost(double rows) const {
275 assert(m_initialized);
276 assert(rows >= 0.0);
277
279 }
280
281 /**
282 Cost of doing a number of key compare operations.
283
284 @param keys number of key compare operations
285
286 @return Cost of comparing the keys
287 */
288
289 double key_compare_cost(double keys) const {
290 assert(m_initialized);
291 assert(keys >= 0.0);
292
294 }
295
296 /**
297 Cost of reading a number of random blocks from a table.
298
299 @param blocks number of blocks to read
300
301 @return Cost estimate
302 */
303
304 double io_block_read_cost(double blocks) const {
305 assert(m_initialized);
306 assert(blocks >= 0.0);
307
308 return blocks * m_se_cost_constants->io_block_read_cost();
309 }
310
311 /**
312 Cost of reading a number of blocks from the storage engine when the
313 block is already in a memory buffer
314
315 @param blocks number of blocks to read
316
317 @return Cost estimate
318 */
319
320 double buffer_block_read_cost(double blocks) const {
321 assert(m_initialized);
322 assert(blocks >= 0.0);
323
325 }
326
327 /**
328 Cost of reading a number of random pages from a table.
329
330 @param pages number of pages to read
331
332 @return Cost estimate
333 */
334
335 double page_read_cost(double pages) const;
336
337 /**
338 Cost of reading a number of random pages from an index.
339
340 @param index the index number
341 @param pages number of pages to read
342
343 @return Cost estimate
344 */
345
346 double page_read_cost_index(uint index, double pages) const;
347
348 /**
349 The fixed part of the cost for doing a sequential seek on disk.
350
351 For a harddisk, this corresponds to half a rotation (see comment
352 for get_sweep_read_cost() in handler.cc).
353 */
354
355 double disk_seek_base_cost() const {
356 assert(m_initialized);
357
359 }
360
361 private:
362 /**
363 The cost for seeking past one block in a sequential seek.
364
365 For a harddisk, this represents the cost of having to move the
366 disk head to the correct cylinder.
367
368 @todo Check that the BLOCKS_IN_AV_SEEK is correct to include in the
369 DISK_SEEK_PROP_COST
370 */
371
372 double disk_seek_prop_cost() const {
374 }
375
376 public:
377 /**
378 Cost estimate for a sequential disk seek where a given number of blocks
379 are skipped.
380
381 @param seek_blocks number of blocks to seek past
382
383 @return The cost estimate for the seek operation
384 */
385
386 double disk_seek_cost(double seek_blocks) const {
387 assert(seek_blocks >= 0.0);
388 assert(m_initialized);
389
390 const double cost =
391 disk_seek_base_cost() + disk_seek_prop_cost() * seek_blocks;
392 return cost;
393 }
394
395 protected: // To be able make a gunit fake sub class
396 /**
397 Pointer to the cost model for the query. This is used for getting
398 cost estimates for server operations.
399 */
401
402 /**
403 Cost constants for the storage engine that stores the table.
404 */
406
407#if !defined(NDEBUG)
408 /**
409 Used for detecting if this object is used without having been initialized.
410 */
412#endif
413
414 private:
415 /// The table that this is the cost model for
417};
418
419#endif /* OPT_COSTMODEL_INCLUDED */
Set of all cost constants used by the server and all storage engines.
Definition: opt_costconstants.h:417
API for getting cost estimates for server operations that are not directly related to a table object.
Definition: opt_costmodel.h:52
double disk_tmptable_create_cost() const
Cost of creating a temporary table using a disk based storage engine.
Definition: opt_costmodel.h:138
enum_tmptable_type
Temporary table types that the cost model differentiate between.
Definition: opt_costmodel.h:57
@ DISK_TMPTABLE
Definition: opt_costmodel.h:57
@ MEMORY_TMPTABLE
Definition: opt_costmodel.h:57
void init()
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:229
double tmptable_create_cost(enum_tmptable_type tmptable_type) const
Cost estimate for creating a temporary table.
Definition: opt_costmodel.h:172
virtual ~Cost_model_server()
Destructor for Cost_model_server objects.
Definition: opt_costmodel.cc:36
Cost_model_server()
Definition: opt_costmodel.h:59
double key_compare_cost(double keys) const
Cost of doing a number of key compare operations.
Definition: opt_costmodel.h:110
double disk_tmptable_row_cost() const
Cost of storing or retrieving a row using a disk based storage engine.
Definition: opt_costmodel.h:146
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:95
double memory_tmptable_create_cost() const
Cost of creating a temporary table in the memory storage engine.
Definition: opt_costmodel.h:122
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:190
const Server_cost_constants * m_server_cost_constants
Definition: opt_costmodel.h:223
double memory_tmptable_row_cost() const
Cost of storing or retrieving a row using the memory storage engine.
Definition: opt_costmodel.h:130
const Cost_model_constants * m_cost_constants
Cost constants to use in cost calculations.
Definition: opt_costmodel.h:215
const Cost_model_constants * get_cost_constants() const
Return a pointer to the object containing the current cost constants.
Definition: opt_costmodel.h:207
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:158
API for getting cost estimates for operations on table data.
Definition: opt_costmodel.h:240
const TABLE * m_table
The table that this is the cost model for.
Definition: opt_costmodel.h:416
double io_block_read_cost(double blocks) const
Cost of reading a number of random blocks from a table.
Definition: opt_costmodel.h:304
const Cost_model_server * m_cost_model_server
Pointer to the cost model for the query.
Definition: opt_costmodel.h:400
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:320
double page_read_cost(double pages) const
Cost of reading a number of random pages from a table.
Definition: opt_costmodel.cc:79
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:95
const SE_cost_constants * m_se_cost_constants
Cost constants for the storage engine that stores the table.
Definition: opt_costmodel.h:405
Cost_model_table()
Definition: opt_costmodel.h:242
double disk_seek_base_cost() const
The fixed part of the cost for doing a sequential seek on disk.
Definition: opt_costmodel.h:355
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:274
void init(const Cost_model_server *cost_model_server, const TABLE *table)
Initializes the cost model object.
Definition: opt_costmodel.cc:61
bool m_initialized
Used for detecting if this object is used without having been initialized.
Definition: opt_costmodel.h:411
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:386
double disk_seek_prop_cost() const
The cost for seeking past one block in a sequential seek.
Definition: opt_costmodel.h:372
double key_compare_cost(double keys) const
Cost of doing a number of key compare operations.
Definition: opt_costmodel.h:289
Cost constants for a storage engine.
Definition: opt_costconstants.h:197
double memory_block_read_cost() const
Cost of reading one random block from an in-memory database buffer.
Definition: opt_costconstants.h:211
double io_block_read_cost() const
Cost of reading one random block from disk.
Definition: opt_costconstants.h:217
Cost constants for operations done by the server.
Definition: opt_costconstants.h:64
double row_evaluate_cost() const
Cost for evaluating the query condition on a row.
Definition: opt_costconstants.h:81
double disk_temptable_row_cost() const
Cost for retrieving or storing a row in an internal disk resident temporary table.
Definition: opt_costconstants.h:115
double memory_temptable_create_cost() const
Cost for creating an internal temporary table in memory.
Definition: opt_costconstants.h:91
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:99
double disk_temptable_create_cost() const
Cost for creating an internal temporary table in a disk resident storage engine.
Definition: opt_costconstants.h:107
double key_compare_cost() const
Cost for comparing two keys.
Definition: opt_costconstants.h:86
Fido Client Authentication nullptr
Definition: fido_client_plugin.cc:222
static uint keys
Definition: hp_test2.cc:46
constexpr const double DISK_SEEK_BASE_COST
Definition: opt_costmodel.h:41
constexpr const double DISK_SEEK_PROP_COST
Definition: opt_costmodel.h:43
constexpr const int BLOCKS_IN_AVG_SEEK
Definition: opt_costmodel.h:42
Definition: table.h:1399
unsigned int uint
Definition: uca9-dump.cc:75