MySQL 8.0.37
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;
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 using the default values
68 defined in this class.
69 */
77
78 /**
79 Cost for evaluating the query condition on a row.
80 */
81 double row_evaluate_cost() const { return m_row_evaluate_cost; }
82
83 /**
84 Cost for comparing two keys.
85 */
86 double key_compare_cost() const { return m_key_compare_cost; }
87
88 /**
89 Cost for creating an internal temporary table in memory.
90 */
93 }
94
95 /**
96 Cost for retrieving or storing a row in an internal temporary table
97 stored in memory.
98 */
101 }
102
103 /**
104 Cost for creating an internal temporary table in a disk resident
105 storage engine.
106 */
109 }
110
111 /**
112 Cost for retrieving or storing a row in an internal disk resident
113 temporary table.
114 */
116
117 /**
118 Set the value of one of the cost constants.
119
120 @param name name of cost constant
121 @param value new value
122
123 @return Status for updating the cost constant
124 */
125
126 cost_constant_error set(const LEX_CSTRING &name, const double value);
127
128 private:
129 /*
130 This section declares constants for the default values. The actual
131 default values are found in the .cc file.
132 */
133
134 /// Default cost for evaluation of the query condition for a row.
135 static const double ROW_EVALUATE_COST;
136
137 /// Default cost for comparing row ids.
138 static const double KEY_COMPARE_COST;
139
140 /*
141 Constants related to the use of temporary tables in query execution.
142 Lookup and write operations are currently assumed to be equally costly
143 (concerns MEMORY_TEMPTABLE_ROW_COST and DISK_TEMPTABLE_ROW_COST).
144 */
145
146 /// Cost for creating a memory temporary table.
147 static const double MEMORY_TEMPTABLE_CREATE_COST;
148
149 /// Cost for inserting or reading a row in a memory temporary table.
150 static const double MEMORY_TEMPTABLE_ROW_COST;
151
152 /// Cost for creating a disk temporary table
153 static const double DISK_TEMPTABLE_CREATE_COST;
154
155 /// Cost for inserting or reading a row in a disk temporary table.
156 static const double DISK_TEMPTABLE_ROW_COST;
157
158 /*
159 This section specifies cost constants for server operations
160 */
161
162 /// Cost for evaluating the query condition on a row
164
165 /// Cost for comparing two keys
167
168 /// Cost for creating an internal temporary table in memory
170
171 /**
172 Cost for retrieving or storing a row in an internal temporary table
173 stored in memory.
174 */
176
177 /**
178 Cost for creating an internal temporary table in a disk resident
179 storage engine.
180 */
182
183 /**
184 Cost for retrieving or storing a row in an internal disk resident
185 temporary table.
186 */
188};
189
190/**
191 Cost constants for a storage engine.
192
193 Storage engines that want to add new cost constants should make
194 a subclass of this class.
195*/
196
198 public:
204
205 virtual ~SE_cost_constants() = default;
206
207 /**
208 Cost of reading one random block from an in-memory database buffer.
209 */
210
212
213 /**
214 Cost of reading one random block from disk.
215 */
216
217 double io_block_read_cost() const { return m_io_block_read_cost; }
218
219 protected:
220 /**
221 Set the value of one of the cost constants.
222
223 If a storage engine wants to introduce a new cost constant, it should
224 provide an implementation of this function. If the cost constant
225 is not recognized by the function in the subclass, then this function
226 should be called to allow the cost constant in the base class to be
227 given the updated value.
228
229 @param name name of cost constant
230 @param value new value
231 @param default_value specify whether the new value is a default value or
232 an engine specific value
233
234 @return Status for updating the cost constant
235 */
236
237 virtual cost_constant_error set(const LEX_CSTRING &name, const double value,
238 bool default_value);
239
240 protected:
242
243 /**
244 Update the value of a cost constant.
245
246 @param name name of the cost constant
247 @param value the new value this cost constant should take
248
249 @return Status for updating the cost constant
250 */
251
252 cost_constant_error update(const LEX_CSTRING &name, const double value);
253
254 /**
255 Update the default value of a cost constant.
256
257 If this const constant already has been given a non-default value,
258 then calling this will have no effect on the current value for the
259 cost constant.
260
261 @param name name of the cost constant
262 @param value the new value this cost constant should take
263
264 @return Status for updating the cost constant
265 */
266
268 const double value);
269
270 /**
271 Utility function for changing the value of a cost constant.
272
273 The cost constant will be updated to the new value iff:
274 a) the current value is the default value, or
275 b) the current value is not the default value and the new value
276 is not a default value
277
278 @param[out] cost_constant pointer to the cost constant that
279 should be updated
280 @param[in,out] cost_constant_is_default whether the current value has the
281 default value or not
282 @param new_value the new value for the cost constant
283 @param new_value_is_default whether this is a new default value
284 or not
285 */
286
287 void update_cost_value(double *cost_constant, bool *cost_constant_is_default,
288 double new_value, bool new_value_is_default);
289
290 private:
291 /*
292 This section specifies default values for cost constants.
293 */
294
295 /// Default cost for reading a random block from an in-memory buffer
296 static const double MEMORY_BLOCK_READ_COST;
297
298 /// Default cost for reading a random disk block
299 static const double IO_BLOCK_READ_COST;
300
301 /*
302 This section specifies cost constants for the table
303 */
304
305 /// Cost constant for reading a random block from an in-memory buffer
307
308 /// Cost constant for reading a random disk block.
310
311 /*
312 This section has boolean variables that is used for knowing whether
313 the above cost variables is using the default value or not.
314 */
315
316 /// Whether the memory_block_read_cost is a default value or not
318
319 /// Whether the io_block_read_cost is a default value or not
321};
322
323/**
324 Class that keeps all cost constants for a storage engine. Since
325 storage engines can use different types of storage devices, each
326 device type can have its own set of cost constants.
327
328 @note In the initial implementation there will only be one
329 set of cost constants per storage engine.
330*/
331
333 public:
334 /**
335 Constructor that just initializes the class.
336 */
338
339 /**
340 Destructor. Deletes the allocated cost constant objects.
341 */
343
344 private:
345 /*
346 Since this object owns the cost constant objects, we must prevent that we
347 create copies of this object that share the cost constant objects.
348 */
351
352 protected:
354
355 /**
356 Set the storage constants to be used for a given storage type for
357 this storage engine.
358
359 @param cost_constants cost constants for the storage engine
360 @param storage_class the storage class these cost constants should be
361 used for
362 */
363
365 unsigned int storage_class) {
366 assert(cost_constants != nullptr);
367 assert(storage_class < MAX_STORAGE_CLASSES);
368 assert(m_se_cost_constants[storage_class] == nullptr);
369
370 m_se_cost_constants[storage_class] = cost_constants;
371 }
372
373 /**
374 Retrieve the cost constants to be used for this storage engine for
375 a specified storage class.
376
377 @param storage_class the storage class these cost constants should be
378 used for
379 */
380
382 unsigned int storage_class) const {
383 assert(storage_class < MAX_STORAGE_CLASSES);
384 assert(m_se_cost_constants[storage_class] != nullptr);
385
386 return m_se_cost_constants[storage_class];
387 }
388
389 /**
390 Retrieve the cost constants to be used for this storage engine for
391 a specified storage class.
392
393 @param storage_class the storage class these cost constants should be
394 used for
395 */
396
397 SE_cost_constants *get_cost_constants(unsigned int storage_class) {
398 assert(storage_class < MAX_STORAGE_CLASSES);
399 assert(m_se_cost_constants[storage_class] != nullptr);
400
401 return m_se_cost_constants[storage_class];
402 }
403
404 private:
405 /**
406 Array of cost constant sets for this storage engine. There will
407 be one set of cost constants for each device type defined for the
408 storage engine.
409 */
411};
412
413/**
414 Set of all cost constants used by the server and all storage engines.
415*/
416
418 public:
419 /**
420 Creates a set with cost constants using the default values defined in
421 the source code.
422 */
423
425
426 /**
427 Destructor.
428
429 @note The only reason for making this virtual is to be able to make
430 a sub-class for use in unit testing.
431 */
432
433 virtual ~Cost_model_constants();
434
435 /**
436 Get the cost constants that should be used for server operations.
437
438 @return the cost constants for the server
439 */
440
442 return &m_server_constants;
443 }
444
445 /**
446 Return the cost constants that should be used for a given table.
447
448 @param table the table to find cost constants for
449
450 @return the cost constants to use for the table
451 */
452
453 const SE_cost_constants *get_se_cost_constants(const TABLE *table) const;
454
455 /**
456 Update the value for one of the server cost constants.
457
458 @param name name of the cost constant
459 @param value new value
460
461 @return Status for updating the cost constant
462 */
463
465 double value);
466
467 /**
468 Update the value for one of the storage engine cost constants.
469
470 @param thd the THD
471 @param se_name name of storage engine
472 @param storage_category storage device type
473 @param name name of cost constant
474 @param value new value
475
476 @return Status for updating the cost constant
477 */
478
480 const LEX_CSTRING &se_name,
481 uint storage_category,
482 const LEX_CSTRING &name,
483 double value);
484
485 protected:
487
488 /**
489 Increment the reference counter for this cost constant set
490 */
491
493
494 /**
495 Decrement the reference counter for this cost constant set
496
497 When the returned value is zero, there is nobody using this object
498 and it can be deleted by the caller.
499
500 @return the updated reference count
501 */
502
503 unsigned int dec_ref_count() {
504 assert(m_ref_counter > 0);
505
507 return m_ref_counter;
508 }
509
510 private:
511 /**
512 Utility function for finding the slot number for a storage engine
513 based on the storage engine name.
514
515 The only reason for making this function virtual is to be able to
516 override it in unit tests.
517
518 @param thd the THD
519 @param name name of storage engine
520
521 @return slot number for the storage engine, HA_SLOT_UNDEF if there
522 is no handler for this name
523 */
524
526 const LEX_CSTRING &name) const;
527
528 /**
529 Update the default value for a storage engine cost constant.
530
531 @param name name of cost constant
532 @param storage_category storage device type
533 @param value new value
534
535 @return Status for updating the cost constant
536 */
537
539 uint storage_category,
540 double value);
541
542 /// Cost constants for server operations
544
545 /**
546 Cost constants for storage engines
547 15 should be enough for most use cases, see PREALLOC_NUM_HA.
548 */
550
551 /// Reference counter for this set of cost constants.
552 unsigned int m_ref_counter;
553};
554
555#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:417
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:284
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:263
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:316
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:333
const Server_cost_constants * get_server_cost_constants() const
Get the cost constants that should be used for server operations.
Definition: opt_costconstants.h:441
unsigned int dec_ref_count()
Decrement the reference counter for this cost constant set.
Definition: opt_costconstants.h:503
Cost_model_constants()
Creates a set with cost constants using the default values defined in the source code.
Definition: opt_costconstants.cc:225
unsigned int m_ref_counter
Reference counter for this set of cost constants.
Definition: opt_costconstants.h:552
void inc_ref_count()
Increment the reference counter for this cost constant set.
Definition: opt_costconstants.h:492
Server_cost_constants m_server_constants
Cost constants for server operations.
Definition: opt_costconstants.h:543
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:549
virtual ~Cost_model_constants()
Destructor.
Definition: opt_costconstants.cc:261
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:289
Class that keeps all cost constants for a storage engine.
Definition: opt_costconstants.h:332
SE_cost_constants * m_se_cost_constants[MAX_STORAGE_CLASSES]
Array of cost constant sets for this storage engine.
Definition: opt_costconstants.h:410
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:381
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:364
Cost_model_se_info()
Constructor that just initializes the class.
Definition: opt_costconstants.cc:213
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:397
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:218
A typesafe replacement for DYNAMIC_ARRAY.
Definition: prealloced_array.h:71
Cost constants for a storage engine.
Definition: opt_costconstants.h:197
double m_io_block_read_cost
Cost constant for reading a random disk block.
Definition: opt_costconstants.h:309
bool m_io_block_read_cost_default
Whether the io_block_read_cost is a default value or not.
Definition: opt_costconstants.h:320
double m_memory_block_read_cost
Cost constant for reading a random block from an in-memory buffer.
Definition: opt_costconstants.h:306
cost_constant_error update_default(const LEX_CSTRING &name, const double value)
Update the default value of a cost constant.
Definition: opt_costconstants.cc:191
static const double MEMORY_BLOCK_READ_COST
Default cost for reading a random block from an in-memory buffer.
Definition: opt_costconstants.h:296
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:154
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:196
cost_constant_error update(const LEX_CSTRING &name, const double value)
Update the value of a cost constant.
Definition: opt_costconstants.cc:186
double memory_block_read_cost() const
Cost of reading one random block from an in-memory database buffer.
Definition: opt_costconstants.h:211
SE_cost_constants()
Definition: opt_costconstants.h:199
bool m_memory_block_read_cost_default
Whether the memory_block_read_cost is a default value or not.
Definition: opt_costconstants.h:317
double io_block_read_cost() const
Cost of reading one random block from disk.
Definition: opt_costconstants.h:217
static const double IO_BLOCK_READ_COST
Default cost for reading a random disk block.
Definition: opt_costconstants.h:299
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:81
double m_row_evaluate_cost
Cost for evaluating the query condition on a row.
Definition: opt_costconstants.h:163
double disk_temptable_row_cost() const
Cost for retrieving or storing a row in an internal disk resident temporary table.
Definition: opt_costconstants.h:115
static const double KEY_COMPARE_COST
Default cost for comparing row ids.
Definition: opt_costconstants.h:138
double memory_temptable_create_cost() const
Cost for creating an internal temporary table in memory.
Definition: opt_costconstants.h:91
static const double MEMORY_TEMPTABLE_CREATE_COST
Cost for creating a memory temporary table.
Definition: opt_costconstants.h:147
static const double ROW_EVALUATE_COST
Default cost for evaluation of the query condition for a row.
Definition: opt_costconstants.h:135
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:175
static const double MEMORY_TEMPTABLE_ROW_COST
Cost for inserting or reading a row in a memory temporary table.
Definition: opt_costconstants.h:150
cost_constant_error set(const LEX_CSTRING &name, const double value)
Set the value of one of the cost constants.
Definition: opt_costconstants.cc:87
static const double DISK_TEMPTABLE_CREATE_COST
Cost for creating a disk temporary table.
Definition: opt_costconstants.h:153
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:99
static const double DISK_TEMPTABLE_ROW_COST
Cost for inserting or reading a row in a disk temporary table.
Definition: opt_costconstants.h:156
double m_disk_temptable_row_cost
Cost for retrieving or storing a row in an internal disk resident temporary table.
Definition: opt_costconstants.h:187
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:166
Server_cost_constants()
Creates a server cost constants object using the default values defined in this class.
Definition: opt_costconstants.h:70
double disk_temptable_create_cost() const
Cost for creating an internal temporary table in a disk resident storage engine.
Definition: opt_costconstants.h:107
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:86
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:34
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:33
Definition: mysql_lex_string.h:40
Definition: table.h:1398
unsigned int uint
Definition: uca9-dump.cc:75