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