1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
|
#ifndef OPT_COSTMODEL_INCLUDED
#define OPT_COSTMODEL_INCLUDED
/*
Copyright (c) 2014, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include <assert.h>
#include <stddef.h>
#include <sys/types.h>
#include "sql/opt_costconstants.h"
/*
For sequential disk seeks the cost formula is:
DISK_SEEK_BASE_COST + DISK_SEEK_PROP_COST * #blocks_to_skip
The cost of average seek
DISK_SEEK_BASE_COST + DISK_SEEK_PROP_COST*BLOCKS_IN_AVG_SEEK =1.0.
*/
constexpr const double DISK_SEEK_BASE_COST{0.9};
constexpr const int BLOCKS_IN_AVG_SEEK{128};
constexpr const double DISK_SEEK_PROP_COST{0.1 / BLOCKS_IN_AVG_SEEK};
struct TABLE;
/**
API for getting cost estimates for server operations that are not
directly related to a table object.
*/
class Cost_model_server {
public:
/**
Temporary table types that the cost model differentiate between.
*/
enum enum_tmptable_type { MEMORY_TMPTABLE, DISK_TMPTABLE };
Cost_model_server()
: m_cost_constants(nullptr), m_server_cost_constants(nullptr) {
#if !defined(NDEBUG)
m_initialized = false;
#endif
}
/**
Destructor for Cost_model_server objects.
@note This is declared virtual in order to make it easier to implement
stubs for this class for use in unit tests.
*/
virtual ~Cost_model_server();
/**
Initialize the cost model object for a query.
This function must be called before calling any cost estimation
functions for a query. It should also be called when starting
optimization of a new query in case any cost estimate constants
have changed.
*/
void init();
/**
Cost of processing a number of records and evaluating the query condition
on the records.
@param rows number of rows to evaluate
@return Cost of evaluating the records
*/
double row_evaluate_cost(double rows) const {
assert(m_initialized);
assert(rows >= 0.0);
return rows * m_server_cost_constants->row_evaluate_cost();
}
/**
Cost of doing a number of key compare operations.
@param keys number of key compare operations
@return Cost of comparing the keys
*/
double key_compare_cost(double keys) const {
assert(m_initialized);
assert(keys >= 0.0);
return keys * m_server_cost_constants->key_compare_cost();
}
private:
/**
Cost of creating a temporary table in the memory storage engine.
*/
double memory_tmptable_create_cost() const {
return m_server_cost_constants->memory_temptable_create_cost();
}
/**
Cost of storing or retrieving a row using the memory storage engine.
*/
double memory_tmptable_row_cost() const {
return m_server_cost_constants->memory_temptable_row_cost();
}
/**
Cost of creating a temporary table using a disk based storage engine.
*/
double disk_tmptable_create_cost() const {
return m_server_cost_constants->disk_temptable_create_cost();
}
/**
Cost of storing or retrieving a row using a disk based storage engine.
*/
double disk_tmptable_row_cost() const {
return m_server_cost_constants->disk_temptable_row_cost();
}
/**
Cost estimate for a row operation (insert, read) on a temporary table.
@param tmptable_type storage type for the temporary table
@return The estimated cost
*/
double tmptable_row_cost(enum_tmptable_type tmptable_type) const {
if (tmptable_type == MEMORY_TMPTABLE) return memory_tmptable_row_cost();
return disk_tmptable_row_cost();
}
public:
/**
Cost estimate for creating a temporary table.
@param tmptable_type storage type for the temporary table
@return Cost estimate
*/
double tmptable_create_cost(enum_tmptable_type tmptable_type) const {
assert(m_initialized);
if (tmptable_type == MEMORY_TMPTABLE) return memory_tmptable_create_cost();
return disk_tmptable_create_cost();
}
/**
Cost estimate for inserting and reading records from a
temporary table.
@param tmptable_type storage type for the temporary table
@param write_rows number of rows that will be written to table
@param read_rows number of rows that will be read from table
@return The estimated cost
*/
double tmptable_readwrite_cost(enum_tmptable_type tmptable_type,
double write_rows, double read_rows) const {
assert(m_initialized);
assert(write_rows >= 0.0);
assert(read_rows >= 0.0);
return (write_rows + read_rows) * tmptable_row_cost(tmptable_type);
}
protected:
friend class Cost_model_table;
/**
Return a pointer to the object containing the current cost constants.
@return Cost constants
*/
const Cost_model_constants *get_cost_constants() const {
assert(m_initialized);
return m_cost_constants;
}
private:
/// Cost constants to use in cost calculations
const Cost_model_constants *m_cost_constants;
protected: // To be able make a gunit fake sub class
/*
Cost constants for the server operations. The purpose for this is
to have direct access to these instead of having to go through the
cost constant set each time these are needed.
*/
const Server_cost_constants *m_server_cost_constants;
#if !defined(NDEBUG)
/**
Used for detecting if this object is used without having been initialized.
*/
bool m_initialized;
#endif
};
/**
API for getting cost estimates for operations on table data.
@note The initial implementation mostly has functions for accessing
cost constants for basic operations.
*/
class Cost_model_table {
public:
Cost_model_table()
: m_cost_model_server(nullptr),
m_se_cost_constants(nullptr),
m_table(nullptr) {
#if !defined(NDEBUG)
m_initialized = false;
#endif
}
/**
Initializes the cost model object.
This function must be called before calling any cost estimation
functions for a query. It should also be called when starting
optimization of a new query in case any cost estimate constants
have changed.
@param cost_model_server the main cost model object for this query
@param table the table the cost model should be used for
*/
void init(const Cost_model_server *cost_model_server, const TABLE *table);
/**
Cost of processing a number of records and evaluating the query condition
on the records.
@param rows number of rows to evaluate
@return Cost of evaluating the records
*/
double row_evaluate_cost(double rows) const {
assert(m_initialized);
assert(rows >= 0.0);
return m_cost_model_server->row_evaluate_cost(rows);
}
/**
Cost of doing a number of key compare operations.
@param keys number of key compare operations
@return Cost of comparing the keys
*/
double key_compare_cost(double keys) const {
assert(m_initialized);
assert(keys >= 0.0);
return m_cost_model_server->key_compare_cost(keys);
}
/**
Cost of reading a number of random blocks from a table.
@param blocks number of blocks to read
@return Cost estimate
*/
double io_block_read_cost(double blocks) const {
assert(m_initialized);
assert(blocks >= 0.0);
return blocks * m_se_cost_constants->io_block_read_cost();
}
/**
Cost of reading a number of blocks from the storage engine when the
block is already in a memory buffer
@param blocks number of blocks to read
@return Cost estimate
*/
double buffer_block_read_cost(double blocks) const {
assert(m_initialized);
assert(blocks >= 0.0);
return blocks * m_se_cost_constants->memory_block_read_cost();
}
/**
Cost of reading a number of random pages from a table.
@param pages number of pages to read
@return Cost estimate
*/
double page_read_cost(double pages) const;
/**
Cost of reading a number of random pages from an index.
@param index the index number
@param pages number of pages to read
@return Cost estimate
*/
double page_read_cost_index(uint index, double pages) const;
/**
The fixed part of the cost for doing a sequential seek on disk.
For a harddisk, this corresponds to half a rotation (see comment
for get_sweep_read_cost() in handler.cc).
*/
double disk_seek_base_cost() const {
assert(m_initialized);
return DISK_SEEK_BASE_COST * io_block_read_cost(1.0);
}
private:
/**
The cost for seeking past one block in a sequential seek.
For a harddisk, this represents the cost of having to move the
disk head to the correct cylinder.
@todo Check that the BLOCKS_IN_AV_SEEK is correct to include in the
DISK_SEEK_PROP_COST
*/
double disk_seek_prop_cost() const {
return DISK_SEEK_PROP_COST * io_block_read_cost(1.0);
}
public:
/**
Cost estimate for a sequential disk seek where a given number of blocks
are skipped.
@param seek_blocks number of blocks to seek past
@return The cost estimate for the seek operation
*/
double disk_seek_cost(double seek_blocks) const {
assert(seek_blocks >= 0.0);
assert(m_initialized);
const double cost =
disk_seek_base_cost() + disk_seek_prop_cost() * seek_blocks;
return cost;
}
protected: // To be able make a gunit fake sub class
/**
Pointer to the cost model for the query. This is used for getting
cost estimates for server operations.
*/
const Cost_model_server *m_cost_model_server;
/**
Cost constants for the storage engine that stores the table.
*/
const SE_cost_constants *m_se_cost_constants;
#if !defined(NDEBUG)
/**
Used for detecting if this object is used without having been initialized.
*/
bool m_initialized;
#endif
private:
/// The table that this is the cost model for
const TABLE *m_table;
};
#endif /* OPT_COSTMODEL_INCLUDED */
|