1
0
mirror of https://github.com/MariaDB/server.git synced 2025-07-29 05:21:33 +03:00

Changing all cost calculation to be given in milliseconds

This makes it easier to compare different costs and also allows
the optimizer to optimizer different storage engines more reliably.

- Added tests/check_costs.pl, a tool to verify optimizer cost calculations.
  - Most engine costs has been found with this program. All steps to
    calculate the new costs are documented in Docs/optimizer_costs.txt

- User optimizer_cost variables are given in microseconds (as individual
  costs can be very small). Internally they are stored in ms.
- Changed DISK_READ_COST (was DISK_SEEK_BASE_COST) from a hard disk cost
  (9 ms) to common SSD cost (400MB/sec).
- Removed cost calculations for hard disks (rotation etc).
- Changed the following handler functions to return IO_AND_CPU_COST.
  This makes it easy to apply different cost modifiers in ha_..time()
  functions for io and cpu costs.
  - scan_time()
  - rnd_pos_time() & rnd_pos_call_time()
  - keyread_time()
- Enhanched keyread_time() to calculate the full cost of reading of a set
  of keys with a given number of ranges and optional number of blocks that
  need to be accessed.
- Removed read_time() as keyread_time() + rnd_pos_time() can do the same
  thing and more.
- Tuned cost for: heap, myisam, Aria, InnoDB, archive and MyRocks.
  Used heap table costs for json_table. The rest are using default engine
  costs.
- Added the following new optimizer variables:
  - optimizer_disk_read_ratio
  - optimizer_disk_read_cost
  - optimizer_key_lookup_cost
  - optimizer_row_lookup_cost
  - optimizer_row_next_find_cost
  - optimizer_scan_cost
- Moved all engine specific cost to OPTIMIZER_COSTS structure.
- Changed costs to use 'records_out' instead of 'records_read' when
  recalculating costs.
- Split optimizer_costs.h to optimizer_costs.h and optimizer_defaults.h.
  This allows one to change costs without having to compile a lot of
  files.
- Updated costs for filter lookup.
- Use a better cost estimate in best_extension_by_limited_search()
  for the sorting cost.
- Fixed previous issues with 'filtered' explain column as we are now
  using 'records_out' (min rows seen for table) to calculate filtering.
  This greatly simplifies the filtering code in
  JOIN_TAB::save_explain_data().

This change caused a lot of queries to be optimized differently than
before, which exposed different issues in the optimizer that needs to
be fixed.  These fixes are in the following commits.  To not have to
change the same test case over and over again, the changes in the test
cases are done in a single commit after all the critical change sets
are done.

InnoDB changes:
- Updated InnoDB to not divide big range cost with 2.
- Added cost for InnoDB (innobase_update_optimizer_costs()).
- Don't mark clustered primary key with HA_KEYREAD_ONLY. This will
  prevent that the optimizer is trying to use index-only scans on
  the clustered key.
- Disabled ha_innobase::scan_time() and ha_innobase::read_time() and
  ha_innobase::rnd_pos_time() as the default engine cost functions now
  works good for InnoDB.

Other things:
- Added  --show-query-costs (\Q) option to mysql.cc to show the query
  cost after each query (good when working with query costs).
- Extended my_getopt with GET_ADJUSTED_VALUE which allows one to adjust
  the value that user is given. This is used to change cost from
  microseconds (user input) to milliseconds (what the server is
  internally using).
- Added include/my_tracker.h  ; Useful include file to quickly test
  costs of a function.
- Use handler::set_table() in all places instead of 'table= arg'.
- Added SHOW_OPTIMIZER_COSTS to sys variables. These are input and
  shown in microseconds for the user but stored as milliseconds.
  This is to make the numbers easier to read for the user (less
  pre-zeros).  Implemented in 'Sys_var_optimizer_cost' class.
- In test_quick_select() do not use index scans if 'no_keyread' is set
  for the table. This is what we do in other places of the server.
- Added THD parameter to Unique::get_use_cost() and
  check_index_intersect_extension() and similar functions to be able
  to provide costs to called functions.
- Changed 'records' to 'rows' in optimizer_trace.
- Write more information to optimizer_trace.
- Added INDEX_BLOCK_FILL_FACTOR_MUL (4) and INDEX_BLOCK_FILL_FACTOR_DIV (3)
  to calculate usage space of keys in b-trees. (Before we used numeric
  constants).
- Removed code that assumed that b-trees has similar costs as binary
  trees. Replaced with engine calls that returns the cost.
