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)
2022-08-11 13:05:23 +03:00
|
|
|
#ifndef OPTIMIZER_DEFAULTS_INCLUDED
|
|
|
|
#define OPTIMIZER_DEFAULTS_INCLUDED
|
|
|
|
/*
|
|
|
|
Copyright (c) 2022, MariaDB AB
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; version 2 of
|
|
|
|
the License.
|
|
|
|
|
|
|
|
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 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-1335 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
This file contains costs constants used by the optimizer
|
|
|
|
All costs should be based on milliseconds (1 cost = 1 ms)
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Cost for finding the first key in a key scan */
|
|
|
|
#define DEFAULT_KEY_LOOKUP_COST ((double) 0.000435777)
|
|
|
|
|
|
|
|
/* Cost of finding a row based on row_ID */
|
|
|
|
#define DEFAULT_ROW_LOOKUP_COST ((double) 0.000130839)
|
|
|
|
|
|
|
|
/*
|
|
|
|
Cost of finding and copying key and row blocks from the storage
|
|
|
|
engine index cache to an internal cache as part of an index
|
|
|
|
scan. This includes all mutexes that needs to be taken to get
|
|
|
|
exclusive access to a page. The number is taken from accessing an
|
|
|
|
existing blocks from Aria page cache.
|
|
|
|
Used in handler::scan_time() and handler::keyread_time()
|
|
|
|
*/
|
|
|
|
#define DEFAULT_INDEX_BLOCK_COPY_COST ((double) 3.56e-05)
|
|
|
|
|
|
|
|
/*
|
|
|
|
Cost of copying a row to 'table->record'.
|
|
|
|
Used by scan_time() and rnd_pos_time() methods.
|
|
|
|
|
|
|
|
If this is too small, then table scans will be prefered over 'ref'
|
|
|
|
as with table scans there are no key read (KEY_LOOKUP_COST), fewer
|
|
|
|
disk reads but more record copying and row comparisions. If it's
|
|
|
|
too big then MariaDB will used key lookup even when table scan is
|
|
|
|
better.
|
|
|
|
*/
|
|
|
|
#define DEFAULT_ROW_COPY_COST ((double) 0.000060866)
|
|
|
|
|
|
|
|
/*
|
|
|
|
Cost of copying the key to 'table->record'
|
|
|
|
|
|
|
|
If this is too small, then, for small tables, index scans will be
|
|
|
|
prefered over 'ref' as with index scans there are fewer disk reads.
|
|
|
|
*/
|
|
|
|
#define DEFAULT_KEY_COPY_COST ((double) 0.000015685)
|
|
|
|
|
|
|
|
/*
|
|
|
|
Cost of finding the next index entry and checking its rowid against filter
|
|
|
|
This cost is very low as it's done inside the storage engine.
|
|
|
|
Should be smaller than KEY_COPY_COST.
|
|
|
|
*/
|
|
|
|
#define DEFAULT_KEY_NEXT_FIND_COST ((double) 0.000082347)
|
|
|
|
|
|
|
|
/* Cost of finding the next row when scanning a table */
|
|
|
|
#define DEFAULT_ROW_NEXT_FIND_COST ((double) 0.000045916)
|
|
|
|
|
|
|
|
/**
|
|
|
|
The cost of executing the WHERE clause as part of any row check.
|
|
|
|
Increasing this would force the optimizer to use row combinations
|
|
|
|
that reads fewer rows.
|
|
|
|
The default cost comes from recording times from a simple where clause that
|
|
|
|
compares two fields (date and a double) with constants.
|
|
|
|
*/
|
|
|
|
#define DEFAULT_WHERE_COST ((double) 3.2e-05)
|
|
|
|
|
|
|
|
/* The cost of comparing a key when using range access or sorting */
|
|
|
|
#define DEFAULT_KEY_COMPARE_COST 0.000011361
|
|
|
|
|
|
|
|
/* Rowid compare is usually just a single memcmp of a short string */
|
|
|
|
#define DEFAULT_ROWID_COMPARE_COST 0.000002653
|
|
|
|
/* Rowid copy is usually just a single memcpy of a short string */
|
|
|
|
#define DEFAULT_ROWID_COPY_COST 0.000002653
|
|
|
|
|
|
|
|
/*
|
|
|
|
Average disk seek time on a hard disk is 8-10 ms, which is also
|
|
|
|
about the time to read a IO_SIZE (8192) block.
|
|
|
|
|
|
|
|
A medium ssd is about 400MB/second, which gives us the time for
|
|
|
|
reading an IO_SIZE block to IO_SIZE/400000000 = 0.0000204 sec= 0.02 ms.
|
|
|
|
*/
|
|
|
|
#define DEFAULT_DISK_READ_COST ((double) IO_SIZE / 400000000.0 * 1000)
|
|
|
|
|
|
|
|
/*
|
|
|
|
The follwoing is an old comment for hard-disks, please ignore the
|
|
|
|
following, except if you like history:
|
|
|
|
|
|
|
|
For sequential hard 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 = 10.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
The table/index cache_miss/total_cache_request ratio.
|
|
|
|
1.0 means that a searched for key or row will never be in the cache while
|
|
|
|
0.0 means it always in the cache (and we don't have to do any disk reads).
|
|
|
|
|
|
|
|
According to folklore, one should not have to access disk for more
|
|
|
|
than 20% of the cache request for MariaDB to run very well.
|
|
|
|
However in practice when we read rows or keys in a query, we will often
|
|
|
|
read the same row over and over again. Because of this we set
|
|
|
|
DEFAULT_DISK_READ_RATIO to 0.20/10 = 0.02.
|
|
|
|
|
|
|
|
Increasing DISK_READ_RATIO will make MariaDB prefer key lookup over
|
|
|
|
table scans as the impact of ROW_COPY_COST and INDEX_COPY cost will
|
|
|
|
have a larger impact when more rows are examined..
|
|
|
|
|
|
|
|
We are not yet taking into account cache usage statistics as this
|
|
|
|
could confuse users as the EXPLAIN and costs for a query would change
|
|
|
|
between to query calls, which may confuse users (and also make the
|
|
|
|
mtr tests very unpredictable).
|
|
|
|
|
|
|
|
Note that the engine's avg_io_cost() (DEFAULT_DISK_READ_COST by default)
|
|
|
|
is multiplied with this constant!
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define DEFAULT_DISK_READ_RATIO 0.02
|
|
|
|
|
|
|
|
/*
|
|
|
|
The following costs are mainly to ensure we don't do table and index
|
|
|
|
scans for small tables, like the one we have in the mtr test suite.
|
|
|
|
|
|
|
|
This is mostly to keep the mtr tests use indexes (as the optimizer would
|
|
|
|
if the tables are large). It will also ensure that EXPLAIN is showing
|
|
|
|
more key user for users where they are testing queries with small tables
|
|
|
|
at the start of projects.
|
|
|
|
This is probably OK for most a the execution time difference between table
|
|
|
|
scan and index scan compared to key lookups so small when using small
|
|
|
|
tables. It also helps to fill the index cache which will help mitigate
|
|
|
|
the speed difference.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
Extra cost for full table and index scan. Used to prefer key and range
|
|
|
|
over index and table scans
|
|
|
|
|
|
|
|
INDEX_SCAN_SETUP_COST (defined in optimizer_costs.h) is half of
|
|
|
|
table_scan_setup_cost to get the optimizer to prefer index scans to table
|
|
|
|
scans as key copy is faster than row copy and index blocks provides
|
|
|
|
more information in the cache.
|
|
|
|
|
|
|
|
This will also help MyISAM as with MyISAM the table scans has a cost
|
|
|
|
very close to index scans (they are fast but require a read call
|
|
|
|
that we want to avoid even if it's small).
|
|
|
|
|
|
|
|
10 usec is about 10 MyISAM row lookups with optimizer_disk_read_ratio= 0.02
|
|
|
|
*/
|
|
|
|
#define DEFAULT_TABLE_SCAN_SETUP_COST 0.01 // 10 usec
|
|
|
|
|
|
|
|
/* Extra cost for doing a range scan. Used to prefer 'ref' over range */
|
|
|
|
#define MULTI_RANGE_READ_SETUP_COST KEY_LOOKUP_COST
|
|
|
|
|
|
|
|
/*
|
|
|
|
Temporary file and temporary table related costs
|
|
|
|
Used with subquery materialization, derived tables etc
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define TMPFILE_CREATE_COST 0.5 // Cost of creating and deleting files
|
|
|
|
#define HEAP_TEMPTABLE_CREATE_COST 0.025 // ms
|
|
|
|
/* Cost taken from HEAP_LOOKUP_COST in ha_heap.cc */
|
2022-09-30 17:10:37 +03:00
|
|
|
#define HEAP_TEMPTABLE_LOOKUP_COST (0.00016097)
|
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)
2022-08-11 13:05:23 +03:00
|
|
|
#define DISK_TEMPTABLE_LOOKUP_COST(thd) (tmp_table_optimizer_costs.key_lookup_cost + tmp_table_optimizer_costs.row_lookup_cost + tmp_table_optimizer_costs.row_copy_cost)
|
|
|
|
#define DISK_TEMPTABLE_CREATE_COST TMPFILE_CREATE_COST*2 // 2 tmp tables
|
|
|
|
#define DISK_TEMPTABLE_BLOCK_SIZE IO_SIZE
|
|
|
|
|
|
|
|
#endif /* OPTIMIZER_DEFAULTS_INCLUDED */
|