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