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