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:
@@ -35,6 +35,7 @@
|
||||
#include "sql_test.h"
|
||||
#include <my_bit.h>
|
||||
#include "opt_trace.h"
|
||||
#include "optimizer_defaults.h"
|
||||
|
||||
/*
|
||||
This file contains optimizations for semi-join subqueries.
|
||||
@@ -1471,8 +1472,8 @@ void get_delayed_table_estimates(TABLE *table,
|
||||
hash_sj_engine->tmp_table->s->reclength);
|
||||
|
||||
/* Do like in handler::ha_scan_and_compare_time, but ignore the where cost */
|
||||
*scan_time= ((data_size/table->file->stats.block_size+2) *
|
||||
table->file->avg_io_cost()) + *out_rows * file->ROW_COPY_COST;
|
||||
*scan_time= ((data_size/IO_SIZE * table->file->avg_io_cost()) +
|
||||
*out_rows * file->ROW_COPY_COST);
|
||||
}
|
||||
|
||||
|
||||
@@ -2595,11 +2596,9 @@ bool optimize_semijoin_nests(JOIN *join, table_map all_table_map)
|
||||
We don't need to check the where clause for each row, so no
|
||||
WHERE_COST is needed.
|
||||
*/
|
||||
scan_cost= (TABLE_SCAN_SETUP_COST +
|
||||
(cost.block_size == 0 ? 0 :
|
||||
((rowlen * (double) sjm->rows) / cost.block_size +
|
||||
TABLE_SCAN_SETUP_COST)));
|
||||
scan_cost= (rowlen * (double) sjm->rows) / cost.block_size;
|
||||
total_cost= (scan_cost * cost.cache_hit_ratio * cost.avg_io_cost +
|
||||
TABLE_SCAN_SETUP_COST_THD(thd) +
|
||||
row_copy_cost * sjm->rows);
|
||||
sjm->scan_cost.convert_from_cost(total_cost);
|
||||
|
||||
@@ -2699,8 +2698,6 @@ get_tmp_table_costs(THD *thd, double row_count, uint row_size, bool blobs_used,
|
||||
bool add_copy_cost)
|
||||
{
|
||||
TMPTABLE_COSTS cost;
|
||||
double row_copy_cost= add_copy_cost ? ROW_COPY_COST_THD(thd) : 0;
|
||||
|
||||
/* From heap_prepare_hp_create_info(), assuming one hash key used */
|
||||
row_size+= sizeof(char*)*2;
|
||||
row_size= MY_ALIGN(MY_MAX(row_size, sizeof(char*)) + 1, sizeof(char*));
|
||||
@@ -2708,24 +2705,31 @@ get_tmp_table_costs(THD *thd, double row_count, uint row_size, bool blobs_used,
|
||||
if (row_count > thd->variables.max_heap_table_size / (double) row_size ||
|
||||
blobs_used)
|
||||
{
|
||||
double row_copy_cost= (add_copy_cost ?
|
||||
tmp_table_optimizer_costs.row_copy_cost :
|
||||
0);
|
||||
/* Disk based table */
|
||||
cost.lookup= ((DISK_TEMPTABLE_LOOKUP_COST *
|
||||
thd->optimizer_cache_hit_ratio)) + row_copy_cost;
|
||||
cost.write= cost.lookup + row_copy_cost;
|
||||
cost.lookup= ((tmp_table_optimizer_costs.key_lookup_cost *
|
||||
tmp_table_optimizer_costs.disk_read_ratio) +
|
||||
row_copy_cost);
|
||||
cost.write= cost.lookup;
|
||||
cost.create= DISK_TEMPTABLE_CREATE_COST;
|
||||
cost.block_size= DISK_TEMPTABLE_BLOCK_SIZE;
|
||||
cost.avg_io_cost= 1.0;
|
||||
cost.cache_hit_ratio= thd->optimizer_cache_hit_ratio;
|
||||
cost.avg_io_cost= tmp_table_optimizer_costs.disk_read_cost;
|
||||
cost.cache_hit_ratio= tmp_table_optimizer_costs.disk_read_ratio;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Values are as they are in heap.h */
|
||||
double row_copy_cost= (add_copy_cost ?
|
||||
heap_optimizer_costs.row_copy_cost :
|
||||
0);
|
||||
cost.lookup= HEAP_TEMPTABLE_LOOKUP_COST + row_copy_cost;
|
||||
cost.write= cost.lookup + row_copy_cost;
|
||||
cost.write= cost.lookup;
|
||||
cost.create= HEAP_TEMPTABLE_CREATE_COST;
|
||||
cost.block_size= 0;
|
||||
cost.avg_io_cost= HEAP_TEMPTABLE_LOOKUP_COST;
|
||||
cost.cache_hit_ratio= 1.0;
|
||||
cost.block_size= 1;
|
||||
cost.avg_io_cost= 0;
|
||||
cost.cache_hit_ratio= 0;
|
||||
}
|
||||
return cost;
|
||||
}
|
||||
@@ -3196,7 +3200,7 @@ bool Sj_materialization_picker::check_qep(JOIN *join,
|
||||
if (unlikely(trace.trace_started()))
|
||||
{
|
||||
trace.
|
||||
add("records", *record_count).
|
||||
add("rows", *record_count).
|
||||
add("cost", *read_time);
|
||||
}
|
||||
return TRUE;
|
||||
@@ -3250,7 +3254,7 @@ bool Sj_materialization_picker::check_qep(JOIN *join,
|
||||
best_access_path(join, join->positions[i].table, rem_tables,
|
||||
join->positions, i,
|
||||
disable_jbuf, prefix_rec_count, &curpos, &dummy);
|
||||
prefix_rec_count= COST_MULT(prefix_rec_count, curpos.records_read);
|
||||
prefix_rec_count= COST_MULT(prefix_rec_count, curpos.records_out);
|
||||
prefix_cost= COST_ADD(prefix_cost, curpos.read_time);
|
||||
//TODO: take into account join condition selectivity here
|
||||
}
|
||||
@@ -3277,7 +3281,7 @@ bool Sj_materialization_picker::check_qep(JOIN *join,
|
||||
if (unlikely(trace.trace_started()))
|
||||
{
|
||||
trace.
|
||||
add("records", *record_count).
|
||||
add("rows", *record_count).
|
||||
add("cost", *read_time);
|
||||
}
|
||||
return TRUE;
|
||||
@@ -3378,7 +3382,7 @@ bool LooseScan_picker::check_qep(JOIN *join,
|
||||
if (unlikely(trace.trace_started()))
|
||||
{
|
||||
trace.
|
||||
add("records", *record_count).
|
||||
add("rows", *record_count).
|
||||
add("cost", *read_time);
|
||||
}
|
||||
return TRUE;
|
||||
@@ -3476,7 +3480,7 @@ bool Firstmatch_picker::check_qep(JOIN *join,
|
||||
- remove fanout added by the last table
|
||||
*/
|
||||
if (*record_count)
|
||||
*record_count /= join->positions[idx].records_read;
|
||||
*record_count /= join->positions[idx].records_out;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -3497,7 +3501,7 @@ bool Firstmatch_picker::check_qep(JOIN *join,
|
||||
if (unlikely(trace.trace_started()))
|
||||
{
|
||||
trace.
|
||||
add("records", *record_count).
|
||||
add("rows", *record_count).
|
||||
add("cost", *read_time);
|
||||
}
|
||||
return TRUE;
|
||||
@@ -3624,21 +3628,22 @@ bool Duplicate_weedout_picker::check_qep(JOIN *join,
|
||||
*/
|
||||
uint first_tab= first_dupsweedout_table;
|
||||
double dups_cost;
|
||||
double prefix_rec_count;
|
||||
double first_weedout_table_rec_count;
|
||||
double sj_inner_fanout= 1.0;
|
||||
double sj_outer_fanout= 1.0;
|
||||
uint temptable_rec_size;
|
||||
|
||||
if (first_tab == join->const_tables)
|
||||
{
|
||||
prefix_rec_count= 1.0;
|
||||
first_weedout_table_rec_count= 1.0;
|
||||
temptable_rec_size= 0;
|
||||
dups_cost= 0.0;
|
||||
}
|
||||
else
|
||||
{
|
||||
dups_cost= join->positions[first_tab - 1].prefix_cost;
|
||||
prefix_rec_count= join->positions[first_tab - 1].prefix_record_count;
|
||||
first_weedout_table_rec_count=
|
||||
join->positions[first_tab - 1].prefix_record_count;
|
||||
temptable_rec_size= 8; /* This is not true but we'll make it so */
|
||||
}
|
||||
|
||||
@@ -3677,17 +3682,14 @@ bool Duplicate_weedout_picker::check_qep(JOIN *join,
|
||||
sj_outer_fanout,
|
||||
temptable_rec_size,
|
||||
0, 0);
|
||||
double write_cost=
|
||||
COST_ADD(one_cost.create,
|
||||
COST_MULT(join->positions[first_tab].prefix_record_count,
|
||||
sj_outer_fanout * one_cost.write));
|
||||
double full_lookup_cost=
|
||||
COST_MULT(join->positions[first_tab].prefix_record_count,
|
||||
COST_MULT(sj_outer_fanout,
|
||||
sj_inner_fanout * one_cost.lookup));
|
||||
*read_time= COST_ADD(dups_cost, COST_ADD(write_cost, full_lookup_cost));
|
||||
double prefix_record_count= join->positions[first_tab].prefix_record_count;
|
||||
double write_cost= (one_cost.create +
|
||||
prefix_record_count * sj_outer_fanout * one_cost.write);
|
||||
double full_lookup_cost= (prefix_record_count * sj_outer_fanout *
|
||||
sj_inner_fanout * one_cost.lookup);
|
||||
*read_time= dups_cost + write_cost + full_lookup_cost;
|
||||
|
||||
*record_count= prefix_rec_count * sj_outer_fanout;
|
||||
*record_count= first_weedout_table_rec_count * sj_outer_fanout;
|
||||
*handled_fanout= dups_removed_fanout;
|
||||
*strategy= SJ_OPT_DUPS_WEEDOUT;
|
||||
if (unlikely(join->thd->trace_started()))
|
||||
@@ -3695,7 +3697,10 @@ bool Duplicate_weedout_picker::check_qep(JOIN *join,
|
||||
Json_writer_object trace(join->thd);
|
||||
trace.
|
||||
add("strategy", "DuplicateWeedout").
|
||||
add("records", *record_count).
|
||||
add("prefix_row_count", prefix_record_count).
|
||||
add("tmp_table_rows", sj_outer_fanout).
|
||||
add("sj_inner_fanout", sj_inner_fanout).
|
||||
add("rows", *record_count).
|
||||
add("dups_cost", dups_cost).
|
||||
add("write_cost", write_cost).
|
||||
add("full_lookup_cost", full_lookup_cost).
|
||||
@@ -3899,7 +3904,7 @@ static void recalculate_prefix_record_count(JOIN *join, uint start, uint end)
|
||||
prefix_count= 1.0;
|
||||
else
|
||||
prefix_count= COST_MULT(join->best_positions[j-1].prefix_record_count,
|
||||
join->best_positions[j-1].records_read);
|
||||
join->best_positions[j-1].records_out);
|
||||
|
||||
join->best_positions[j].prefix_record_count= prefix_count;
|
||||
}
|
||||
@@ -4051,7 +4056,7 @@ void fix_semijoin_strategies_for_picked_join_order(JOIN *join)
|
||||
join->best_positions, i,
|
||||
FALSE, prefix_rec_count,
|
||||
join->best_positions + i, &dummy);
|
||||
prefix_rec_count *= join->best_positions[i].records_read;
|
||||
prefix_rec_count *= join->best_positions[i].records_out;
|
||||
rem_tables &= ~join->best_positions[i].table->table->map;
|
||||
}
|
||||
}
|
||||
@@ -4093,7 +4098,7 @@ void fix_semijoin_strategies_for_picked_join_order(JOIN *join)
|
||||
TRUE /* no jbuf */,
|
||||
record_count, join->best_positions + idx, &dummy);
|
||||
}
|
||||
record_count *= join->best_positions[idx].records_read;
|
||||
record_count *= join->best_positions[idx].records_out;
|
||||
rem_tables &= ~join->best_positions[idx].table->table->map;
|
||||
}
|
||||
}
|
||||
@@ -4151,7 +4156,7 @@ void fix_semijoin_strategies_for_picked_join_order(JOIN *join)
|
||||
}
|
||||
}
|
||||
rem_tables &= ~join->best_positions[idx].table->table->map;
|
||||
record_count *= join->best_positions[idx].records_read;
|
||||
record_count *= join->best_positions[idx].records_out;
|
||||
}
|
||||
first_pos->sj_strategy= SJ_OPT_LOOSE_SCAN;
|
||||
first_pos->n_sj_tables= my_count_bits(first_pos->table->emb_sj_nest->sj_inner_tables);
|
||||
@@ -5368,7 +5373,8 @@ int setup_semijoin_dups_elimination(JOIN *join, ulonglong options,
|
||||
Got a table that's not within any semi-join nest. This is a case
|
||||
like this:
|
||||
|
||||
SELECT * FROM ot1, nt1 WHERE ot1.col IN (SELECT expr FROM it1, it2)
|
||||
SELECT * FROM ot1, nt1 WHERE
|
||||
ot1.col IN (SELECT expr FROM it1, it2)
|
||||
|
||||
with a join order of
|
||||
|
||||
@@ -6780,7 +6786,7 @@ bool JOIN::choose_subquery_plan(table_map join_tables)
|
||||
Json_writer_object trace_wrapper(thd);
|
||||
Json_writer_object trace_subquery(thd, "subquery_plan");
|
||||
trace_subquery.
|
||||
add("records", inner_record_count_1).
|
||||
add("rows", inner_record_count_1).
|
||||
add("materialization_cost", materialize_strategy_cost).
|
||||
add("in_exist_cost", in_exists_strategy_cost).
|
||||
add("choosen", strategy);
|
||||
|
Reference in New Issue
Block a user