MySQL  8.0.20
Source Code Documentation
opt_costmodel.h
Go to the documentation of this file.
1 #ifndef OPT_COSTMODEL_INCLUDED
2 #define OPT_COSTMODEL_INCLUDED
3 
4 /*
5  Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
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 <stddef.h>
28 #include <sys/types.h>
29 
30 #include "my_dbug.h"
31 #include "sql/opt_costconstants.h"
32 #include "sql/sql_const.h" // defines for cost constants
33 
34 struct TABLE;
35 
36 /**
37  API for getting cost estimates for server operations that are not
38  directly related to a table object.
39 */
40 
42  public:
43  /**
44  Temporary table types that the cost model differentiate between.
45  */
47 
50 #if !defined(DBUG_OFF)
51  m_initialized = false;
52 #endif
53  }
54 
55  /**
56  Desctructor for Cost_model_server objects.
57 
58  @note This is declared virtual in order to make it easier to implement
59  stubs for this class for use in unit tests.
60  */
61 
62  virtual ~Cost_model_server();
63 
64  /**
65  Initialize the cost model object for a query.
66 
67  This function must be called before calling any cost estimation
68  functions for a query. It should also be called when starting
69  optimization of a new query in case any cost estimate constants
70  have changed.
71  */
72 
73  void init();
74 
75  /**
76  Cost of processing a number of records and evaluating the query condition
77  on the records.
78 
79  @param rows number of rows to evaluate
80 
81  @return Cost of evaluating the records
82  */
83 
84  double row_evaluate_cost(double rows) const {
86  DBUG_ASSERT(rows >= 0.0);
87 
89  }
90 
91  /**
92  Cost of doing a number of key compare operations.
93 
94  @param keys number of key compare operations
95 
96  @return Cost of comparing the keys
97  */
98 
99  double key_compare_cost(double keys) const {
101  DBUG_ASSERT(keys >= 0.0);
102 
103  return keys * m_server_cost_constants->key_compare_cost();
104  }
105 
106  private:
107  /**
108  Cost of creating a temporary table in the memory storage engine.
109  */
110 
113  }
114 
115  /**
116  Cost of storing or retrieving a row using the memory storage engine.
117  */
118 
119  double memory_tmptable_row_cost() const {
121  }
122 
123  /**
124  Cost of creating a temporary table using a disk based storage engine.
125  */
126 
127  double disk_tmptable_create_cost() const {
129  }
130 
131  /**
132  Cost of storing or retriving a row using a disk based storage engine.
133  */
134 
135  double disk_tmptable_row_cost() const {
137  }
138 
139  /**
140  Cost estimate for a row operation (insert, read) on a temporary table.
141 
142  @param tmptable_type storage type for the temporary table
143 
144  @return The estimated cost
145  */
146 
147  double tmptable_row_cost(enum_tmptable_type tmptable_type) const {
148  if (tmptable_type == MEMORY_TMPTABLE) return memory_tmptable_row_cost();
149  return disk_tmptable_row_cost();
150  }
151 
152  public:
153  /**
154  Cost estimate for creating a temporary table.
155 
156  @param tmptable_type storage type for the temporary table
157 
158  @return Cost estimate
159  */
160 
161  double tmptable_create_cost(enum_tmptable_type tmptable_type) const {
163 
164  if (tmptable_type == MEMORY_TMPTABLE) return memory_tmptable_create_cost();
165  return disk_tmptable_create_cost();
166  }
167 
168  /**
169  Cost estimate for inserting and reading records from a
170  temporary table.
171 
172  @param tmptable_type storage type for the temporary table
173  @param write_rows number of rows that will be written to table
174  @param read_rows number of rows that will be read from table
175 
176  @return The estimated cost
177  */
178 
180  double write_rows, double read_rows) const {
182  DBUG_ASSERT(write_rows >= 0.0);
183  DBUG_ASSERT(read_rows >= 0.0);
184 
185  return (write_rows + read_rows) * tmptable_row_cost(tmptable_type);
186  }
187 
188  protected:
189  friend class Cost_model_table;
190  /**
191  Return a pointer to the object containing the current cost constants.
192 
193  @return Cost constants
194  */
195 
198 
199  return m_cost_constants;
200  }
201 
202  private:
203  /// Cost constants to use in cost calculations
205 
206  protected: // To be able make a gunit fake sub class
207  /*
208  Cost constants for the server operations. The purpose for this is
209  to have direct access to these instead of having to go through the
210  cost constant set each time these are needed.
211  */
213 
214 #if !defined(DBUG_OFF)
215  /**
216  Used for detecting if this object is used without having been initialized.
217  */
219 #endif
220 };
221 
222 /**
223  API for getting cost estimates for operations on table data.
224 
225  @note The initial implementation mostly has functions for accessing
226  cost constants for basic operations.
227 */
228 
230  public:
232  : m_cost_model_server(nullptr),
233  m_se_cost_constants(nullptr),
234  m_table(nullptr) {
235 #if !defined(DBUG_OFF)
236  m_initialized = false;
237 #endif
238  }
239 
240  /**
241  Initializes the cost model object.
242 
243  This function must be called before calling any cost estimation
244  functions for a query. It should also be called when starting
245  optimization of a new query in case any cost estimate constants
246  have changed.
247 
248  @param cost_model_server the main cost model object for this query
249  @param table the table the cost model should be used for
250  */
251 
252  void init(const Cost_model_server *cost_model_server, const TABLE *table);
253 
254  /**
255  Cost of processing a number of records and evaluating the query condition
256  on the records.
257 
258  @param rows number of rows to evaluate
259 
260  @return Cost of evaluating the records
261  */
262 
263  double row_evaluate_cost(double rows) const {
265  DBUG_ASSERT(rows >= 0.0);
266 
267  return m_cost_model_server->row_evaluate_cost(rows);
268  }
269 
270  /**
271  Cost of doing a number of key compare operations.
272 
273  @param keys number of key compare operations
274 
275  @return Cost of comparing the keys
276  */
277 
278  double key_compare_cost(double keys) const {
280  DBUG_ASSERT(keys >= 0.0);
281 
282  return m_cost_model_server->key_compare_cost(keys);
283  }
284 
285  /**
286  Cost of reading a number of random blocks from a table.
287 
288  @param blocks number of blocks to read
289 
290  @return Cost estimate
291  */
292 
293  double io_block_read_cost(double blocks) const {
295  DBUG_ASSERT(blocks >= 0.0);
296 
297  return blocks * m_se_cost_constants->io_block_read_cost();
298  }
299 
300  /**
301  Cost of reading a number of blocks from the storage engine when the
302  block is already in a memory buffer
303 
304  @param blocks number of blocks to read
305 
306  @return Cost estimate
307  */
308 
309  double buffer_block_read_cost(double blocks) const {
311  DBUG_ASSERT(blocks >= 0.0);
312 
313  return blocks * m_se_cost_constants->memory_block_read_cost();
314  }
315 
316  /**
317  Cost of reading a number of random pages from a table.
318 
319  @param pages number of pages to read
320 
321  @return Cost estimate
322  */
323 
324  double page_read_cost(double pages) const;
325 
326  /**
327  Cost of reading a number of random pages from an index.
328 
329  @param index the index number
330  @param pages number of pages to read
331 
332  @return Cost estimate
333  */
334 
335  double page_read_cost_index(uint index, double pages) const;
336 
337  /**
338  The fixed part of the cost for doing a sequential seek on disk.
339 
340  For a harddisk, this corresponds to half a rotation (see comment
341  for get_sweep_read_cost() in handler.cc).
342  */
343 
344  double disk_seek_base_cost() const {
346 
347  return DISK_SEEK_BASE_COST * io_block_read_cost(1.0);
348  }
349 
350  private:
351  /**
352  The cost for seeking past one block in a sequential seek.
353 
354  For a harddisk, this represents the cost of having to move the
355  disk head to the correct cylinder.
356 
357  @todo Check that the BLOCKS_IN_AV_SEEK is correct to include in the
358  DISK_SEEK_PROP_COST (@see sql_const.h).
359 
360  See the comments for this constant in sql_const.h.
361  */
362 
363  double disk_seek_prop_cost() const {
364  return DISK_SEEK_PROP_COST * io_block_read_cost(1.0);
365  }
366 
367  public:
368  /**
369  Cost estimate for a sequential disk seek where a given number of blocks
370  are skipped.
371 
372  @param seek_blocks number of blocks to seek past
373 
374  @return The cost estimate for the seek operation
375  */
376 
377  double disk_seek_cost(double seek_blocks) const {
378  DBUG_ASSERT(seek_blocks >= 0.0);
380 
381  const double cost =
382  disk_seek_base_cost() + disk_seek_prop_cost() * seek_blocks;
383  return cost;
384  }
385 
386  protected: // To be able make a gunit fake sub class
387  /**
388  Pointer to the cost model for the query. This is used for getting
389  cost estimates for server operations.
390  */
392 
393  /**
394  Cost constants for the storage engine that stores the table.
395  */
397 
398 #if !defined(DBUG_OFF)
399  /**
400  Used for detecting if this object is used without having been initialized.
401  */
403 #endif
404 
405  private:
406  /// The table that this is the cost model for
407  const TABLE *m_table;
408 };
409 
410 #endif /* OPT_COSTMODEL_INCLUDED */
Set of all cost constants used by the server and all storage engines.
Definition: opt_costconstants.h:415
const Cost_model_constants * m_cost_constants
Cost constants to use in cost calculations.
Definition: opt_costmodel.h:204
double memory_tmptable_row_cost() const
Cost of storing or retrieving a row using the memory storage engine.
Definition: opt_costmodel.h:119
double row_evaluate_cost(double rows) const
Cost of processing a number of records and evaluating the query condition on the records.
Definition: opt_costmodel.h:84
double buffer_block_read_cost(double blocks) const
Cost of reading a number of blocks from the storage engine when the block is already in a memory buff...
Definition: opt_costmodel.h:309
Definition: opt_costmodel.h:46
void init()
Initialize the cost model object for a query.
Definition: opt_costmodel.cc:43
double disk_seek_cost(double seek_blocks) const
Cost estimate for a sequential disk seek where a given number of blocks are skipped.
Definition: opt_costmodel.h:377
File containing constants that can be used throughout the server.
enum_tmptable_type
Temporary table types that the cost model differentiate between.
Definition: opt_costmodel.h:46
double memory_tmptable_create_cost() const
Cost of creating a temporary table in the memory storage engine.
Definition: opt_costmodel.h:111
Definition: table.h:1306
Cost constants for operations done by the server.
Definition: opt_costconstants.h:62
bool m_initialized
Used for detecting if this object is used without having been initialized.
Definition: opt_costmodel.h:402
double row_evaluate_cost(double rows) const
Cost of processing a number of records and evaluating the query condition on the records.
Definition: opt_costmodel.h:263
const Cost_model_constants * get_cost_constants() const
Return a pointer to the object containing the current cost constants.
Definition: opt_costmodel.h:196
double key_compare_cost(double keys) const
Cost of doing a number of key compare operations.
Definition: opt_costmodel.h:99
#define DBUG_ASSERT(A)
Definition: my_dbug.h:199
double row_evaluate_cost() const
Cost for evaluating the query condition on a row.
Definition: opt_costconstants.h:79
const SE_cost_constants * m_se_cost_constants
Cost constants for the storage engine that stores the table.
Definition: opt_costmodel.h:396
double tmptable_readwrite_cost(enum_tmptable_type tmptable_type, double write_rows, double read_rows) const
Cost estimate for inserting and reading records from a temporary table.
Definition: opt_costmodel.h:179
const Server_cost_constants * m_server_cost_constants
Definition: opt_costmodel.h:212
#define DISK_SEEK_PROP_COST
Definition: sql_const.h:239
double tmptable_create_cost(enum_tmptable_type tmptable_type) const
Cost estimate for creating a temporary table.
Definition: opt_costmodel.h:161
Cost_model_server()
Definition: opt_costmodel.h:48
static uint keys
Definition: hp_test2.cc:45
unsigned int uint
Definition: uca-dump.cc:29
double io_block_read_cost(double blocks) const
Cost of reading a number of random blocks from a table.
Definition: opt_costmodel.h:293
const Cost_model_server * m_cost_model_server
Pointer to the cost model for the query.
Definition: opt_costmodel.h:391
double disk_tmptable_row_cost() const
Cost of storing or retriving a row using a disk based storage engine.
Definition: opt_costmodel.h:135
double disk_temptable_create_cost() const
Cost for creating an internal temporary table in a disk resident storage engine.
Definition: opt_costconstants.h:105
Definition: opt_costmodel.h:46
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:97
double memory_temptable_create_cost() const
Cost for creating an internal temporary table in memory.
Definition: opt_costconstants.h:89
double key_compare_cost() const
Cost for comparing two keys.
Definition: opt_costconstants.h:84
virtual ~Cost_model_server()
Desctructor for Cost_model_server objects.
Definition: opt_costmodel.cc:34
double disk_seek_prop_cost() const
The cost for seeking past one block in a sequential seek.
Definition: opt_costmodel.h:363
const TABLE * m_table
The table that this is the cost model for.
Definition: opt_costmodel.h:407
double key_compare_cost(double keys) const
Cost of doing a number of key compare operations.
Definition: opt_costmodel.h:278
double tmptable_row_cost(enum_tmptable_type tmptable_type) const
Cost estimate for a row operation (insert, read) on a temporary table.
Definition: opt_costmodel.h:147
double disk_seek_base_cost() const
The fixed part of the cost for doing a sequential seek on disk.
Definition: opt_costmodel.h:344
bool m_initialized
Used for detecting if this object is used without having been initialized.
Definition: opt_costmodel.h:218
API for getting cost estimates for operations on table data.
Definition: opt_costmodel.h:229
Cost_model_table()
Definition: opt_costmodel.h:231
#define DISK_SEEK_BASE_COST
Definition: sql_const.h:235
Cost constants for a storage engine.
Definition: opt_costconstants.h:195
API for getting cost estimates for server operations that are not directly related to a table object...
Definition: opt_costmodel.h:41
double disk_temptable_row_cost() const
Cost for retrieving or storing a row in an internal disk resident temporary table.
Definition: opt_costconstants.h:113
double disk_tmptable_create_cost() const
Cost of creating a temporary table using a disk based storage engine.
Definition: opt_costmodel.h:127
Dialog Client Authentication nullptr
Definition: dialog.cc:353