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