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