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