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 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
|
#ifndef OPT_COSTCONSTANTS_INCLUDED
#define OPT_COSTCONSTANTS_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 "lex_string.h"
#include "prealloced_array.h"
class THD;
struct TABLE;
/**
Error codes returned from the functions that do updates of the
cost constants.
*/
enum cost_constant_error {
COST_CONSTANT_OK,
UNKNOWN_COST_NAME,
UNKNOWN_ENGINE_NAME,
INVALID_COST_VALUE,
INVALID_DEVICE_TYPE
};
/**
The cost model should support different types of storage devices each with
different cost constants. Due to that we in the current version does not
have a way to know which storage device a given table is stored on, the
initial version of the cost model will only have one set of cost constants
per storage engine.
*/
const unsigned int MAX_STORAGE_CLASSES = 1;
/**
Cost constants for operations done by the server
*/
class Server_cost_constants {
public:
/**
Creates a server cost constants object using the default values
defined in this class.
*/
Server_cost_constants()
: m_row_evaluate_cost(ROW_EVALUATE_COST),
m_key_compare_cost(KEY_COMPARE_COST),
m_memory_temptable_create_cost(MEMORY_TEMPTABLE_CREATE_COST),
m_memory_temptable_row_cost(MEMORY_TEMPTABLE_ROW_COST),
m_disk_temptable_create_cost(DISK_TEMPTABLE_CREATE_COST),
m_disk_temptable_row_cost(DISK_TEMPTABLE_ROW_COST) {}
/**
Cost for evaluating the query condition on a row.
*/
double row_evaluate_cost() const { return m_row_evaluate_cost; }
/**
Cost for comparing two keys.
*/
double key_compare_cost() const { return m_key_compare_cost; }
/**
Cost for creating an internal temporary table in memory.
*/
double memory_temptable_create_cost() const {
return m_memory_temptable_create_cost;
}
/**
Cost for retrieving or storing a row in an internal temporary table
stored in memory.
*/
double memory_temptable_row_cost() const {
return m_memory_temptable_row_cost;
}
/**
Cost for creating an internal temporary table in a disk resident
storage engine.
*/
double disk_temptable_create_cost() const {
return m_disk_temptable_create_cost;
}
/**
Cost for retrieving or storing a row in an internal disk resident
temporary table.
*/
double disk_temptable_row_cost() const { return m_disk_temptable_row_cost; }
/**
Set the value of one of the cost constants.
@param name name of cost constant
@param value new value
@return Status for updating the cost constant
*/
cost_constant_error set(const LEX_CSTRING &name, const double value);
private:
/*
This section declares constants for the default values. The actual
default values are found in the .cc file.
*/
/// Default cost for evaluation of the query condition for a row.
static const double ROW_EVALUATE_COST;
/// Default cost for comparing row ids.
static const double KEY_COMPARE_COST;
/*
Constants related to the use of temporary tables in query execution.
Lookup and write operations are currently assumed to be equally costly
(concerns MEMORY_TEMPTABLE_ROW_COST and DISK_TEMPTABLE_ROW_COST).
*/
/// Cost for creating a memory temporary table.
static const double MEMORY_TEMPTABLE_CREATE_COST;
/// Cost for inserting or reading a row in a memory temporary table.
static const double MEMORY_TEMPTABLE_ROW_COST;
/// Cost for creating a disk temporary table
static const double DISK_TEMPTABLE_CREATE_COST;
/// Cost for inserting or reading a row in a disk temporary table.
static const double DISK_TEMPTABLE_ROW_COST;
/*
This section specifies cost constants for server operations
*/
/// Cost for evaluating the query condition on a row
double m_row_evaluate_cost;
/// Cost for comparing two keys
double m_key_compare_cost;
/// Cost for creating an internal temporary table in memory
double m_memory_temptable_create_cost;
/**
Cost for retrieving or storing a row in an internal temporary table
stored in memory.
*/
double m_memory_temptable_row_cost;
/**
Cost for creating an internal temporary table in a disk resident
storage engine.
*/
double m_disk_temptable_create_cost;
/**
Cost for retrieving or storing a row in an internal disk resident
temporary table.
*/
double m_disk_temptable_row_cost;
};
/**
Cost constants for a storage engine.
Storage engines that want to add new cost constants should make
a subclass of this class.
*/
class SE_cost_constants {
public:
SE_cost_constants()
: m_memory_block_read_cost(MEMORY_BLOCK_READ_COST),
m_io_block_read_cost(IO_BLOCK_READ_COST),
m_memory_block_read_cost_default(true),
m_io_block_read_cost_default(true) {}
virtual ~SE_cost_constants() = default;
/**
Cost of reading one random block from an in-memory database buffer.
*/
double memory_block_read_cost() const { return m_memory_block_read_cost; }
/**
Cost of reading one random block from disk.
*/
double io_block_read_cost() const { return m_io_block_read_cost; }
protected:
/**
Set the value of one of the cost constants.
If a storage engine wants to introduce a new cost constant, it should
provide an implementation of this function. If the cost constant
is not recognized by the function in the subclass, then this function
should be called to allow the cost constant in the base class to be
given the updated value.
@param name name of cost constant
@param value new value
@param default_value specify whether the new value is a default value or
an engine specific value
@return Status for updating the cost constant
*/
virtual cost_constant_error set(const LEX_CSTRING &name, const double value,
bool default_value);
protected:
friend class Cost_model_constants;
/**
Update the value of a cost constant.
@param name name of the cost constant
@param value the new value this cost constant should take
@return Status for updating the cost constant
*/
cost_constant_error update(const LEX_CSTRING &name, const double value);
/**
Update the default value of a cost constant.
If this const constant already has been given a non-default value,
then calling this will have no effect on the current value for the
cost constant.
@param name name of the cost constant
@param value the new value this cost constant should take
@return Status for updating the cost constant
*/
cost_constant_error update_default(const LEX_CSTRING &name,
const double value);
/**
Utility function for changing the value of a cost constant.
The cost constant will be updated to the new value iff:
a) the current value is the default value, or
b) the current value is not the default value and the new value
is not a default value
@param[out] cost_constant pointer to the cost constant that
should be updated
@param[in,out] cost_constant_is_default whether the current value has the
default value or not
@param new_value the new value for the cost constant
@param new_value_is_default whether this is a new default value
or not
*/
void update_cost_value(double *cost_constant, bool *cost_constant_is_default,
double new_value, bool new_value_is_default);
private:
/*
This section specifies default values for cost constants.
*/
/// Default cost for reading a random block from an in-memory buffer
static const double MEMORY_BLOCK_READ_COST;
/// Default cost for reading a random disk block
static const double IO_BLOCK_READ_COST;
/*
This section specifies cost constants for the table
*/
/// Cost constant for reading a random block from an in-memory buffer
double m_memory_block_read_cost;
/// Cost constant for reading a random disk block.
double m_io_block_read_cost;
/*
This section has boolean variables that is used for knowing whether
the above cost variables is using the default value or not.
*/
/// Whether the memory_block_read_cost is a default value or not
bool m_memory_block_read_cost_default;
/// Whether the io_block_read_cost is a default value or not
bool m_io_block_read_cost_default;
};
/**
Class that keeps all cost constants for a storage engine. Since
storage engines can use different types of storage devices, each
device type can have its own set of cost constants.
@note In the initial implementation there will only be one
set of cost constants per storage engine.
*/
class Cost_model_se_info {
public:
/**
Constructor that just initializes the class.
*/
Cost_model_se_info();
/**
Destructor. Deletes the allocated cost constant objects.
*/
~Cost_model_se_info();
private:
/*
Since this object owns the cost constant objects, we must prevent that we
create copies of this object that share the cost constant objects.
*/
Cost_model_se_info(const Cost_model_se_info &);
Cost_model_se_info &operator=(const Cost_model_se_info &rhs);
protected:
friend class Cost_model_constants;
/**
Set the storage constants to be used for a given storage type for
this storage engine.
@param cost_constants cost constants for the storage engine
@param storage_class the storage class these cost constants should be
used for
*/
void set_cost_constants(SE_cost_constants *cost_constants,
unsigned int storage_class) {
assert(cost_constants != nullptr);
assert(storage_class < MAX_STORAGE_CLASSES);
assert(m_se_cost_constants[storage_class] == nullptr);
m_se_cost_constants[storage_class] = cost_constants;
}
/**
Retrieve the cost constants to be used for this storage engine for
a specified storage class.
@param storage_class the storage class these cost constants should be
used for
*/
const SE_cost_constants *get_cost_constants(
unsigned int storage_class) const {
assert(storage_class < MAX_STORAGE_CLASSES);
assert(m_se_cost_constants[storage_class] != nullptr);
return m_se_cost_constants[storage_class];
}
/**
Retrieve the cost constants to be used for this storage engine for
a specified storage class.
@param storage_class the storage class these cost constants should be
used for
*/
SE_cost_constants *get_cost_constants(unsigned int storage_class) {
assert(storage_class < MAX_STORAGE_CLASSES);
assert(m_se_cost_constants[storage_class] != nullptr);
return m_se_cost_constants[storage_class];
}
private:
/**
Array of cost constant sets for this storage engine. There will
be one set of cost constants for each device type defined for the
storage engine.
*/
SE_cost_constants *m_se_cost_constants[MAX_STORAGE_CLASSES];
};
/**
Set of all cost constants used by the server and all storage engines.
*/
class Cost_model_constants {
public:
/**
Creates a set with cost constants using the default values defined in
the source code.
*/
Cost_model_constants();
/**
Destructor.
@note The only reason for making this virtual is to be able to make
a sub-class for use in unit testing.
*/
virtual ~Cost_model_constants();
/**
Get the cost constants that should be used for server operations.
@return the cost constants for the server
*/
const Server_cost_constants *get_server_cost_constants() const {
return &m_server_constants;
}
/**
Return the cost constants that should be used for a given table.
@param table the table to find cost constants for
@return the cost constants to use for the table
*/
const SE_cost_constants *get_se_cost_constants(const TABLE *table) const;
/**
Update the value for one of the server cost constants.
@param name name of the cost constant
@param value new value
@return Status for updating the cost constant
*/
cost_constant_error update_server_cost_constant(const LEX_CSTRING &name,
double value);
/**
Update the value for one of the storage engine cost constants.
@param thd the THD
@param se_name name of storage engine
@param storage_category storage device type
@param name name of cost constant
@param value new value
@return Status for updating the cost constant
*/
cost_constant_error update_engine_cost_constant(THD *thd,
const LEX_CSTRING &se_name,
uint storage_category,
const LEX_CSTRING &name,
double value);
protected:
friend class Cost_constant_cache;
/**
Increment the reference counter for this cost constant set
*/
void inc_ref_count() { m_ref_counter++; }
/**
Decrement the reference counter for this cost constant set
When the returned value is zero, there is nobody using this object
and it can be deleted by the caller.
@return the updated reference count
*/
unsigned int dec_ref_count() {
assert(m_ref_counter > 0);
m_ref_counter--;
return m_ref_counter;
}
private:
/**
Utility function for finding the slot number for a storage engine
based on the storage engine name.
The only reason for making this function virtual is to be able to
override it in unit tests.
@param thd the THD
@param name name of storage engine
@return slot number for the storage engine, HA_SLOT_UNDEF if there
is no handler for this name
*/
virtual uint find_handler_slot_from_name(THD *thd,
const LEX_CSTRING &name) const;
/**
Update the default value for a storage engine cost constant.
@param name name of cost constant
@param storage_category storage device type
@param value new value
@return Status for updating the cost constant
*/
cost_constant_error update_engine_default_cost(const LEX_CSTRING &name,
uint storage_category,
double value);
/// Cost constants for server operations
Server_cost_constants m_server_constants;
/**
Cost constants for storage engines
15 should be enough for most use cases, see PREALLOC_NUM_HA.
*/
Prealloced_array<Cost_model_se_info, 15> m_engines;
/// Reference counter for this set of cost constants.
unsigned int m_ref_counter;
};
#endif /* OPT_COSTCONSTANTS_INCLUDEDED */
|