MySQL 8.4.3
Source Code Documentation
opt_costconstants.h
Go to the documentation of this file.
1#ifndef OPT_COSTCONSTANTS_INCLUDED
2#define OPT_COSTCONSTANTS_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
32#include "lex_string.h"
33
34#include "prealloced_array.h"
35
36class THD;
37struct TABLE;
39
40/**
41 Error codes returned from the functions that do updates of the
42 cost constants.
43*/
50};
51
52/**
53 The cost model should support different types of storage devices each with
54 different cost constants. Due to that we in the current version does not
55 have a way to know which storage device a given table is stored on, the
56 initial version of the cost model will only have one set of cost constants
57 per storage engine.
58*/
59const unsigned int MAX_STORAGE_CLASSES = 1;
60
61/**
62 Cost constants for operations done by the server
63*/
64
66 public:
67 /**
68 Creates a server cost constants object with default values. The default
69 values of the cost constants are specified here.
70
71 @param optimizer The type of optimizer to construct cost constants for.
72
73 @note The default cost constants are displayed in the default_value column
74 of the mysql.server_cost tables. If any default value is changed, make sure
75 to update the column definitions in mysql_system_tables.sql and
76 mysql_system_tables_fix.sql.
77
78 */
80 switch (optimizer) {
83 m_key_compare_cost = 0.05;
88 break;
91 m_key_compare_cost = 0.05;
96 break;
97 }
98 }
99
100 /**
101 Cost for evaluating the query condition on a row.
102 */
103 double row_evaluate_cost() const { return m_row_evaluate_cost; }
104
105 /**
106 Cost for comparing two keys.
107 */
108 double key_compare_cost() const { return m_key_compare_cost; }
109
110 /**
111 Cost for creating an internal temporary table in memory.
112 */
115 }
116
117 /**
118 Cost for retrieving or storing a row in an internal temporary table
119 stored in memory.
120 */
123 }
124
125 /**
126 Cost for creating an internal temporary table in a disk resident
127 storage engine.
128 */
131 }
132
133 /**
134 Cost for retrieving or storing a row in an internal disk resident
135 temporary table.
136 */
138
139 /**
140 Set the value of one of the cost constants.
141
142 @param name name of cost constant
143 @param value new value
144
145 @return Status for updating the cost constant
146 */
147
148 cost_constant_error set(const LEX_CSTRING &name, const double value);
149
150 private:
151 /// Cost for evaluating the query condition on a row.
153
154 /// Cost for comparing two keys.
156
157 /**
158 Cost for creating an internal temporary table in memory.
159
160 @note Creating a Memory temporary table is by benchmark found to be as
161 costly as writing 10 rows into the table.
162 */
164
165 /**
166 Cost for retrieving or storing a row in an internal temporary table stored
167 in memory.
168
169 @note Writing a row to or reading a row from a Memory temporary table is
170 equivalent to evaluating a row in the join engine.
171 */
173
174 /**
175 Cost for creating an internal temporary table in a disk resident storage
176 engine.
177
178 @note Creating a MyISAM table is 20 times slower than creating a Memory
179 table.
180
181 */
183
184 /**
185 Cost for retrieving or storing a row in an internal disk resident temporary
186 table.
187
188 @note Generating MyISAM rows sequentially is 2 times slower than generating
189 Memory rows, when number of rows is greater than 1000. However, we do not
190 have benchmarks for very large tables, so setting this factor conservatively
191 to be 5 times slower (ie the cost is 1.0).
192 */
194};
195
196/**
197 Cost constants for a storage engine.
198
199 Storage engines that want to add new cost constants should make
200 a subclass of this class.
201*/
203 public:
204 /**
205 Creates a storage engine cost constants object with default values. The
206 default values for the cost constants are specified here.
207
208 @param optimizer The type of optimizer to construct cost constants for.
209
210 @note The default cost constants are displayed in the default_value column
211 of the mysql.engine_cost cost table. If any default value is changed, make
212 sure to update the column definitions in mysql_system_tables.sql and
213 mysql_system_tables_fix.sql.
214
215 */
219 switch (optimizer) {
223 break;
227 break;
228 }
229 }
230
231 virtual ~SE_cost_constants() = default;
232
233 /**
234 Cost of reading one random block from an in-memory database buffer.
235 */
236
238
239 /**
240 Cost of reading one random block from disk.
241 */
242
243 double io_block_read_cost() const { return m_io_block_read_cost; }
244
245 protected:
246 /**
247 Set the value of one of the cost constants.
248
249 If a storage engine wants to introduce a new cost constant, it should
250 provide an implementation of this function. If the cost constant
251 is not recognized by the function in the subclass, then this function
252 should be called to allow the cost constant in the base class to be
253 given the updated value.
254
255 @param name name of cost constant
256 @param value new value
257 @param default_value specify whether the new value is a default value or
258 an engine specific value
259
260 @return Status for updating the cost constant
261 */
262
263 virtual cost_constant_error set(const LEX_CSTRING &name, const double value,
264 bool default_value);
265
266 protected:
268
269 /**
270 Update the value of a cost constant.
271
272 @param name name of the cost constant
273 @param value the new value this cost constant should take
274
275 @return Status for updating the cost constant
276 */
277
278 cost_constant_error update(const LEX_CSTRING &name, const double value);
279
280 /**
281 Update the default value of a cost constant.
282
283 If this const constant already has been given a non-default value,
284 then calling this will have no effect on the current value for the
285 cost constant.
286
287 @param name name of the cost constant
288 @param value the new value this cost constant should take
289
290 @return Status for updating the cost constant
291 */
292
294 const double value);
295
296 /**
297 Utility function for changing the value of a cost constant.
298
299 The cost constant will be updated to the new value iff:
300 a) the current value is the default value, or
301 b) the current value is not the default value and the new value
302 is not a default value
303
304 @param[out] cost_constant pointer to the cost constant that
305 should be updated
306 @param[in,out] cost_constant_is_default whether the current value has the
307 default value or not
308 @param new_value the new value for the cost constant
309 @param new_value_is_default whether this is a new default value
310 or not
311 */
312
313 void update_cost_value(double *cost_constant, bool *cost_constant_is_default,
314 double new_value, bool new_value_is_default);
315
316 private:
317 /// Cost constant for reading a random block from an in-memory buffer
319
320 /// Cost constant for reading a random disk block.
322
323 /// Whether the memory_block_read_cost is a default value or not
325
326 /// Whether the io_block_read_cost is a default value or not
328};
329
330/**
331 Class that keeps all cost constants for a storage engine. Since
332 storage engines can use different types of storage devices, each
333 device type can have its own set of cost constants.
334
335 @note In the initial implementation there will only be one
336 set of cost constants per storage engine.
337*/
338
340 public:
341 /**
342 Constructor that just initializes the class.
343 */
345
346 /**
347 Destructor. Deletes the allocated cost constant objects.
348 */
350
351 private:
352 /*
353 Since this object owns the cost constant objects, we must prevent that we
354 create copies of this object that share the cost constant objects.
355 */
358
359 protected:
361
362 /**
363 Set the storage constants to be used for a given storage type for
364 this storage engine.
365
366 @param cost_constants cost constants for the storage engine
367 @param storage_class the storage class these cost constants should be
368 used for
369 */
370
372 unsigned int storage_class) {
373 assert(cost_constants != nullptr);
374 assert(storage_class < MAX_STORAGE_CLASSES);
375 assert(m_se_cost_constants[storage_class] == nullptr);
376
377 m_se_cost_constants[storage_class] = cost_constants;
378 }
379
380 /**
381 Retrieve the cost constants to be used for this storage engine for
382 a specified storage class.
383
384 @param storage_class the storage class these cost constants should be
385 used for
386 */
387
389 unsigned int storage_class) const {
390 assert(storage_class < MAX_STORAGE_CLASSES);
391 assert(m_se_cost_constants[storage_class] != nullptr);
392
393 return m_se_cost_constants[storage_class];
394 }
395
396 /**
397 Retrieve the cost constants to be used for this storage engine for
398 a specified storage class.
399
400 @param storage_class the storage class these cost constants should be
401 used for
402 */
403
404 SE_cost_constants *get_cost_constants(unsigned int storage_class) {
405 assert(storage_class < MAX_STORAGE_CLASSES);
406 assert(m_se_cost_constants[storage_class] != nullptr);
407
408 return m_se_cost_constants[storage_class];
409 }
410
411 private:
412 /**
413 Array of cost constant sets for this storage engine. There will
414 be one set of cost constants for each device type defined for the
415 storage engine.
416 */
418};
419
420/**
421 Set of all cost constants used by the server and all storage engines.
422*/
423
425 public:
426 /**
427 Creates a set with cost constants using the default values defined in
428 the source code.
429 */
430
432
433 /**
434 Destructor.
435
436 @note The only reason for making this virtual is to be able to make
437 a sub-class for use in unit testing.
438 */
439
440 virtual ~Cost_model_constants();
441
442 /**
443 Get the cost constants that should be used for server operations.
444
445 @return the cost constants for the server
446 */
447
449 return &m_server_constants;
450 }
451
452 /**
453 Return the cost constants that should be used for a given table.
454
455 @param table the table to find cost constants for
456
457 @return the cost constants to use for the table
458 */
459
461
462 /**
463 Update the value for one of the server cost constants.
464
465 @param name name of the cost constant
466 @param value new value
467
468 @return Status for updating the cost constant
469 */
470
472 double value);
473
474 /**
475 Update the value for one of the storage engine cost constants.
476
477 @param thd the THD
478 @param se_name name of storage engine
479 @param storage_category storage device type
480 @param name name of cost constant
481 @param value new value
482
483 @return Status for updating the cost constant
484 */
485
487 const LEX_CSTRING &se_name,
488 uint storage_category,
489 const LEX_CSTRING &name,
490 double value);
491
492 protected:
494
495 /**
496 Increment the reference counter for this cost constant set
497 */
498
500
501 /**
502 Decrement the reference counter for this cost constant set
503
504 When the returned value is zero, there is nobody using this object
505 and it can be deleted by the caller.
506
507 @return the updated reference count
508 */
509
510 unsigned int dec_ref_count() {
511 assert(m_ref_counter > 0);
512
514 return m_ref_counter;
515 }
516
517 private:
518 /**
519 Utility function for finding the slot number for a storage engine
520 based on the storage engine name.
521
522 The only reason for making this function virtual is to be able to
523 override it in unit tests.
524
525 @param thd the THD
526 @param name name of storage engine
527
528 @return slot number for the storage engine, HA_SLOT_UNDEF if there
529 is no handler for this name
530 */
531
532 virtual uint find_handler_slot_from_name(THD *thd,
533 const LEX_CSTRING &name) const;
534
535 /**
536 Update the default value for a storage engine cost constant.
537
538 @param name name of cost constant
539 @param storage_category storage device type
540 @param value new value
541
542 @return Status for updating the cost constant
543 */
544
546 uint storage_category,
547 double value);
548
549 /// Cost constants for server operations
551
552 /**
553 Cost constants for storage engines
554 15 should be enough for most use cases, see PREALLOC_NUM_HA.
555 */
557
558 /// Reference counter for this set of cost constants.
559 unsigned int m_ref_counter;
560
561 /// Optimizer type.
563};
564
565#endif /* OPT_COSTCONSTANTS_INCLUDEDED */
This class implements a cache for "cost constant sets".
Definition: opt_costconstantcache.h:59
Set of all cost constants used by the server and all storage engines.
Definition: opt_costconstants.h:424
cost_constant_error update_server_cost_constant(const LEX_CSTRING &name, double value)
Update the value for one of the server cost constants.
Definition: opt_costconstants.cc:232
Optimizer m_optimizer
Optimizer type.
Definition: opt_costconstants.h:562
Cost_model_constants(Optimizer optimizer)
Creates a set with cost constants using the default values defined in the source code.
Definition: opt_costconstants.cc:166
const SE_cost_constants * get_se_cost_constants(const TABLE *table) const
Return the cost constants that should be used for a given table.
Definition: opt_costconstants.cc:207
virtual uint find_handler_slot_from_name(THD *thd, const LEX_CSTRING &name) const
Utility function for finding the slot number for a storage engine based on the storage engine name.
Definition: opt_costconstants.cc:264
cost_constant_error update_engine_default_cost(const LEX_CSTRING &name, uint storage_category, double value)
Update the default value for a storage engine cost constant.
Definition: opt_costconstants.cc:281
const Server_cost_constants * get_server_cost_constants() const
Get the cost constants that should be used for server operations.
Definition: opt_costconstants.h:448
unsigned int dec_ref_count()
Decrement the reference counter for this cost constant set.
Definition: opt_costconstants.h:510
unsigned int m_ref_counter
Reference counter for this set of cost constants.
Definition: opt_costconstants.h:559
void inc_ref_count()
Increment the reference counter for this cost constant set.
Definition: opt_costconstants.h:499
Server_cost_constants m_server_constants
Cost constants for server operations.
Definition: opt_costconstants.h:550
Prealloced_array< Cost_model_se_info, 15 > m_engines
Cost constants for storage engines 15 should be enough for most use cases, see PREALLOC_NUM_HA.
Definition: opt_costconstants.h:556
virtual ~Cost_model_constants()
Destructor.
Definition: opt_costconstants.cc:205
cost_constant_error update_engine_cost_constant(THD *thd, const LEX_CSTRING &se_name, uint storage_category, const LEX_CSTRING &name, double value)
Update the value for one of the storage engine cost constants.
Definition: opt_costconstants.cc:237
Class that keeps all cost constants for a storage engine.
Definition: opt_costconstants.h:339
SE_cost_constants * m_se_cost_constants[MAX_STORAGE_CLASSES]
Array of cost constant sets for this storage engine.
Definition: opt_costconstants.h:417
const SE_cost_constants * get_cost_constants(unsigned int storage_class) const
Retrieve the cost constants to be used for this storage engine for a specified storage class.
Definition: opt_costconstants.h:388
void set_cost_constants(SE_cost_constants *cost_constants, unsigned int storage_class)
Set the storage constants to be used for a given storage type for this storage engine.
Definition: opt_costconstants.h:371
Cost_model_se_info()
Constructor that just initializes the class.
Definition: opt_costconstants.cc:154
SE_cost_constants * get_cost_constants(unsigned int storage_class)
Retrieve the cost constants to be used for this storage engine for a specified storage class.
Definition: opt_costconstants.h:404
Cost_model_se_info & operator=(const Cost_model_se_info &rhs)
Cost_model_se_info(const Cost_model_se_info &)
~Cost_model_se_info()
Destructor.
Definition: opt_costconstants.cc:159
A typesafe replacement for DYNAMIC_ARRAY.
Definition: prealloced_array.h:71
Cost constants for a storage engine.
Definition: opt_costconstants.h:202
double m_io_block_read_cost
Cost constant for reading a random disk block.
Definition: opt_costconstants.h:321
bool m_io_block_read_cost_default
Whether the io_block_read_cost is a default value or not.
Definition: opt_costconstants.h:327
double m_memory_block_read_cost
Cost constant for reading a random block from an in-memory buffer.
Definition: opt_costconstants.h:318
cost_constant_error update_default(const LEX_CSTRING &name, const double value)
Update the default value of a cost constant.
Definition: opt_costconstants.cc:132
virtual ~SE_cost_constants()=default
virtual cost_constant_error set(const LEX_CSTRING &name, const double value, bool default_value)
Set the value of one of the cost constants.
Definition: opt_costconstants.cc:95
void update_cost_value(double *cost_constant, bool *cost_constant_is_default, double new_value, bool new_value_is_default)
Utility function for changing the value of a cost constant.
Definition: opt_costconstants.cc:137
cost_constant_error update(const LEX_CSTRING &name, const double value)
Update the value of a cost constant.
Definition: opt_costconstants.cc:127
double memory_block_read_cost() const
Cost of reading one random block from an in-memory database buffer.
Definition: opt_costconstants.h:237
SE_cost_constants(Optimizer optimizer)
Creates a storage engine cost constants object with default values.
Definition: opt_costconstants.h:216
bool m_memory_block_read_cost_default
Whether the memory_block_read_cost is a default value or not.
Definition: opt_costconstants.h:324
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 m_row_evaluate_cost
Cost for evaluating the query condition on a row.
Definition: opt_costconstants.h:152
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
Server_cost_constants(Optimizer optimizer)
Creates a server cost constants object with default values.
Definition: opt_costconstants.h:79
double memory_temptable_create_cost() const
Cost for creating an internal temporary table in memory.
Definition: opt_costconstants.h:113
double m_memory_temptable_row_cost
Cost for retrieving or storing a row in an internal temporary table stored in memory.
Definition: opt_costconstants.h:172
cost_constant_error set(const LEX_CSTRING &name, const double value)
Set the value of one of the cost constants.
Definition: opt_costconstants.cc:42
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 m_disk_temptable_row_cost
Cost for retrieving or storing a row in an internal disk resident temporary table.
Definition: opt_costconstants.h:193
double m_disk_temptable_create_cost
Cost for creating an internal temporary table in a disk resident storage engine.
Definition: opt_costconstants.h:182
double m_key_compare_cost
Cost for comparing two keys.
Definition: opt_costconstants.h:155
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 m_memory_temptable_create_cost
Cost for creating an internal temporary table in memory.
Definition: opt_costconstants.h:163
double key_compare_cost() const
Cost for comparing two keys.
Definition: opt_costconstants.h:108
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
static PFS_engine_table_share_proxy table
Definition: pfs.cc:61
Optimizer
Definition: opt_costconstants.h:38
const unsigned int MAX_STORAGE_CLASSES
The cost model should support different types of storage devices each with different cost constants.
Definition: opt_costconstants.h:59
cost_constant_error
Error codes returned from the functions that do updates of the cost constants.
Definition: opt_costconstants.h:44
@ INVALID_COST_VALUE
Definition: opt_costconstants.h:48
@ UNKNOWN_COST_NAME
Definition: opt_costconstants.h:46
@ UNKNOWN_ENGINE_NAME
Definition: opt_costconstants.h:47
@ INVALID_DEVICE_TYPE
Definition: opt_costconstants.h:49
@ COST_CONSTANT_OK
Definition: opt_costconstants.h:45
case opt name
Definition: sslopt-case.h:29
Definition: mysql_lex_string.h:40
Definition: table.h:1406