- Added Bitmap::find_first_bit()
- Added timings to join_cache for ANALYZE table (patch by Sergei Petrunia).
- Added records_init and records_after_filter to POSITION to remember
  more of what best_access_patch() calculates.
- table_after_join_selectivity() changed to recalculate 'records_out'
  based on the new fields from best_access_patch()

Bug fixes:
- Some queries did not update last_query_cost (was 0). Fixed by moving
  setting thd->...last_query_cost in JOIN::optimize().
- Write '0' as number of rows for const tables with a matching row.

Some internals:
- Engine cost are stored in OPTIMIZER_COSTS structure.  When a
  handlerton is created, we also created a new cost variable for the
  handlerton. We also create a new variable if the user changes a
  optimizer cost for a not yet loaded handlerton either with command
  line arguments or with SET
  @@global.engine.optimizer_cost_variable=xx.
- There are 3 global OPTIMIZER_COSTS variables:
  default_optimizer_costs   The default costs + changes from the
                            command line without an engine specifier.
  heap_optimizer_costs      Heap table costs, used for temporary tables
  tmp_table_optimizer_costs The cost for the default on disk internal
                            temporary table (MyISAM or Aria)
- The engine cost for a table is stored in table_share. To speed up
  accesses the handler has a pointer to this. The cost is copied
  to the table on first access. If one wants to change the cost one
  must first update the global engine cost and then do a FLUSH TABLES.
  This was done to be able to access the costs for an open table
  without any locks.
- When a handlerton is created, the cost are updated the following way:
  See sql/keycaches.cc for details:
  - Use 'default_optimizer_costs' as a base
  - Call hton->update_optimizer_costs() to override with the engines
    default costs.
  - Override the costs that the user has specified for the engine.
  - One handler open, copy the engine cost from handlerton to TABLE_SHARE.
  - Call handler::update_optimizer_costs() to allow the engine to update
    cost for this particular table.
  - There are two costs stored in THD. These are copied to the handler
    when the table is used in a query:
    - optimizer_where_cost
    - optimizer_scan_setup_cost
- Simply code in best_access_path() by storing all cost result in a
  structure. (Idea/Suggestion by Igor)
This commit is contained in:
Monty
2022-08-11 13:05:23 +03:00
committed by Sergei Petrunia
parent 590416e21c
commit b66cdbd1ea
110 changed files with 5373 additions and 1537 deletions

View File

