mirror of
https://github.com/MariaDB/server.git
synced 2025-08-08 11:22:35 +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:
@@ -20,6 +20,7 @@
|
||||
#include "key.h"
|
||||
#include "sql_statistics.h"
|
||||
#include "rowid_filter.h"
|
||||
#include "optimizer_defaults.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Default MRR implementation (MRR to non-MRR converter)
|
||||
@@ -302,46 +303,37 @@ handler::multi_range_read_info_const(uint keyno, RANGE_SEQ_IF *seq,
|
||||
|
||||
if (total_rows != HA_POS_ERROR)
|
||||
{
|
||||
double io_cost= avg_io_cost();
|
||||
double range_lookup_cost= (io_cost * KEY_LOOKUP_COST *
|
||||
optimizer_cache_cost);
|
||||
double key_cost;
|
||||
set_if_smaller(total_rows, max_rows);
|
||||
|
||||
/* The following calculation is the same as in multi_range_read_info(): */
|
||||
*flags |= HA_MRR_USE_DEFAULT_IMPL;
|
||||
cost->reset();
|
||||
cost->avg_io_cost= cost->idx_avg_io_cost= io_cost;
|
||||
cost->avg_io_cost= cost->idx_avg_io_cost= 0; // Not used!
|
||||
|
||||
if (!is_clustering_key(keyno))
|
||||
{
|
||||
cost->idx_io_count= (double) io_blocks;
|
||||
key_cost= ha_keyread_time(keyno, n_ranges, total_rows, io_blocks);
|
||||
cost->idx_cpu_cost= key_cost;
|
||||
|
||||
if (!(*flags & HA_MRR_INDEX_ONLY))
|
||||
{
|
||||
cost->idx_cpu_cost= (ha_keyread_time(keyno, 1, total_rows) +
|
||||
(n_ranges-1) * range_lookup_cost);
|
||||
cost->cpu_cost= ha_read_time(keyno, 0, total_rows);
|
||||
cost->copy_cost= rows2double(total_rows) * ROW_COPY_COST;
|
||||
/* ha_rnd_pos_time includes ROW_COPY_COST */
|
||||
cost->cpu_cost= ha_rnd_pos_time(total_rows);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Index only read */
|
||||
cost->idx_cpu_cost= (ha_keyread_time(keyno, 1, total_rows) +
|
||||
(n_ranges-1) * range_lookup_cost);
|
||||
cost->copy_cost= rows2double(total_rows) * KEY_COPY_COST;
|
||||
cost->copy_cost= rows2double(total_rows) * KEY_COPY_COST;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/*
|
||||
Clustered index
|
||||
If all index dives are to a few blocks, then limit the
|
||||
ranges used by read_time to the number of dives.
|
||||
*/
|
||||
/* Clustered index */
|
||||
io_blocks+= unassigned_single_point_ranges;
|
||||
uint limited_ranges= (uint) MY_MIN((ulonglong) n_ranges, io_blocks);
|
||||
cost->idx_cpu_cost= limited_ranges * range_lookup_cost;
|
||||
cost->cpu_cost= ha_read_time(keyno, 0, total_rows);
|
||||
cost->copy_cost= rows2double(total_rows) * ROW_COPY_COST;
|
||||
key_cost= ha_keyread_time(keyno, n_ranges, total_rows, io_blocks);
|
||||
cost->idx_cpu_cost= key_cost;
|
||||
cost->copy_cost= rows2double(total_rows) * ROW_COPY_COST;
|
||||
}
|
||||
cost->comp_cost= (rows2double(total_rows) * WHERE_COST +
|
||||
MULTI_RANGE_READ_SETUP_COST);
|
||||
@@ -378,7 +370,7 @@ handler::multi_range_read_info_const(uint keyno, RANGE_SEQ_IF *seq,
|
||||
@param keyno Index number
|
||||
@param n_ranges Estimated number of ranges (i.e. intervals) in the
|
||||
range sequence.
|
||||
@param n_rows Estimated total number of records contained within all
|
||||
@param total_rows Estimated total number of records contained within all
|
||||
of the ranges
|
||||
@param bufsz INOUT IN: Size of the buffer available for use
|
||||
OUT: Size of the buffer that will be actually used, or
|
||||
@@ -393,7 +385,7 @@ handler::multi_range_read_info_const(uint keyno, RANGE_SEQ_IF *seq,
|
||||
other Error or can't perform the requested scan
|
||||
*/
|
||||
|
||||
ha_rows handler::multi_range_read_info(uint keyno, uint n_ranges, uint n_rows,
|
||||
ha_rows handler::multi_range_read_info(uint keyno, uint n_ranges, uint total_rows,
|
||||
uint key_parts, uint *bufsz,
|
||||
uint *flags, Cost_estimate *cost)
|
||||
{
|
||||
@@ -410,38 +402,27 @@ ha_rows handler::multi_range_read_info(uint keyno, uint n_ranges, uint n_rows,
|
||||
/* Produce the same cost as non-MRR code does */
|
||||
if (!is_clustering_key(keyno))
|
||||
{
|
||||
double range_lookup_cost= (avg_io_cost() * KEY_LOOKUP_COST *
|
||||
optimizer_cache_cost);
|
||||
/*
|
||||
idx_io_count could potentially be increased with the number of
|
||||
index leaf blocks we have to read for finding n_rows.
|
||||
*/
|
||||
cost->idx_io_count= n_ranges;
|
||||
double key_cost= ha_keyread_time(keyno, n_ranges, total_rows, 0);
|
||||
cost->idx_cpu_cost= key_cost;
|
||||
|
||||
if (!(*flags & HA_MRR_INDEX_ONLY))
|
||||
{
|
||||
cost->idx_cpu_cost= (keyread_time(keyno, 1, n_rows) +
|
||||
(n_ranges-1) * range_lookup_cost);
|
||||
cost->cpu_cost= read_time(keyno, 0, n_rows);
|
||||
cost->copy_cost= rows2double(n_rows) * ROW_COPY_COST;
|
||||
/* ha_rnd_pos_time includes ROW_COPY_COST */
|
||||
cost->cpu_cost= ha_rnd_pos_time(total_rows);
|
||||
}
|
||||
else
|
||||
{
|
||||
/*
|
||||
Same as above, but take into account copying the key to the upper
|
||||
level.
|
||||
*/
|
||||
cost->idx_cpu_cost= (keyread_time(keyno, 1, n_rows) +
|
||||
(n_ranges-1) * range_lookup_cost);
|
||||
cost->copy_cost= rows2double(n_rows) * KEY_COPY_COST;
|
||||
/* Index only read */
|
||||
cost->copy_cost= rows2double(total_rows) * KEY_COPY_COST;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Clustering key */
|
||||
cost->cpu_cost= read_time(keyno, n_ranges, n_rows);
|
||||
cost->copy_cost= rows2double(n_rows) * ROW_COPY_COST;
|
||||
cost->cpu_cost= ha_keyread_time(keyno, n_ranges, total_rows, 0);
|
||||
cost->copy_cost= rows2double(total_rows) * ROW_COPY_COST;
|
||||
}
|
||||
cost->comp_cost= rows2double(n_rows) * WHERE_COST;
|
||||
cost->comp_cost= rows2double(total_rows) * WHERE_COST;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -2043,7 +2024,7 @@ bool DsMrr_impl::get_disk_sweep_mrr_cost(uint keynr, ha_rows rows, uint flags,
|
||||
cost->mem_cost= (double)rows_in_last_step * elem_size;
|
||||
|
||||
/* Total cost of all index accesses */
|
||||
index_read_cost= primary_file->ha_keyread_and_copy_time(keynr, 1, rows);
|
||||
index_read_cost= primary_file->ha_keyread_and_copy_time(keynr, 1, rows, 0);
|
||||
cost->add_io(index_read_cost, 1 /* Random seeks */);
|
||||
return FALSE;
|
||||
}
|
||||
@@ -2081,42 +2062,6 @@ void get_sort_and_sweep_cost(TABLE *table, ha_rows nrows, Cost_estimate *cost)
|
||||
/**
|
||||
Get cost of reading nrows table records in a "disk sweep"
|
||||
|
||||
A disk sweep read is a sequence of handler->rnd_pos(rowid) calls that made
|
||||
for an ordered sequence of rowids.
|
||||
|
||||
We assume hard disk IO. The read is performed as follows:
|
||||
|
||||
1. The disk head is moved to the needed cylinder
|
||||
2. The controller waits for the plate to rotate
|
||||
3. The data is transferred
|
||||
|
||||
Time to do #3 is insignificant compared to #2+#1.
|
||||
|
||||
Time to move the disk head is proportional to head travel distance.
|
||||
|
||||
Time to wait for the plate to rotate depends on whether the disk head
|
||||
was moved or not.
|
||||
|
||||
If disk head wasn't moved, the wait time is proportional to distance
|
||||
between the previous block and the block we're reading.
|
||||
|
||||
If the head was moved, we don't know how much we'll need to wait for the
|
||||
plate to rotate. We assume the wait time to be a variate with a mean of
|
||||
0.5 of full rotation time.
|
||||
|
||||
Our cost units are "random disk seeks". The cost of random disk seek is
|
||||
actually not a constant, it depends one range of cylinders we're going
|
||||
to access. We make it constant by introducing a fuzzy concept of "typical
|
||||
datafile length" (it's fuzzy as it's hard to tell whether it should
|
||||
include index file, temp.tables etc). Then random seek cost is:
|
||||
|
||||
1 = half_rotation_cost + move_cost * 1/3 * typical_data_file_length
|
||||
|
||||
We define half_rotation_cost as DISK_SEEK_BASE_COST=0.9.
|
||||
|
||||
If handler::avg_io_cost() < 1.0, then we will trust the handler
|
||||
when it comes to the average cost (this is for example true for HEAP).
|
||||
|
||||
@param table Table to be accessed
|
||||
@param nrows Number of rows to retrieve
|
||||
@param interrupted TRUE <=> Assume that the disk sweep will be
|
||||
@@ -2131,8 +2076,7 @@ void get_sweep_read_cost(TABLE *table, ha_rows nrows, bool interrupted,
|
||||
|
||||
cost->reset();
|
||||
#ifndef OLD_SWEEP_COST
|
||||
cost->cpu_cost= table->file->ha_rnd_pos_time(nrows);
|
||||
cost->avg_io_cost= table->file->avg_io_cost();
|
||||
cost->cpu_cost= table->file->ha_rnd_pos_call_time(nrows);
|
||||
#else
|
||||
if (table->file->pk_is_clustering_key(table->s->primary_key))
|
||||
{
|
||||
|
Reference in New Issue
Block a user