@ -46,6 +46,7 @@
#include "ha_sequence.h"
#include "rowid_filter.h"
#include "mysys_err.h"
#include "optimizer_defaults.h"
#ifdef WITH_PARTITION_STORAGE_ENGINE
#include "ha_partition.h"
@ -621,8 +622,44 @@ int ha_finalize_handlerton(st_plugin_int *plugin)
}
const char *hton_no_exts[]= { 0 };
/*
Get a pointer to the global engine optimizer costs (like
innodb.disk_read_cost) and store the pointer in the handlerton.
This is called once when a handlerton is created.
We also update the not set global costs with the default costs
to allow information_schema to print the real used values.
*/
static bool update_optimizer_costs(handlerton *hton)
{
OPTIMIZER_COSTS costs= default_optimizer_costs;
LEX_CSTRING *name= hton_name(hton);
if (hton->update_optimizer_costs)
hton->update_optimizer_costs(&costs);
mysql_mutex_lock(&LOCK_optimizer_costs);
hton->optimizer_costs= get_or_create_optimizer_costs(name->str,
name->length);
if (!hton->optimizer_costs)
{
mysql_mutex_unlock(&LOCK_optimizer_costs);
return 1; // OOM
}
/* Update not set values from current default costs */
for (uint i=0 ; i < sizeof(OPTIMIZER_COSTS)/sizeof(double) ; i++)
{
double *var= ((double*) hton->optimizer_costs)+i;
if (*var == OPTIMIZER_COST_UNDEF)
*var= ((double*) &costs)[i];
}
mysql_mutex_unlock(&LOCK_optimizer_costs);
return 0;
}
const char *hton_no_exts[]= { 0 };
int ha_initialize_handlerton(st_plugin_int *plugin)
{
@ -725,6 +762,12 @@ int ha_initialize_handlerton(st_plugin_int *plugin)
hton->savepoint_offset= savepoint_alloc_size;
savepoint_alloc_size+= tmp;
hton2plugin[hton->slot]=plugin;
if (plugin->plugin->type == MYSQL_STORAGE_ENGINE_PLUGIN &&
!(hton->flags & HTON_HIDDEN) &&
update_optimizer_costs(hton))
goto err_deinit;
if (hton->prepare)
{
total_ha_2pc++;
@ -764,7 +807,6 @@ int ha_initialize_handlerton(st_plugin_int *plugin)
resolve_sysvar_table_options(hton);
update_discovery_counters(hton, 1);
DBUG_RETURN(0);
err_deinit:
@ -3209,6 +3251,7 @@ handler *handler::clone(const char *name, MEM_ROOT *mem_root)
if (new_handler->ha_open(table, name, table->db_stat,
HA_OPEN_IGNORE_IF_LOCKED, mem_root))
goto err;
new_handler->set_optimizer_costs(ha_thd());
return new_handler;
@ -3240,58 +3283,97 @@ LEX_CSTRING *handler::engine_name()
return hton_name(ht);
}
/*
It is assumed that the value of the parameter 'ranges' can be only 0 or 1.
If ranges == 1 then the function returns the cost of index only scan
by index 'keyno' of one range containing 'rows' key entries.
If ranges == 0 then the function returns only the cost of copying
those key entries into the engine buffers.
Calculate cost for an index scan for given index and number of records.
This function doesn't take in account into copying the key to record
(KEY_COPY_COST) or comparing the key to the where clause (WHERE_COST)
@param index Index to use
@param ranges Number of ranges (b-tree dives in case of b-tree).
Used by partition engine
@param rows Number of expected rows
@param blocks Number of disk blocks to read (from range optimizer).
0 if not known
This function does not take in account into looking up the key,
copying the key to record and finding the next key. These cost are
handled in ha_keyread_time()
*/
double handler::keyread_time(uint index, uint ranges, ha_rows rows)
IO_AND_CPU_COST handler::keyread_time(uint index, ulong ranges, ha_rows rows,
ulonglong blocks)
{
size_t len;
double cost;
DBUG_ASSERT(ranges == 0 || ranges == 1);
len= table->key_info[index].key_length + ref_length;
if (table->file->is_clustering_key(index))
len= table->s->stored_rec_length;
IO_AND_CPU_COST cost;
ulonglong io_blocks= 0;
DBUG_ASSERT(ranges > 0);
cost= ((double)rows*len/(stats.block_size+1) *
INDEX_BLOCK_COPY_COST(table->in_use));
/*
We divide the cost with optimizer_cache_cost as ha_keyread_time()
and ha_key_scan_time() will multiply the result value with
optimizer_cache_cost and we want to keep the above 'memory operation'
cost unaffected by this multiplication.
*/
cost/= optimizer_cache_cost;
if (ranges)
/* memory engine has stats.block_size == 0 */
if (stats.block_size)
{
uint keys_per_block= (uint) (stats.block_size*3/4/len+1);
/*
We let the cost grow slowly in proportion to number of rows to
promote indexes with less rows.
We do not calculate exact number of block reads as then index
only reads will be more costly than normal reads, especially
compared to InnoDB clustered keys.
KEY_LOOKUP_COST is the cost of finding the first key in the
range. Finding the next key is usually a fast operation so we
don't count it here, it is taken into account in
ha_keyread_and_copy_time()
*/
cost+= (((double) (rows / keys_per_block) + KEY_LOOKUP_COST) *
avg_io_cost());
if (!blocks)
{
/* Estimate length of index data */
if (rows <= 1) // EQ_REF optimization
{
blocks= 1;
io_blocks= (stats.block_size + IO_SIZE - 1)/ IO_SIZE;
}
else
{
size_t len= table->key_storage_length(index);
blocks= ((ulonglong) ((rows * len / INDEX_BLOCK_FILL_FACTOR_DIV *
INDEX_BLOCK_FILL_FACTOR_MUL +
stats.block_size-1)) / stats.block_size +
(ranges - 1));
io_blocks= blocks * stats.block_size / IO_SIZE;
}
}
else
io_blocks= blocks * stats.block_size / IO_SIZE;
}
cost.io= (double) io_blocks * avg_io_cost();
cost.cpu= blocks * INDEX_BLOCK_COPY_COST;
return cost;
}
/*
Cost of doing a set of range scans and finding the key position.
This function is used both with index scans (in which case there should be
an additional KEY_COPY_COST) and when normal index + fetch row scan,
in which case there should an additional rnd_pos_time() cost.
*/
double handler::ha_keyread_time(uint index, ulong ranges, ha_rows rows,
ulonglong blocks)
{
if (rows < ranges)
rows= ranges;
IO_AND_CPU_COST cost= keyread_time(index, ranges, rows, blocks);
return (cost.io * DISK_READ_RATIO +
cost.cpu + ranges * KEY_LOOKUP_COST +
(rows - ranges) * KEY_NEXT_FIND_COST);
}
/*
Read a row from a clustered index
Cost is similar to ha_rnd_pos_call_time() as a index_read() on a clusterd
key has identical code as rnd_pos() (At least in InnoDB:)
*/
double handler::ha_keyread_clustered_and_copy_time(uint index, ulong ranges,
ha_rows rows,
ulonglong blocks)
{
if (rows < ranges)
rows= ranges;
IO_AND_CPU_COST cost= keyread_time(index, ranges, rows, blocks);
return (cost.io * DISK_READ_RATIO +
cost.cpu + ranges * ROW_LOOKUP_COST +
(rows - ranges) * ROW_NEXT_FIND_COST +
rows * ROW_COPY_COST);
}
THD *handler::ha_thd(void) const
{
DBUG_ASSERT(!table || !table->in_use || table->in_use == current_thd);
@ -3364,7 +3446,7 @@ int handler::ha_open(TABLE *table_arg, const char *name, int mode,
name, ht->db_type, table_arg->db_stat, mode,
test_if_locked));
table= table_arg;
set_table(table_arg);
DBUG_ASSERT(table->s == table_share);
DBUG_ASSERT(m_lock_type == F_UNLCK);
DBUG_PRINT("info", ("old m_lock_type: %d F_UNLCK %d", m_lock_type, F_UNLCK));
@ -3414,14 +3496,15 @@ int handler::ha_open(TABLE *table_arg, const char *name, int mode,
else
dup_ref=ref+ALIGN_SIZE(ref_length);
cached_table_flags= table_flags();
if (!table->s->optimizer_costs_inited)
{
table->s->optimizer_costs_inited=1;
/* Copy data from global 'engine'.optimizer_costs to TABLE_SHARE */
table->s->update_optimizer_costs(partition_ht());
/* Update costs depend on table structure */
update_optimizer_costs(&table->s->optimizer_costs);
}
/* Copy current optimizer costs. Needed in case clone() is used */
set_optimizer_costs(table->in_use);
DBUG_ASSERT(optimizer_key_copy_cost >= 0.0);
DBUG_ASSERT(optimizer_key_next_find_cost >= 0.0);
DBUG_ASSERT(optimizer_row_copy_cost >= 0.0);
DBUG_ASSERT(optimizer_where_cost >= 0.0);
DBUG_ASSERT(optimizer_key_cmp_cost >= 0.0);
reset_statistics();
}
internal_tmp_table= MY_TEST(test_if_locked & HA_OPEN_INTERNAL_TABLE);
@ -3453,6 +3536,15 @@ int handler::ha_close(void)
DBUG_RETURN(close());
}
void handler::change_table_ptr(TABLE *table_arg, TABLE_SHARE *share)
{
DBUG_ASSERT(table_arg->s == share);
table= table_arg;
table_share= share;
costs= &share->optimizer_costs;
reset_statistics();
}
int handler::ha_rnd_next(uchar *buf)
{
@ -8767,27 +8859,19 @@ Table_scope_and_contents_source_st::fix_period_fields(THD *thd,
}
/*
Copy common optimizer cost variables to the engine
Copy upper level cost to the engine as part of start statement
This is needed to provide fast acccess to these variables during
optimization (as we refer to them multiple times).
This is needed to provide fast access to these variables during
optimization (as we refer to them multiple times during one query).
The other option would be to access them from thd, but that
would require a function call (as we cannot access THD from
an inline handler function) and two extra memory accesses
for each variable.
index_block_copy_cost is not copied as it is used so seldom.
The other option would be to access them from THD, but that would
require a function call (as we cannot easily access THD from an
inline handler function) and two extra memory accesses for each
variable.
*/
void handler::set_optimizer_costs(THD *thd)
{
optimizer_key_copy_cost= thd->variables.optimizer_key_copy_cost;
optimizer_key_next_find_cost=
thd->variables.optimizer_key_next_find_cost;
optimizer_row_copy_cost= thd->variables.optimizer_row_copy_cost;
optimizer_where_cost= thd->variables.optimizer_where_cost;
optimizer_key_cmp_cost= thd->variables.optimizer_key_cmp_cost;
set_optimizer_cache_cost(thd->optimizer_cache_hit_ratio);
optimizer_where_cost= thd->variables.optimizer_where_cost;
optimizer_scan_setup_cost= thd->variables.optimizer_scan_setup_cost;
}