2017-12-30 21:29:09 +01:00
|
|
|
/*
|
2020-11-02 14:48:47 +01:00
|
|
|
Copyright (c) 2017, 2020, MariaDB
|
2017-12-30 21:29:09 +01:00
|
|
|
|
|
|
|
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-1301 USA */
|
|
|
|
|
|
|
|
/*
|
|
|
|
This file contains functions to support the splitting technique.
|
|
|
|
This optimization technique can be applied to equi-joins involving
|
|
|
|
materialized tables such as materialized views, materialized derived tables
|
|
|
|
and materialized CTEs. The technique also could be applied to materialized
|
|
|
|
semi-joins though the code below does not support this usage yet.
|
|
|
|
|
|
|
|
Here are the main ideas behind this technique that we'll call SM optimization
|
|
|
|
(SplitMaterialization).
|
|
|
|
|
|
|
|
Consider the query
|
|
|
|
SELECT t1.a, t.min
|
|
|
|
FROM t1, (SELECT t2.a, MIN(t2.b) as min FROM t2 GROUP BY t2.a) t
|
|
|
|
WHERE t1.a = t.a and t1.b < const
|
|
|
|
|
|
|
|
Re-write the query into
|
|
|
|
SELECT t1.a, t.min
|
|
|
|
FROM t1, LATERAL (SELECT t2.a, MIN(t2.b) as min
|
|
|
|
FROM t2 WHERE t2.a = t1.a GROUP BY t2.a) t
|
|
|
|
WHERE t1.b < const
|
|
|
|
|
|
|
|
The execution of the original query (Q1) does the following:
|
|
|
|
1. Executes the query in the specification of the derived table
|
|
|
|
and puts the result set into a temporary table with an index
|
|
|
|
on the first column.
|
|
|
|
2. Joins t1 with the temporary table using the its index.
|
|
|
|
|
|
|
|
The execution of the transformed query (Q1R) follows these steps:
|
|
|
|
1. For each row of t1 where t1.b < const a temporary table
|
|
|
|
containing all rows of of t2 with t2.a = t1.a is created
|
|
|
|
2. If there are any rows in the temporary table aggregation
|
|
|
|
is performed for them
|
|
|
|
3. The result of the aggregation is joined with t1.
|
|
|
|
|
|
|
|
The second execution can win if:
|
|
|
|
a) There is an efficient way to select rows of t2 for which t2.a = t1.a
|
|
|
|
(For example if there is an index on t2.a)
|
|
|
|
and
|
|
|
|
b) The number of temporary tables created for partitions
|
|
|
|
is much smaller that the total number of partitions
|
|
|
|
|
|
|
|
It should be noted that for the transformed query aggregation
|
|
|
|
for a partition may be performed several times.
|
|
|
|
|
|
|
|
As we can see the optimization basically splits table t2 into
|
|
|
|
partitions and performs aggregation over each of them
|
|
|
|
independently.
|
|
|
|
|
|
|
|
If we have only one equi-join condition then we either push it as
|
|
|
|
for Q1R or we don't. In a general case we may have much more options.
|
|
|
|
Consider the query (Q3)
|
|
|
|
SELECT
|
|
|
|
FROM t1,t2 (SELECT t3.a, t3.b, MIN(t3.c) as min
|
|
|
|
FROM t3 GROUP BY a,b) t
|
|
|
|
WHERE t.a = t1.a AND t.b = t2.b
|
|
|
|
AND t1.c < c1 and t2.c < c2
|
|
|
|
AND P(t1,t2);
|
|
|
|
(P(t1,t2) designates some additional conditions over columns of t1,t2).
|
|
|
|
|
|
|
|
Assuming that there indexes on t3(a,b) and t3(b) here we have several
|
|
|
|
reasonable options to push equi-join conditions into the derived.
|
|
|
|
All these options should be taken into account when the optimizer
|
|
|
|
evaluates different join orders. When the join order (t1,t,t2) is
|
|
|
|
evaluated there is only one way of splitting : to push the condition
|
|
|
|
t.a = t1.a into t. With the join order (t2,t,t1) only the condition
|
|
|
|
t.b = t2.b can be pushed. When the join orders (t1,t2,t) and (t2,t1,t)
|
|
|
|
are evaluated then the optimizer should consider pushing t.a = t1.a,
|
|
|
|
t.b = t2.b and (t.a = t1.a AND t.b = t2.b) to choose the best condition
|
|
|
|
for splitting. Apparently here last condition is the best one because
|
|
|
|
it provides the miximum possible number of partitions.
|
|
|
|
|
|
|
|
If we dropped the index on t3(a,b) and created the index on t3(a) instead
|
|
|
|
then we would have two options for splitting: to push t.a = t1.a or to
|
|
|
|
push t.b = t2.b. If the selectivity of the index t3(a) is better than
|
|
|
|
the selectivity of t3(b) then the first option is preferred.
|
|
|
|
|
|
|
|
Although the condition (t.a = t1.a AND t.b = t2.b) provides a better
|
|
|
|
splitting than the condition t.a = t1.a the latter will be used for
|
|
|
|
splitting if the execution plan with the join order (t1,t,t2) turns out
|
|
|
|
to be the cheapest one. It's quite possible when the join condition
|
|
|
|
P(t1,t2) has a bad selectivity.
|
|
|
|
|
|
|
|
Whenever the optimizer evaluates the cost of using a splitting it
|
|
|
|
compares it with the cost of materialization without splitting.
|
|
|
|
|
|
|
|
If we just drop the index on t3(a,b) the chances that the splitting
|
|
|
|
will be used becomes much lower but they still exists providing that
|
|
|
|
the fanout of the partial join of t1 and t2 is small enough.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
Splitting can be applied to a materialized table specified by the query
|
|
|
|
with post-join operations that require partitioning of the result set produced
|
|
|
|
by the join expression used in the FROM clause the query such as GROUP BY
|
|
|
|
operation and window function operation. In any of these cases the post-join
|
|
|
|
operation can be executed independently for any partition only over the rows
|
|
|
|
of this partition. Also if the set of all partitions is divided into disjoint
|
|
|
|
subsets the operation can applied to each subset independently. In this case
|
|
|
|
all rows are first partitioned into the groups each of which contains all the
|
|
|
|
rows from the partitions belonging the same subset and then each group
|
|
|
|
is subpartitioned into groups in the the post join operation.
|
|
|
|
|
|
|
|
The set of all rows belonging to the union of several partitions is called
|
|
|
|
here superpartition. If a grouping operation is defined by the list
|
|
|
|
e_1,...,e_n then any set S = {e_i1,...,e_ik} can be used to devide all rows
|
|
|
|
into superpartions such that for any two rows r1, r2 the following holds:
|
|
|
|
e_ij(r1) = e_ij(r2) for each e_ij from S. We use the splitting technique
|
|
|
|
only if S consists of references to colums of the joined tables.
|
|
|
|
For example if the GROUP BY list looks like this a, g(b), c we can consider
|
|
|
|
applying the splitting technique to the superpartitions defined by {a,c},
|
|
|
|
{a}, {c} (a and c here may be the references to the columns from different
|
|
|
|
tables).
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
The following describes when and how the optimizer decides whether it
|
|
|
|
makes sense to employ the splitting technique.
|
|
|
|
|
|
|
|
1. For each instance of a materialized table (derived/view/CTE) it is
|
|
|
|
checked that it is potentially splittable. Now it is done right after the
|
|
|
|
execution plan for the select specifying this table has been chosen.
|
|
|
|
|
|
|
|
2. Any potentially splittable materialized table T is subject to two-phase
|
|
|
|
optimization. It means that the optimizer first builds the best execution
|
|
|
|
plan for join that specifies T. Then the control is passed back to the
|
|
|
|
optimization process of the embedding select Q. After the execution plan
|
|
|
|
for Q has been chosen the optimizer finishes the optimization of the join
|
|
|
|
specifying T.
|
|
|
|
|
|
|
|
3. When the optimizer builds the container with the KEYUSE structures
|
|
|
|
for the join of embedding select it detects the equi-join conditions
|
|
|
|
PC that potentially could be pushed into a potentially splittable
|
|
|
|
materialized table T. The collected information about such conditions
|
|
|
|
is stored together with other facts on potential splittings for table T.
|
|
|
|
|
|
|
|
4. When the optimizer starts looking for the best execution plan for the
|
|
|
|
embedding select Q for each potentially splittable materialized table T
|
|
|
|
it creates special KEYUSE structures for pushable equi-join conditions
|
|
|
|
PC. These structures are used to add new elements to the container
|
|
|
|
of KEYUSE structures built for T. The specifics of these elements is
|
|
|
|
that they can be ebabled and disabled during the process of choosing
|
|
|
|
the best plan for Q.
|
|
|
|
|
|
|
|
5. When the optimizer extends a partial join order with a potentially
|
|
|
|
splittable materialized table T (in function best_access_path) it
|
|
|
|
first evaluates a new execution plan for the modified specification
|
|
|
|
of T that adds all equi-join conditions that can be pushed with
|
|
|
|
current join prefix to the WHERE conditions of the original
|
|
|
|
specification of T. If the cost of the new plan is better than the
|
|
|
|
the cost of the original materialized table then the optimizer
|
|
|
|
prefers to use splitting for the current join prefix. As the cost
|
|
|
|
of the plan depends only on the pushed conditions it makes sense
|
|
|
|
to cache this plan for other prefixes.
|
|
|
|
|
|
|
|
6. The optimizer takes into account the cost of splitting / materialization
|
|
|
|
of a potentially splittable materialized table T as a startup cost
|
|
|
|
to access table T.
|
|
|
|
|
|
|
|
7. When the optimizer finally chooses the best execution plan for
|
|
|
|
the embedding select Q and this plan prefers using splitting
|
|
|
|
for table T with pushed equi-join conditions PC then the execution
|
|
|
|
plan for the underlying join with these conditions is chosen for T.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
The implementation of the splitting technique below allows to apply
|
|
|
|
the technique only to a materialized derived table / view / CTE whose
|
|
|
|
specification is either a select with GROUP BY or a non-grouping select
|
|
|
|
with window functions that share the same PARTITION BY list.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "mariadb.h"
|
|
|
|
#include "sql_select.h"
|
2021-03-28 23:33:27 +02:00
|
|
|
#include "opt_trace.h"
|
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 12:05:23 +02:00
|
|
|
#include "optimizer_defaults.h"
|
2017-12-30 21:29:09 +01:00
|
|
|
|
|
|
|
/* Info on a splitting field */
|
|
|
|
struct SplM_field_info
|
|
|
|
{
|
|
|
|
/* Splitting field in the materialized table T */
|
|
|
|
Field *mat_field;
|
|
|
|
/* The item from the select list of the specification of T */
|
|
|
|
Item *producing_item;
|
|
|
|
/* The corresponding splitting field from the specification of T */
|
|
|
|
Field *underlying_field;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Info on the splitting execution plan saved in SplM_opt_info::cache */
|
|
|
|
struct SplM_plan_info
|
|
|
|
{
|
|
|
|
/* The cached splitting execution plan P */
|
Fix all warnings given by UBSAN
The easiest way to compile and test the server with UBSAN is to run:
./BUILD/compile-pentium64-ubsan
and then run mysql-test-run.
After this commit, one should be able to run this without any UBSAN
warnings. There is still a few compiler warnings that should be fixed
at some point, but these do not expose any real bugs.
The 'special' cases where we disable, suppress or circumvent UBSAN are:
- ref10 source (as here we intentionally do some shifts that UBSAN
complains about.
- x86 version of optimized int#korr() methods. UBSAN do not like unaligned
memory access of integers. Fixed by using byte_order_generic.h when
compiling with UBSAN
- We use smaller thread stack with ASAN and UBSAN, which forced me to
disable a few tests that prints the thread stack size.
- Verifying class types does not work for shared libraries. I added
suppression in mysql-test-run.pl for this case.
- Added '#ifdef WITH_UBSAN' when using integer arithmetic where it is
safe to have overflows (two cases, in item_func.cc).
Things fixed:
- Don't left shift signed values
(byte_order_generic.h, mysqltest.c, item_sum.cc and many more)
- Don't assign not non existing values to enum variables.
- Ensure that bool and enum values are properly initialized in
constructors. This was needed as UBSAN checks that these types has
correct values when one copies an object.
(gcalc_tools.h, ha_partition.cc, item_sum.cc, partition_element.h ...)
- Ensure we do not called handler functions on unallocated objects or
deleted objects.
(events.cc, sql_acl.cc).
- Fixed bugs in Item_sp::Item_sp() where we did not call constructor
on Query_arena object.
- Fixed several cast of objects to an incompatible class!
(Item.cc, Item_buff.cc, item_timefunc.cc, opt_subselect.cc, sql_acl.cc,
sql_select.cc ...)
- Ensure we do not do integer arithmetic that causes over or underflows.
This includes also ++ and -- of integers.
(Item_func.cc, Item_strfunc.cc, item_timefunc.cc, sql_base.cc ...)
- Added JSON_VALUE_UNITIALIZED to json_value_types and ensure that
value_type is initialized to this instead of to -1, which is not a valid
enum value for json_value_types.
- Ensure we do not call memcpy() when second argument could be null.
- Fixed that Item_func_str::make_empty_result() creates an empty string
instead of a null string (safer as it ensures we do not do arithmetic
on null strings).
Other things:
- Changed struct st_position to an OBJECT and added an initialization
function to it to ensure that we do not copy or use uninitialized
members. The change to a class was also motived that we used "struct
st_position" and POSITION randomly trough the code which was
confusing.
- Notably big rewrite in sql_acl.cc to avoid using deleted objects.
- Changed in sql_partition to use '^' instead of '-'. This is safe as
the operator is either 0 or 0x8000000000000000ULL.
- Added check for select_nr < INT_MAX in JOIN::build_explain() to
avoid bug when get_select() could return NULL.
- Reordered elements in POSITION for better alignment.
- Changed sql_test.cc::print_plan() to use pointers instead of objects.
- Fixed bug in find_set() where could could execute '1 << -1'.
- Added variable have_sanitizer, used by mtr. (This variable was before
only in 10.5 and up). It can now have one of two values:
ASAN or UBSAN.
- Moved ~Archive_share() from ha_archive.cc to ha_archive.h and marked
it virtual. This was an effort to get UBSAN to work with loaded storage
engines. I kept the change as the new place is better.
- Added in CONNECT engine COLBLK::SetName(), to get around a wrong cast
in tabutil.cpp.
- Added HAVE_REPLICATION around usage of rgi_slave, to get embedded
server to compile with UBSAN. (Patch from Marko).
- Added #ifdef for powerpc64 to avoid a bug in old gcc versions related
to integer arithmetic.
Changes that should not be needed but had to be done to suppress warnings
from UBSAN:
- Added static_cast<<uint16_t>> around shift to get rid of a LOT of
compiler warnings when using UBSAN.
- Had to change some '/' of 2 base integers to shift to get rid of
some compile time warnings.
Reviewed by:
- Json changes: Alexey Botchkov
- Charset changes in ctype-uca.c: Alexander Barkov
- InnoDB changes & Embedded server: Marko Mäkelä
- sql_acl.cc changes: Vicențiu Ciorbaru
- build_explain() changes: Sergey Petrunia
2021-04-18 14:29:13 +02:00
|
|
|
POSITION *best_positions;
|
2017-12-30 21:29:09 +01:00
|
|
|
/* The cost of the above plan */
|
|
|
|
double cost;
|
|
|
|
/* Selectivity of splitting used in P */
|
|
|
|
double split_sel;
|
|
|
|
/* For fast search of KEYUSE_EXT elements used for splitting in P */
|
|
|
|
struct KEYUSE_EXT *keyuse_ext_start;
|
|
|
|
/* The tables that contains the fields used for splitting in P */
|
|
|
|
TABLE *table;
|
|
|
|
/* The number of the key from 'table' used for splitting in P */
|
|
|
|
uint key;
|
|
|
|
/* Number of the components of 'key' used for splitting in P */
|
|
|
|
uint parts;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
The structure contains the information that is used by the optimizer
|
|
|
|
for potentially splittable materialization of T that is a materialized
|
|
|
|
derived_table / view / CTE
|
|
|
|
*/
|
|
|
|
class SplM_opt_info : public Sql_alloc
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/* The join for the select specifying T */
|
|
|
|
JOIN *join;
|
|
|
|
/* The map of tables from 'join' whose columns can be used for partitioning */
|
|
|
|
table_map tables_usable_for_splitting;
|
|
|
|
/* Info about the fields of the joined tables usable for splitting */
|
|
|
|
SplM_field_info *spl_fields;
|
|
|
|
/* The number of elements in the above list */
|
|
|
|
uint spl_field_cnt;
|
2021-03-24 04:54:54 +01:00
|
|
|
/* The list of equalities injected into WHERE for split optimization */
|
|
|
|
List<Item> inj_cond_list;
|
2017-12-30 21:29:09 +01:00
|
|
|
/* Contains the structures to generate all KEYUSEs for pushable equalities */
|
|
|
|
List<KEY_FIELD> added_key_fields;
|
|
|
|
/* The cache of evaluated execution plans for 'join' with pushed equalities */
|
|
|
|
List<SplM_plan_info> plan_cache;
|
|
|
|
/* Cost of best execution plan for join when nothing is pushed */
|
|
|
|
double unsplit_cost;
|
|
|
|
/* Cardinality of T when nothing is pushed */
|
|
|
|
double unsplit_card;
|
|
|
|
/* Lastly evaluated execution plan for 'join' with pushed equalities */
|
|
|
|
SplM_plan_info *last_plan;
|
|
|
|
|
|
|
|
SplM_plan_info *find_plan(TABLE *table, uint key, uint parts);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void TABLE::set_spl_opt_info(SplM_opt_info *spl_info)
|
|
|
|
{
|
|
|
|
if (spl_info)
|
|
|
|
spl_info->join->spl_opt_info= spl_info;
|
|
|
|
spl_opt_info= spl_info;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TABLE::deny_splitting()
|
|
|
|
{
|
|
|
|
DBUG_ASSERT(spl_opt_info != NULL);
|
|
|
|
spl_opt_info->join->spl_opt_info= NULL;
|
|
|
|
spl_opt_info= NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-15 23:28:19 +02:00
|
|
|
double TABLE::get_materialization_cost()
|
|
|
|
{
|
|
|
|
DBUG_ASSERT(spl_opt_info != NULL);
|
|
|
|
return spl_opt_info->unsplit_cost;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-30 21:29:09 +01:00
|
|
|
/* This structure is auxiliary and used only in the function that follows it */
|
|
|
|
struct SplM_field_ext_info: public SplM_field_info
|
|
|
|
{
|
|
|
|
uint item_no;
|
|
|
|
bool is_usable_for_ref_access;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief
|
|
|
|
Check whether this join is one for potentially splittable materialized table
|
|
|
|
|
|
|
|
@details
|
|
|
|
The function checks whether this join is for select that specifies
|
|
|
|
a potentially splittable materialized table T. If so, the collected
|
|
|
|
info on potential splittability of T is attached to the field spl_opt_info
|
|
|
|
of the TABLE structure for T.
|
|
|
|
|
|
|
|
The function returns a positive answer if the following holds:
|
|
|
|
1. the optimizer switch 'split_materialized' is set 'on'
|
|
|
|
2. the select owning this join specifies a materialized derived/view/cte T
|
|
|
|
3. this is the only select in the specification of T
|
|
|
|
4. condition pushdown is not prohibited into T
|
|
|
|
5. T is not recursive
|
2018-01-02 23:09:16 +01:00
|
|
|
6. not all of this join are constant or optimized away
|
|
|
|
7. T is either
|
|
|
|
7.1. a grouping table with GROUP BY list P
|
2017-12-30 21:29:09 +01:00
|
|
|
or
|
2018-01-02 23:09:16 +01:00
|
|
|
7.2. a non-grouping table with window functions over the same non-empty
|
2017-12-30 21:29:09 +01:00
|
|
|
partition specified by the PARTITION BY list P
|
2018-01-02 23:09:16 +01:00
|
|
|
8. P contains some references on the columns of the joined tables C
|
2017-12-30 21:29:09 +01:00
|
|
|
occurred also in the select list of this join
|
2018-01-02 23:09:16 +01:00
|
|
|
9. There are defined some keys usable for ref access of fields from C
|
2020-02-08 04:42:11 +01:00
|
|
|
with available statistics.
|
2022-01-03 22:01:52 +01:00
|
|
|
10. The select doesn't use WITH ROLLUP (This limitation can probably be
|
|
|
|
lifted)
|
2017-12-30 21:29:09 +01:00
|
|
|
|
|
|
|
@retval
|
|
|
|
true if the answer is positive
|
|
|
|
false otherwise
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool JOIN::check_for_splittable_materialized()
|
|
|
|
{
|
|
|
|
ORDER *partition_list= 0;
|
|
|
|
st_select_lex_unit *unit= select_lex->master_unit();
|
|
|
|
TABLE_LIST *derived= unit->derived;
|
|
|
|
if (!(optimizer_flag(thd, OPTIMIZER_SWITCH_SPLIT_MATERIALIZED)) || // !(1)
|
|
|
|
!(derived && derived->is_materialized_derived()) || // !(2)
|
|
|
|
(unit->first_select()->next_select()) || // !(3)
|
|
|
|
(derived->prohibit_cond_pushdown) || // !(4)
|
2018-01-02 23:09:16 +01:00
|
|
|
(derived->is_recursive_with_table()) || // !(5)
|
2022-01-03 22:01:52 +01:00
|
|
|
(table_count == 0 || const_tables == top_join_tab_count) || // !(6)
|
|
|
|
rollup.state != ROLLUP::STATE_NONE) // (10)
|
2017-12-30 21:29:09 +01:00
|
|
|
return false;
|
2018-01-02 23:09:16 +01:00
|
|
|
if (group_list) // (7.1)
|
2017-12-30 21:29:09 +01:00
|
|
|
{
|
|
|
|
if (!select_lex->have_window_funcs())
|
|
|
|
partition_list= group_list;
|
|
|
|
}
|
|
|
|
else if (select_lex->have_window_funcs() &&
|
2018-01-02 23:09:16 +01:00
|
|
|
select_lex->window_specs.elements == 1) // (7.2)
|
2017-12-30 21:29:09 +01:00
|
|
|
{
|
|
|
|
partition_list=
|
|
|
|
select_lex->window_specs.head()->partition_list->first;
|
|
|
|
}
|
|
|
|
if (!partition_list)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ORDER *ord;
|
2020-01-29 13:50:26 +01:00
|
|
|
Dynamic_array<SplM_field_ext_info> candidates(PSI_INSTRUMENT_MEM);
|
2017-12-30 21:29:09 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
Select from partition_list all candidates for splitting.
|
|
|
|
A candidate must be
|
2018-01-02 23:09:16 +01:00
|
|
|
- field item or refer to such (8.1)
|
|
|
|
- item mentioned in the select list (8.2)
|
2017-12-30 21:29:09 +01:00
|
|
|
Put info about such candidates into the array candidates
|
|
|
|
*/
|
|
|
|
table_map usable_tables= 0; // tables that contains the candidate
|
|
|
|
for (ord= partition_list; ord; ord= ord->next)
|
|
|
|
{
|
|
|
|
Item *ord_item= *ord->item;
|
2018-01-02 23:09:16 +01:00
|
|
|
if (ord_item->real_item()->type() != Item::FIELD_ITEM) // !(8.1)
|
2017-12-30 21:29:09 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
Field *ord_field= ((Item_field *) (ord_item->real_item()))->field;
|
|
|
|
|
2018-04-18 08:39:40 +02:00
|
|
|
/* Ignore fields from of inner tables of outer joins */
|
|
|
|
TABLE_LIST *tbl= ord_field->table->pos_in_table_list;
|
|
|
|
if (tbl->is_inner_table_of_outer_join())
|
2017-12-30 21:29:09 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
List_iterator<Item> li(fields_list);
|
|
|
|
Item *item;
|
|
|
|
uint item_no= 0;
|
|
|
|
while ((item= li++))
|
|
|
|
{
|
2018-01-02 23:09:16 +01:00
|
|
|
if ((*ord->item)->eq(item, 0)) // (8.2)
|
2017-12-30 21:29:09 +01:00
|
|
|
{
|
|
|
|
SplM_field_ext_info new_elem;
|
|
|
|
new_elem.producing_item= item;
|
|
|
|
new_elem.item_no= item_no;
|
|
|
|
new_elem.mat_field= derived->table->field[item_no];
|
|
|
|
new_elem.underlying_field= ord_field;
|
|
|
|
new_elem.is_usable_for_ref_access= false;
|
|
|
|
candidates.push(new_elem);
|
|
|
|
usable_tables|= ord_field->table->map;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
item_no++;
|
|
|
|
}
|
|
|
|
}
|
2018-01-02 23:09:16 +01:00
|
|
|
if (candidates.elements() == 0) // no candidates satisfying (8.1) && (8.2)
|
2017-12-30 21:29:09 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
For each table from this join find the keys that can be used for ref access
|
|
|
|
of the fields mentioned in the 'array candidates'
|
|
|
|
*/
|
|
|
|
|
|
|
|
SplM_field_ext_info *cand;
|
|
|
|
SplM_field_ext_info *cand_start= &candidates.at(0);
|
|
|
|
SplM_field_ext_info *cand_end= cand_start + candidates.elements();
|
|
|
|
|
|
|
|
for (JOIN_TAB *tab= join_tab;
|
|
|
|
tab < join_tab + top_join_tab_count; tab++)
|
|
|
|
{
|
|
|
|
TABLE *table= tab->table;
|
|
|
|
if (!(table->map & usable_tables))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
table->keys_usable_for_splitting.clear_all();
|
|
|
|
uint i;
|
|
|
|
for (i= 0; i < table->s->keys; i++)
|
|
|
|
{
|
|
|
|
if (!table->keys_in_use_for_query.is_set(i))
|
|
|
|
continue;
|
|
|
|
KEY *key_info= table->key_info + i;
|
|
|
|
uint key_parts= table->actual_n_key_parts(key_info);
|
|
|
|
uint usable_kp_cnt= 0;
|
|
|
|
for ( ; usable_kp_cnt < key_parts; usable_kp_cnt++)
|
|
|
|
{
|
|
|
|
if (key_info->actual_rec_per_key(usable_kp_cnt) == 0)
|
|
|
|
break;
|
|
|
|
int fldnr= key_info->key_part[usable_kp_cnt].fieldnr;
|
|
|
|
|
|
|
|
for (cand= cand_start; cand < cand_end; cand++)
|
|
|
|
{
|
2018-08-03 23:54:02 +02:00
|
|
|
if (cand->underlying_field->table == table &&
|
|
|
|
cand->underlying_field->field_index + 1 == fldnr)
|
2017-12-30 21:29:09 +01:00
|
|
|
{
|
|
|
|
cand->is_usable_for_ref_access= true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (cand == cand_end)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (usable_kp_cnt)
|
|
|
|
table->keys_usable_for_splitting.set_bit(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Count the candidate fields that can be accessed by ref */
|
2018-02-06 13:55:58 +01:00
|
|
|
uint spl_field_cnt= (uint)candidates.elements();
|
2017-12-30 21:29:09 +01:00
|
|
|
for (cand= cand_start; cand < cand_end; cand++)
|
|
|
|
{
|
|
|
|
if (!cand->is_usable_for_ref_access)
|
|
|
|
spl_field_cnt--;
|
|
|
|
}
|
|
|
|
|
2018-01-02 23:09:16 +01:00
|
|
|
if (!spl_field_cnt) // No candidate field can be accessed by ref => !(9)
|
2017-12-30 21:29:09 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Create a structure of the type SplM_opt_info and fill it with
|
|
|
|
the collected info on potential splittability of T
|
|
|
|
*/
|
|
|
|
SplM_opt_info *spl_opt_info= new (thd->mem_root) SplM_opt_info();
|
|
|
|
SplM_field_info *spl_field=
|
|
|
|
(SplM_field_info *) (thd->calloc(sizeof(SplM_field_info) *
|
|
|
|
spl_field_cnt));
|
|
|
|
|
|
|
|
if (!(spl_opt_info && spl_field)) // consider T as not good for splitting
|
|
|
|
return false;
|
|
|
|
|
|
|
|
spl_opt_info->join= this;
|
|
|
|
spl_opt_info->tables_usable_for_splitting= 0;
|
|
|
|
spl_opt_info->spl_field_cnt= spl_field_cnt;
|
|
|
|
spl_opt_info->spl_fields= spl_field;
|
|
|
|
for (cand= cand_start; cand < cand_end; cand++)
|
|
|
|
{
|
|
|
|
if (!cand->is_usable_for_ref_access)
|
|
|
|
continue;
|
|
|
|
spl_field->producing_item= cand->producing_item;
|
|
|
|
spl_field->underlying_field= cand->underlying_field;
|
|
|
|
spl_field->mat_field= cand->mat_field;
|
|
|
|
spl_opt_info->tables_usable_for_splitting|=
|
|
|
|
cand->underlying_field->table->map;
|
|
|
|
spl_field++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Attach this info to the table T */
|
|
|
|
derived->table->set_spl_opt_info(spl_opt_info);
|
|
|
|
|
2020-02-08 04:42:11 +01:00
|
|
|
/*
|
|
|
|
If this is specification of a materialized derived table T that is
|
|
|
|
potentially splittable and is used in the from list of the right operand
|
|
|
|
of an IN predicand transformed to a semi-join then the embedding semi-join
|
|
|
|
nest is not allowed to be materialized.
|
|
|
|
*/
|
|
|
|
if (derived && derived->is_materialized_derived() &&
|
|
|
|
derived->embedding && derived->embedding->sj_subq_pred)
|
|
|
|
derived->embedding->sj_subq_pred->types_allow_materialization= FALSE;
|
2017-12-30 21:29:09 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief
|
|
|
|
Collect info on KEY_FIELD usable for splitting
|
|
|
|
|
|
|
|
@param
|
|
|
|
key_field KEY_FIELD to collect info on
|
|
|
|
|
|
|
|
@details
|
|
|
|
The function assumes that this table is potentially splittable.
|
|
|
|
The function checks whether the KEY_FIELD structure key_field built for
|
|
|
|
this table was created for a splitting field f. If so, the function does
|
|
|
|
the following using info from key_field:
|
|
|
|
1. Builds an equality of the form f = key_field->val that could be
|
|
|
|
pushed into this table.
|
|
|
|
2. Creates a new KEY_FIELD structure for this equality and stores
|
|
|
|
a reference to this structure in this->spl_opt_info.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void TABLE::add_splitting_info_for_key_field(KEY_FIELD *key_field)
|
|
|
|
{
|
|
|
|
DBUG_ASSERT(spl_opt_info != NULL);
|
|
|
|
JOIN *join= spl_opt_info->join;
|
|
|
|
Field *field= key_field->field;
|
|
|
|
SplM_field_info *spl_field= spl_opt_info->spl_fields;
|
|
|
|
uint i= spl_opt_info->spl_field_cnt;
|
|
|
|
for ( ; i; i--, spl_field++)
|
|
|
|
{
|
|
|
|
if (spl_field->mat_field == field)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!i) // field is not usable for splitting
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Any equality condition that can be potentially pushed into the
|
|
|
|
materialized derived table is constructed now though later it may turn out
|
|
|
|
that it is not needed, because it is not used for splitting.
|
|
|
|
The reason for this is that the failure to construct it when it has to be
|
|
|
|
injected causes denial for further processing of the query.
|
|
|
|
Formally this equality is needed in the KEY_FIELD structure constructed
|
|
|
|
here that will be used to generate additional keyuses usable for splitting.
|
|
|
|
However key_field.cond could be used for this purpose (see implementations
|
|
|
|
of virtual function can_optimize_keypart_ref()).
|
|
|
|
|
|
|
|
The condition is built in such a form that it can be added to the WHERE
|
|
|
|
condition of the select that specifies this table.
|
|
|
|
*/
|
|
|
|
THD *thd= in_use;
|
|
|
|
Item *left_item= spl_field->producing_item->build_clone(thd);
|
|
|
|
Item *right_item= key_field->val->build_clone(thd);
|
|
|
|
Item_func_eq *eq_item= 0;
|
|
|
|
if (left_item && right_item)
|
|
|
|
{
|
|
|
|
right_item->walk(&Item::set_fields_as_dependent_processor,
|
|
|
|
false, join->select_lex);
|
|
|
|
right_item->update_used_tables();
|
|
|
|
eq_item= new (thd->mem_root) Item_func_eq(thd, left_item, right_item);
|
|
|
|
}
|
|
|
|
if (!eq_item)
|
|
|
|
return;
|
|
|
|
KEY_FIELD *added_key_field=
|
|
|
|
(KEY_FIELD *) thd->alloc(sizeof(KEY_FIELD));
|
|
|
|
if (!added_key_field ||
|
|
|
|
spl_opt_info->added_key_fields.push_back(added_key_field,thd->mem_root))
|
|
|
|
return;
|
|
|
|
added_key_field->field= spl_field->underlying_field;
|
|
|
|
added_key_field->cond= eq_item;
|
|
|
|
added_key_field->val= key_field->val;
|
|
|
|
added_key_field->level= 0;
|
|
|
|
added_key_field->optimize= KEY_OPTIMIZE_EQ;
|
|
|
|
added_key_field->eq_func= true;
|
2018-04-30 09:35:01 +02:00
|
|
|
|
|
|
|
Item *real= key_field->val->real_item();
|
|
|
|
if ((real->type() == Item::FIELD_ITEM) &&
|
|
|
|
((Item_field*)real)->field->maybe_null())
|
|
|
|
added_key_field->null_rejecting= true;
|
|
|
|
else
|
|
|
|
added_key_field->null_rejecting= false;
|
|
|
|
|
2017-12-30 21:29:09 +01:00
|
|
|
added_key_field->cond_guard= NULL;
|
|
|
|
added_key_field->sj_pred_no= UINT_MAX;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
add_ext_keyuse_for_splitting(Dynamic_array<KEYUSE_EXT> *ext_keyuses,
|
|
|
|
KEY_FIELD *added_key_field, uint key, uint part)
|
|
|
|
{
|
|
|
|
KEYUSE_EXT keyuse_ext;
|
|
|
|
Field *field= added_key_field->field;
|
|
|
|
|
|
|
|
JOIN_TAB *tab=field->table->reginfo.join_tab;
|
|
|
|
key_map possible_keys=field->get_possible_keys();
|
|
|
|
possible_keys.intersect(field->table->keys_usable_for_splitting);
|
|
|
|
tab->keys.merge(possible_keys);
|
|
|
|
|
|
|
|
Item_func_eq *eq_item= (Item_func_eq *) (added_key_field->cond);
|
|
|
|
keyuse_ext.table= field->table;
|
|
|
|
keyuse_ext.val= eq_item->arguments()[1];
|
|
|
|
keyuse_ext.key= key;
|
|
|
|
keyuse_ext.keypart=part;
|
|
|
|
keyuse_ext.keypart_map= (key_part_map) 1 << part;
|
|
|
|
keyuse_ext.used_tables= keyuse_ext.val->used_tables();
|
|
|
|
keyuse_ext.optimize= added_key_field->optimize & KEY_OPTIMIZE_REF_OR_NULL;
|
|
|
|
keyuse_ext.ref_table_rows= 0;
|
|
|
|
keyuse_ext.null_rejecting= added_key_field->null_rejecting;
|
|
|
|
keyuse_ext.cond_guard= added_key_field->cond_guard;
|
|
|
|
keyuse_ext.sj_pred_no= added_key_field->sj_pred_no;
|
|
|
|
keyuse_ext.validity_ref= 0;
|
|
|
|
keyuse_ext.needed_in_prefix= added_key_field->val->used_tables();
|
|
|
|
keyuse_ext.validity_var= false;
|
|
|
|
return ext_keyuses->push(keyuse_ext);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
sort_ext_keyuse(KEYUSE_EXT *a, KEYUSE_EXT *b)
|
|
|
|
{
|
|
|
|
if (a->table->tablenr != b->table->tablenr)
|
|
|
|
return (int) (a->table->tablenr - b->table->tablenr);
|
|
|
|
if (a->key != b->key)
|
|
|
|
return (int) (a->key - b->key);
|
|
|
|
return (int) (a->keypart - b->keypart);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
sort_ext_keyuses(Dynamic_array<KEYUSE_EXT> *keyuses)
|
|
|
|
{
|
|
|
|
KEYUSE_EXT *first_keyuse= &keyuses->at(0);
|
|
|
|
my_qsort(first_keyuse, keyuses->elements(), sizeof(KEYUSE_EXT),
|
|
|
|
(qsort_cmp) sort_ext_keyuse);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief
|
|
|
|
Add info on keyuses usable for splitting into an array
|
|
|
|
*/
|
|
|
|
|
|
|
|
static bool
|
|
|
|
add_ext_keyuses_for_splitting_field(Dynamic_array<KEYUSE_EXT> *ext_keyuses,
|
|
|
|
KEY_FIELD *added_key_field)
|
|
|
|
{
|
|
|
|
Field *field= added_key_field->field;
|
|
|
|
TABLE *table= field->table;
|
|
|
|
for (uint key= 0; key < table->s->keys; key++)
|
|
|
|
{
|
|
|
|
if (!(table->keys_usable_for_splitting.is_set(key)))
|
|
|
|
continue;
|
|
|
|
KEY *key_info= table->key_info + key;
|
|
|
|
uint key_parts= table->actual_n_key_parts(key_info);
|
|
|
|
KEY_PART_INFO *key_part_info= key_info->key_part;
|
|
|
|
for (uint part=0; part < key_parts; part++, key_part_info++)
|
|
|
|
{
|
|
|
|
if (!field->eq(key_part_info->field))
|
|
|
|
continue;
|
|
|
|
if (add_ext_keyuse_for_splitting(ext_keyuses, added_key_field, key, part))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
@brief
|
|
|
|
Cost of the post join operation used in specification of splittable table
|
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 12:05:23 +02:00
|
|
|
This does not include the cost of creating the temporary table as this
|
|
|
|
operation can be executed many times for the same temporary table.
|
2017-12-30 21:29:09 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
static
|
|
|
|
double spl_postjoin_oper_cost(THD *thd, double join_record_count, uint rec_len)
|
|
|
|
{
|
|
|
|
double cost;
|
Update row and key fetch cost models to take into account data copy costs
Before this patch, when calculating the cost of fetching and using a
row/key from the engine, we took into account the cost of finding a
row or key from the engine, but did not consistently take into account
index only accessed, clustered key or covered keys for all access
paths.
The cost of the WHERE clause (TIME_FOR_COMPARE) was not consistently
considered in best_access_path(). TIME_FOR_COMPARE was used in
calculation in other places, like greedy_search(), but was in some
cases (like scans) done an a different number of rows than was
accessed.
The cost calculation of row and index scans didn't take into account
the number of rows that where accessed, only the number of accepted
rows.
When using a filter, the cost of index_only_reads and cost of
accessing and disregarding 'filtered rows' where not taken into
account, which made filters cost less than there actually where.
To remedy the above, the following key & row fetch related costs
has been added:
- The cost of fetching and using a row is now split into different costs:
- key + Row fetch cost (as before) but multiplied with the variable
'optimizer_cache_cost' (default to 0.5). This allows the user to
tell the optimizer the likehood of finding the key and row in the
engine cache.
- ROW_COPY_COST, The cost copying a row from the engine to the
sql layer or creating a row from the join_cache to the record
buffer. Mostly affects table scan costs.
- ROW_LOOKUP_COST, the cost of fetching a row by rowid.
- KEY_COPY_COST the cost of finding the next key and copying it from
the engine to the SQL layer. This is used when we calculate the cost
index only reads. It makes index scans more expensive than before if
they cover a lot of rows. (main.index_merge_myisam)
- KEY_LOOKUP_COST, the cost of finding the first key in a range.
This replaces the old define IDX_LOOKUP_COST, but with a higher cost.
- KEY_NEXT_FIND_COST, the cost of finding the next key (and rowid).
when doing a index scan and comparing the rowid to the filter.
Before this cost was assumed to be 0.
All of the above constants/variables are now tuned to be somewhat in
proportion of executing complexity to each other. There is tuning
need for these in the future, but that can wait until the above are
made user variables as that will make tuning much easier.
To make the usage of the above easy, there are new (not virtual)
cost calclation functions in handler:
- ha_read_time(), like read_time(), but take optimizer_cache_cost into
account.
- ha_read_and_copy_time(), like ha_read_time() but take into account
ROW_COPY_TIME
- ha_read_and_compare_time(), like ha_read_and_copy_time() but take
TIME_FOR_COMPARE into account.
- ha_rnd_pos_time(). Read row with row id, taking ROW_COPY_COST
into account. This is used with filesort where we don't need
to execute the WHERE clause again.
- ha_keyread_time(), like keyread_time() but take
optimizer_cache_cost into account.
- ha_keyread_and_copy_time(), like ha_keyread_time(), but add
KEY_COPY_COST.
- ha_key_scan_time(), like key_scan_time() but take
optimizer_cache_cost nto account.
- ha_key_scan_and_compare_time(), like ha_key_scan_time(), but add
KEY_COPY_COST & TIME_FOR_COMPARE.
I also added some setup costs for doing different types of scans and
creating temporary tables (on disk and in memory). This encourages
the optimizer to not use these for simple 'a few row' lookups if
there are adequate key lookup strategies.
- TABLE_SCAN_SETUP_COST, cost of starting a table scan.
- INDEX_SCAN_SETUP_COST, cost of starting an index scan.
- HEAP_TEMPTABLE_CREATE_COST, cost of creating in memory
temporary table.
- DISK_TEMPTABLE_CREATE_COST, cost of creating an on disk temporary
table.
When calculating cost of fetching ranges, we had a cost of
IDX_LOOKUP_COST (0.125) for doing a key div for a new range. This is
now replaced with 'io_cost * KEY_LOOKUP_COST (1.0) *
optimizer_cache_cost', which matches the cost we use for 'ref' and
other key lookups. The effect is that the cost is now a bit higher
when we have many ranges for a key.
Allmost all calculation with TIME_FOR_COMPARE is now done in
best_access_path(). 'JOIN::read_time' now includes the full
cost for finding the rows in the table.
In the result files, many of the changes are now again close to what
they where before the "Update cost for hash and cached joins" commit,
as that commit didn't fix the filter cost (too complex to do
everything in one commit).
The above changes showed a lot of a lot of inconsistencies in
optimizer cost calculation. The main objective with the other changes
was to do calculation as similar (and accurate) as possible and to make
different plans more comparable.
Detailed list of changes:
- Calculate index_only_cost consistently and correctly for all scan
and ref accesses. The row fetch_cost and index_only_cost now
takes into account clustered keys, covered keys and index
only accesses.
- cost_for_index_read now returns both full cost and index_only_cost
- Fixed cost calculation of get_sweep_read_cost() to match other
similar costs. This is bases on the assumption that data is more
often stored on SSD than a hard disk.
- Replaced constant 2.0 with new define TABLE_SCAN_SETUP_COST.
- Some scan cost estimates did not take into account
TIME_FOR_COMPARE. Now all scan costs takes this into
account. (main.show_explain)
- Added session variable optimizer_cache_hit_ratio (default 50%). By
adjusting this on can reduce or increase the cost of index or direct
record lookups. The effect of the default is that key lookups is now
a bit cheaper than before. See usage of 'optimizer_cache_cost' in
handler.h.
- JOIN_TAB::scan_time() did not take into account index only scans,
which produced a wrong cost when index scan was used. Changed
JOIN_TAB:::scan_time() to take into consideration clustered and
covered keys. The values are now cached and we only have to call
this function once. Other calls are changed to use the cached
values. Function renamed to JOIN_TAB::estimate_scan_time().
- Fixed that most index cost calculations are done the same way and
more close to 'range' calculations. The cost is now lower than
before for small data sets and higher for large data sets as we take
into account how many keys are read (main.opt_trace_selectivity,
main.limit_rows_examined).
- Ensured that index_scan_cost() ==
range(scan_of_all_rows_in_table_using_one_range) +
MULTI_RANGE_READ_INFO_CONST. One effect of this is that if there
is choice of doing a full index scan and a range-index scan over
almost the whole table then index scan will be preferred (no
range-read setup cost). (innodb.innodb, main.show_explain,
main.range)
- Fixed the EQ_REF and REF takes into account clustered and covered
keys. This changes some plans to use covered or clustered indexes
as these are much cheaper. (main.subselect_mat_cost,
main.state_tables_innodb, main.limit_rows_examined)
- Rowid filter setup cost and filter compare cost now takes into
account fetching and checking the rowid (KEY_NEXT_FIND_COST).
(main.partition_pruning heap.heap_btree main.log_state)
- Added KEY_NEXT_FIND_COST to
Range_rowid_filter_cost_info::lookup_cost to account of the time
to find and check the next key value against the container
- Introduced ha_keyread_time(rows) that takes into account finding
the next row and copying the key value to 'record'
(KEY_COPY_COST).
- Introduced ha_key_scan_time() for calculating an index scan over
all rows.
- Added IDX_LOOKUP_COST to keyread_time() as a startup cost.
- Added index_only_fetch_cost() as a convenience function to
OPT_RANGE.
- keyread_time() cost is slightly reduced to prefer shorter keys.
(main.index_merge_myisam)
- All of the above caused some index_merge combinations to be
rejected because of cost (main.index_intersect). In some cases
'ref' where replaced with index_merge because of the low
cost calculation of get_sweep_read_cost().
- Some index usage moved from PRIMARY to a covering index.
(main.subselect_innodb)
- Changed cost calculation of filter to take KEY_LOOKUP_COST and
TIME_FOR_COMPARE into account. See sql_select.cc::apply_filter().
filter parameters and costs are now written to optimizer_trace.
- Don't use matchings_records_in_range() to try to estimate the number
of filtered rows for ranges. The reason is that we want to ensure
that 'range' is calculated similar to 'ref'. There is also more work
needed to calculate the selectivity when using ranges and ranges and
filtering. This causes filtering column in EXPLAIN EXTENDED to be
100.00 for some cases where range cannot use filtering.
(main.rowid_filter)
- Introduced ha_scan_time() that takes into account the CPU cost of
finding the next row and copying the row from the engine to
'record'. This causes costs of table scan to slightly increase and
some test to changed their plan from ALL to RANGE or ALL to ref.
(innodb.innodb_mysql, main.select_pkeycache)
In a few cases where scan time of very small tables have lower cost
than a ref or range, things changed from ref/range to ALL.
(main.myisam, main.func_group, main.limit_rows_examined,
main.subselect2)
- Introduced ha_scan_and_compare_time() which is like ha_scan_time()
but also adds the cost of the where clause (TIME_FOR_COMPARE).
- Added small cost for creating temporary table for
materialization. This causes some very small tables to use scan
instead of materialization.
- Added checking of the WHERE clause (TIME_FOR_COMPARE) of the
accepted rows to ROR costs in get_best_ror_intersect()
- Removed '- 0.001' from 'join->best_read' and optimize_straight_join()
to ensure that the 'Last_query_cost' status variable contains the
same value as the one that was calculated by the optimizer.
- Take avg_io_cost() into account in handler::keyread_time() and
handler::read_time(). This should have no effect as it's 1.0 by
default, except for heap that overrides these functions.
- Some 'ref_or_null' accesses changed to 'range' because of cost
adjustments (main.order_by)
- Added scan type "scan_with_join_cache" for optimizer_trace. This is
just to show in the trace what kind of scan was used.
- When using 'scan_with_join_cache' take into account number of
preceding tables (as have to restore all fields for all previous
table combination when checking the where clause)
The new cost added is:
(row_combinations * ROW_COPY_COST * number_of_cached_tables).
This increases the cost of join buffering in proportion of the
number of tables in the join buffer. One effect is that full scans
are now done earlier as the cost is then smaller.
(main.join_outer_innodb, main.greedy_optimizer)
- Removed the usage of 'worst_seeks' in cost_for_index_read as it
caused wrong plans to be created; It prefered JT_EQ_REF even if it
would be much more expensive than a full table scan. A related
issue was that worst_seeks only applied to full lookup, not to
clustered or index only lookups, which is not consistent. This
caused some plans to use index scan instead of eq_ref (main.union)
- Changed federated block size from 4096 to 1500, which is the
typical size of an IO packet.
- Added costs for reading rows to Federated. Needed as there is no
caching of rows in the federated engine.
- Added ha_innobase::rnd_pos_time() cost function.
- A lot of extra things added to optimizer trace
- More costs, especially for materialization and index_merge.
- Make lables more uniform
- Fixed a lot of minor bugs
- Added 'trace_started()' around a lot of trace blocks.
- When calculating ORDER BY with LIMIT cost for using an index
the cost did not take into account the number of row retrivals
that has to be done or the cost of comparing the rows with the
WHERE clause. The cost calculated would be just a fraction of
the real cost. Now we calculate the cost as we do for ranges
and 'ref'.
- 'Using index for group-by' is used a bit more than before as
now take into account the WHERE clause cost when comparing
with 'ref' and prefer the method with fewer row combinations.
(main.group_min_max).
Bugs fixed:
- Fixed that we don't calculate TIME_FOR_COMPARE twice for some plans,
like in optimize_straight_join() and greedy_search()
- Fixed bug in save_explain_data where we could test for the wrong
index when displaying 'Using index'. This caused some old plans to
show 'Using index'. (main.subselect_innodb, main.subselect2)
- Fixed bug in get_best_ror_intersect() where 'min_cost' was not
updated, and the cost we compared with was not the one that was
used.
- Fixed very wrong cost calculation for priority queues in
check_if_pq_applicable(). (main.order_by now correctly uses priority
queue)
- When calculating cost of EQ_REF or REF, we added the cost of
comparing the WHERE clause with the found rows, not all row
combinations. This made ref and eq_ref to be regarded way to cheap
compared to other access methods.
- FORCE INDEX cost calculation didn't take into account clustered or
covered indexes.
- JT_EQ_REF cost was estimated as avg_io_cost(), which is half the
cost of a JT_REF key. This may be true for InnoDB primary key, but
not for other unique keys or other engines. Now we use handler
function to calculate the cost, which allows us to handle
consistently clustered, covered keys and not covered keys.
- ha_start_keyread() didn't call extra_opt() if keyread was already
enabled but still changed the 'keyread' variable (which is wrong).
Fixed by not doing anything if keyread is already enabled.
- multi_range_read_info_cost() didn't take into account io_cost when
calculating the cost of ranges.
- fix_semijoin_strategies_for_picked_join_order() used the wrong
record_count when calling best_access_path() for SJ_OPT_FIRST_MATCH
and SJ_OPT_LOOSE_SCAN.
- Hash joins didn't provide correct best_cost to the upper level, which
means that the cost for hash_joins more expensive than calculated
in best_access_path (a difference of 10x * TIME_OF_COMPARE).
This is fixed in the new code thanks to that we now include
TIME_OF_COMPARE cost in 'read_time'.
Other things:
- Added some 'if (thd->trace_started())' to speed up code
- Removed not used function Cost_estimate::is_zero()
- Simplified testing of HA_POS_ERROR in get_best_ror_intersect().
(No cost changes)
- Moved ha_start_keyread() from join_read_const_table() to join_read_const()
to enable keyread for all types of JT_CONST tables.
- Made a few very short functions inline in handler.h
Notes:
- In main.rowid_filter the join order of order and lineitem is swapped.
This is because the cost of doing a range fetch of lineitem(98 rows) is
almost as big as the whole join of order,lineitem. The filtering will
also ensure that we only have to do very small key fetches of the rows
in lineitem.
- main.index_merge_myisam had a few changes where we are now using
less keys for index_merge. This is because index scans are now more
expensive than before.
- handler->optimizer_cache_cost is updated in ha_external_lock().
This ensures that it is up to date per statements.
Not an optimal solution (for locked tables), but should be ok for now.
- 'DELETE FROM t1 WHERE t1.a > 0 ORDER BY t1.a' does not take cost of
filesort into consideration when table scan is chosen.
(main.myisam_explain_non_select_all)
- perfschema.table_aggregate_global_* has changed because an update
on a table with 1 row will now use table scan instead of key lookup.
TODO in upcomming commits:
- Fix selectivity calculation for ranges with and without filtering and
when there is a ref access but scan is chosen.
For this we have to store the lowest known value for
'accepted_records' in the OPT_RANGE structure.
- Change that records_read does not include filtered rows.
- test_if_cheaper_ordering() needs to be updated to properly calculate
costs. This will fix tests like main.order_by_innodb,
main.single_delete_update
- Extend get_range_limit_read_cost() to take into considering
cost_for_index_read() if there where no quick keys. This will reduce
the computed cost for ORDER BY with LIMIT in some cases.
(main.innodb_ext_key)
- Fix that we take into account selectivity when counting the number
of rows we have to read when considering using a index table scan to
resolve ORDER BY.
- Add new calculation for rnd_pos_time() where we take into account the
benefit of reading multiple rows from the same page.
2021-11-01 11:34:24 +01:00
|
|
|
TMPTABLE_COSTS tmp_cost= get_tmp_table_costs(thd, join_record_count,
|
2022-06-16 12:12:01 +02:00
|
|
|
rec_len, 0, 1);
|
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 12:05:23 +02:00
|
|
|
/* cost to fill tmp table */
|
|
|
|
cost= tmp_cost.write * join_record_count;
|
|
|
|
/* cost to perform post join operation used here */
|
Update row and key fetch cost models to take into account data copy costs
Before this patch, when calculating the cost of fetching and using a
row/key from the engine, we took into account the cost of finding a
row or key from the engine, but did not consistently take into account
index only accessed, clustered key or covered keys for all access
paths.
The cost of the WHERE clause (TIME_FOR_COMPARE) was not consistently
considered in best_access_path(). TIME_FOR_COMPARE was used in
calculation in other places, like greedy_search(), but was in some
cases (like scans) done an a different number of rows than was
accessed.
The cost calculation of row and index scans didn't take into account
the number of rows that where accessed, only the number of accepted
rows.
When using a filter, the cost of index_only_reads and cost of
accessing and disregarding 'filtered rows' where not taken into
account, which made filters cost less than there actually where.
To remedy the above, the following key & row fetch related costs
has been added:
- The cost of fetching and using a row is now split into different costs:
- key + Row fetch cost (as before) but multiplied with the variable
'optimizer_cache_cost' (default to 0.5). This allows the user to
tell the optimizer the likehood of finding the key and row in the
engine cache.
- ROW_COPY_COST, The cost copying a row from the engine to the
sql layer or creating a row from the join_cache to the record
buffer. Mostly affects table scan costs.
- ROW_LOOKUP_COST, the cost of fetching a row by rowid.
- KEY_COPY_COST the cost of finding the next key and copying it from
the engine to the SQL layer. This is used when we calculate the cost
index only reads. It makes index scans more expensive than before if
they cover a lot of rows. (main.index_merge_myisam)
- KEY_LOOKUP_COST, the cost of finding the first key in a range.
This replaces the old define IDX_LOOKUP_COST, but with a higher cost.
- KEY_NEXT_FIND_COST, the cost of finding the next key (and rowid).
when doing a index scan and comparing the rowid to the filter.
Before this cost was assumed to be 0.
All of the above constants/variables are now tuned to be somewhat in
proportion of executing complexity to each other. There is tuning
need for these in the future, but that can wait until the above are
made user variables as that will make tuning much easier.
To make the usage of the above easy, there are new (not virtual)
cost calclation functions in handler:
- ha_read_time(), like read_time(), but take optimizer_cache_cost into
account.
- ha_read_and_copy_time(), like ha_read_time() but take into account
ROW_COPY_TIME
- ha_read_and_compare_time(), like ha_read_and_copy_time() but take
TIME_FOR_COMPARE into account.
- ha_rnd_pos_time(). Read row with row id, taking ROW_COPY_COST
into account. This is used with filesort where we don't need
to execute the WHERE clause again.
- ha_keyread_time(), like keyread_time() but take
optimizer_cache_cost into account.
- ha_keyread_and_copy_time(), like ha_keyread_time(), but add
KEY_COPY_COST.
- ha_key_scan_time(), like key_scan_time() but take
optimizer_cache_cost nto account.
- ha_key_scan_and_compare_time(), like ha_key_scan_time(), but add
KEY_COPY_COST & TIME_FOR_COMPARE.
I also added some setup costs for doing different types of scans and
creating temporary tables (on disk and in memory). This encourages
the optimizer to not use these for simple 'a few row' lookups if
there are adequate key lookup strategies.
- TABLE_SCAN_SETUP_COST, cost of starting a table scan.
- INDEX_SCAN_SETUP_COST, cost of starting an index scan.
- HEAP_TEMPTABLE_CREATE_COST, cost of creating in memory
temporary table.
- DISK_TEMPTABLE_CREATE_COST, cost of creating an on disk temporary
table.
When calculating cost of fetching ranges, we had a cost of
IDX_LOOKUP_COST (0.125) for doing a key div for a new range. This is
now replaced with 'io_cost * KEY_LOOKUP_COST (1.0) *
optimizer_cache_cost', which matches the cost we use for 'ref' and
other key lookups. The effect is that the cost is now a bit higher
when we have many ranges for a key.
Allmost all calculation with TIME_FOR_COMPARE is now done in
best_access_path(). 'JOIN::read_time' now includes the full
cost for finding the rows in the table.
In the result files, many of the changes are now again close to what
they where before the "Update cost for hash and cached joins" commit,
as that commit didn't fix the filter cost (too complex to do
everything in one commit).
The above changes showed a lot of a lot of inconsistencies in
optimizer cost calculation. The main objective with the other changes
was to do calculation as similar (and accurate) as possible and to make
different plans more comparable.
Detailed list of changes:
- Calculate index_only_cost consistently and correctly for all scan
and ref accesses. The row fetch_cost and index_only_cost now
takes into account clustered keys, covered keys and index
only accesses.
- cost_for_index_read now returns both full cost and index_only_cost
- Fixed cost calculation of get_sweep_read_cost() to match other
similar costs. This is bases on the assumption that data is more
often stored on SSD than a hard disk.
- Replaced constant 2.0 with new define TABLE_SCAN_SETUP_COST.
- Some scan cost estimates did not take into account
TIME_FOR_COMPARE. Now all scan costs takes this into
account. (main.show_explain)
- Added session variable optimizer_cache_hit_ratio (default 50%). By
adjusting this on can reduce or increase the cost of index or direct
record lookups. The effect of the default is that key lookups is now
a bit cheaper than before. See usage of 'optimizer_cache_cost' in
handler.h.
- JOIN_TAB::scan_time() did not take into account index only scans,
which produced a wrong cost when index scan was used. Changed
JOIN_TAB:::scan_time() to take into consideration clustered and
covered keys. The values are now cached and we only have to call
this function once. Other calls are changed to use the cached
values. Function renamed to JOIN_TAB::estimate_scan_time().
- Fixed that most index cost calculations are done the same way and
more close to 'range' calculations. The cost is now lower than
before for small data sets and higher for large data sets as we take
into account how many keys are read (main.opt_trace_selectivity,
main.limit_rows_examined).
- Ensured that index_scan_cost() ==
range(scan_of_all_rows_in_table_using_one_range) +
MULTI_RANGE_READ_INFO_CONST. One effect of this is that if there
is choice of doing a full index scan and a range-index scan over
almost the whole table then index scan will be preferred (no
range-read setup cost). (innodb.innodb, main.show_explain,
main.range)
- Fixed the EQ_REF and REF takes into account clustered and covered
keys. This changes some plans to use covered or clustered indexes
as these are much cheaper. (main.subselect_mat_cost,
main.state_tables_innodb, main.limit_rows_examined)
- Rowid filter setup cost and filter compare cost now takes into
account fetching and checking the rowid (KEY_NEXT_FIND_COST).
(main.partition_pruning heap.heap_btree main.log_state)
- Added KEY_NEXT_FIND_COST to
Range_rowid_filter_cost_info::lookup_cost to account of the time
to find and check the next key value against the container
- Introduced ha_keyread_time(rows) that takes into account finding
the next row and copying the key value to 'record'
(KEY_COPY_COST).
- Introduced ha_key_scan_time() for calculating an index scan over
all rows.
- Added IDX_LOOKUP_COST to keyread_time() as a startup cost.
- Added index_only_fetch_cost() as a convenience function to
OPT_RANGE.
- keyread_time() cost is slightly reduced to prefer shorter keys.
(main.index_merge_myisam)
- All of the above caused some index_merge combinations to be
rejected because of cost (main.index_intersect). In some cases
'ref' where replaced with index_merge because of the low
cost calculation of get_sweep_read_cost().
- Some index usage moved from PRIMARY to a covering index.
(main.subselect_innodb)
- Changed cost calculation of filter to take KEY_LOOKUP_COST and
TIME_FOR_COMPARE into account. See sql_select.cc::apply_filter().
filter parameters and costs are now written to optimizer_trace.
- Don't use matchings_records_in_range() to try to estimate the number
of filtered rows for ranges. The reason is that we want to ensure
that 'range' is calculated similar to 'ref'. There is also more work
needed to calculate the selectivity when using ranges and ranges and
filtering. This causes filtering column in EXPLAIN EXTENDED to be
100.00 for some cases where range cannot use filtering.
(main.rowid_filter)
- Introduced ha_scan_time() that takes into account the CPU cost of
finding the next row and copying the row from the engine to
'record'. This causes costs of table scan to slightly increase and
some test to changed their plan from ALL to RANGE or ALL to ref.
(innodb.innodb_mysql, main.select_pkeycache)
In a few cases where scan time of very small tables have lower cost
than a ref or range, things changed from ref/range to ALL.
(main.myisam, main.func_group, main.limit_rows_examined,
main.subselect2)
- Introduced ha_scan_and_compare_time() which is like ha_scan_time()
but also adds the cost of the where clause (TIME_FOR_COMPARE).
- Added small cost for creating temporary table for
materialization. This causes some very small tables to use scan
instead of materialization.
- Added checking of the WHERE clause (TIME_FOR_COMPARE) of the
accepted rows to ROR costs in get_best_ror_intersect()
- Removed '- 0.001' from 'join->best_read' and optimize_straight_join()
to ensure that the 'Last_query_cost' status variable contains the
same value as the one that was calculated by the optimizer.
- Take avg_io_cost() into account in handler::keyread_time() and
handler::read_time(). This should have no effect as it's 1.0 by
default, except for heap that overrides these functions.
- Some 'ref_or_null' accesses changed to 'range' because of cost
adjustments (main.order_by)
- Added scan type "scan_with_join_cache" for optimizer_trace. This is
just to show in the trace what kind of scan was used.
- When using 'scan_with_join_cache' take into account number of
preceding tables (as have to restore all fields for all previous
table combination when checking the where clause)
The new cost added is:
(row_combinations * ROW_COPY_COST * number_of_cached_tables).
This increases the cost of join buffering in proportion of the
number of tables in the join buffer. One effect is that full scans
are now done earlier as the cost is then smaller.
(main.join_outer_innodb, main.greedy_optimizer)
- Removed the usage of 'worst_seeks' in cost_for_index_read as it
caused wrong plans to be created; It prefered JT_EQ_REF even if it
would be much more expensive than a full table scan. A related
issue was that worst_seeks only applied to full lookup, not to
clustered or index only lookups, which is not consistent. This
caused some plans to use index scan instead of eq_ref (main.union)
- Changed federated block size from 4096 to 1500, which is the
typical size of an IO packet.
- Added costs for reading rows to Federated. Needed as there is no
caching of rows in the federated engine.
- Added ha_innobase::rnd_pos_time() cost function.
- A lot of extra things added to optimizer trace
- More costs, especially for materialization and index_merge.
- Make lables more uniform
- Fixed a lot of minor bugs
- Added 'trace_started()' around a lot of trace blocks.
- When calculating ORDER BY with LIMIT cost for using an index
the cost did not take into account the number of row retrivals
that has to be done or the cost of comparing the rows with the
WHERE clause. The cost calculated would be just a fraction of
the real cost. Now we calculate the cost as we do for ranges
and 'ref'.
- 'Using index for group-by' is used a bit more than before as
now take into account the WHERE clause cost when comparing
with 'ref' and prefer the method with fewer row combinations.
(main.group_min_max).
Bugs fixed:
- Fixed that we don't calculate TIME_FOR_COMPARE twice for some plans,
like in optimize_straight_join() and greedy_search()
- Fixed bug in save_explain_data where we could test for the wrong
index when displaying 'Using index'. This caused some old plans to
show 'Using index'. (main.subselect_innodb, main.subselect2)
- Fixed bug in get_best_ror_intersect() where 'min_cost' was not
updated, and the cost we compared with was not the one that was
used.
- Fixed very wrong cost calculation for priority queues in
check_if_pq_applicable(). (main.order_by now correctly uses priority
queue)
- When calculating cost of EQ_REF or REF, we added the cost of
comparing the WHERE clause with the found rows, not all row
combinations. This made ref and eq_ref to be regarded way to cheap
compared to other access methods.
- FORCE INDEX cost calculation didn't take into account clustered or
covered indexes.
- JT_EQ_REF cost was estimated as avg_io_cost(), which is half the
cost of a JT_REF key. This may be true for InnoDB primary key, but
not for other unique keys or other engines. Now we use handler
function to calculate the cost, which allows us to handle
consistently clustered, covered keys and not covered keys.
- ha_start_keyread() didn't call extra_opt() if keyread was already
enabled but still changed the 'keyread' variable (which is wrong).
Fixed by not doing anything if keyread is already enabled.
- multi_range_read_info_cost() didn't take into account io_cost when
calculating the cost of ranges.
- fix_semijoin_strategies_for_picked_join_order() used the wrong
record_count when calling best_access_path() for SJ_OPT_FIRST_MATCH
and SJ_OPT_LOOSE_SCAN.
- Hash joins didn't provide correct best_cost to the upper level, which
means that the cost for hash_joins more expensive than calculated
in best_access_path (a difference of 10x * TIME_OF_COMPARE).
This is fixed in the new code thanks to that we now include
TIME_OF_COMPARE cost in 'read_time'.
Other things:
- Added some 'if (thd->trace_started())' to speed up code
- Removed not used function Cost_estimate::is_zero()
- Simplified testing of HA_POS_ERROR in get_best_ror_intersect().
(No cost changes)
- Moved ha_start_keyread() from join_read_const_table() to join_read_const()
to enable keyread for all types of JT_CONST tables.
- Made a few very short functions inline in handler.h
Notes:
- In main.rowid_filter the join order of order and lineitem is swapped.
This is because the cost of doing a range fetch of lineitem(98 rows) is
almost as big as the whole join of order,lineitem. The filtering will
also ensure that we only have to do very small key fetches of the rows
in lineitem.
- main.index_merge_myisam had a few changes where we are now using
less keys for index_merge. This is because index scans are now more
expensive than before.
- handler->optimizer_cache_cost is updated in ha_external_lock().
This ensures that it is up to date per statements.
Not an optimal solution (for locked tables), but should be ok for now.
- 'DELETE FROM t1 WHERE t1.a > 0 ORDER BY t1.a' does not take cost of
filesort into consideration when table scan is chosen.
(main.myisam_explain_non_select_all)
- perfschema.table_aggregate_global_* has changed because an update
on a table with 1 row will now use table scan instead of key lookup.
TODO in upcomming commits:
- Fix selectivity calculation for ranges with and without filtering and
when there is a ref access but scan is chosen.
For this we have to store the lowest known value for
'accepted_records' in the OPT_RANGE structure.
- Change that records_read does not include filtered rows.
- test_if_cheaper_ordering() needs to be updated to properly calculate
costs. This will fix tests like main.order_by_innodb,
main.single_delete_update
- Extend get_range_limit_read_cost() to take into considering
cost_for_index_read() if there where no quick keys. This will reduce
the computed cost for ORDER BY with LIMIT in some cases.
(main.innodb_ext_key)
- Fix that we take into account selectivity when counting the number
of rows we have to read when considering using a index table scan to
resolve ORDER BY.
- Add new calculation for rnd_pos_time() where we take into account the
benefit of reading multiple rows from the same page.
2021-11-01 11:34:24 +01:00
|
|
|
cost+= tmp_cost.lookup * join_record_count;
|
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 12:05:23 +02:00
|
|
|
/* cost to preform sorting */
|
|
|
|
/* QQQ
|
|
|
|
We should use cost_of_filesort() for computing sort.
|
|
|
|
Do we always preform sorting ? If not, this should be done conditionally
|
|
|
|
*/
|
|
|
|
cost+= ((join_record_count == 0 ? 0 :
|
|
|
|
join_record_count * log2 (join_record_count)) *
|
|
|
|
SORT_INDEX_CMP_COST);
|
2017-12-30 21:29:09 +01:00
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief
|
|
|
|
Add KEYUSE structures that can be usable for splitting
|
|
|
|
|
|
|
|
@details
|
|
|
|
This function is called only for joins created for potentially
|
|
|
|
splittable materialized tables. The function does the following:
|
|
|
|
1. Creates the dynamic array ext_keyuses_for_splitting of KEYUSE_EXT
|
|
|
|
structures and fills is with info about all keyuses that
|
|
|
|
could be used for splitting.
|
|
|
|
2. Sort the array ext_keyuses_for_splitting for fast access by key
|
|
|
|
on certain columns.
|
|
|
|
3. Collects and stores cost and cardinality info on the best execution
|
|
|
|
plan that does not use splitting and save this plan together with
|
|
|
|
corresponding array of keyuses.
|
|
|
|
4. Expand this array with KEYUSE elements built from the info stored
|
|
|
|
in ext_keyuses_for_splitting that could be produced by pushed
|
|
|
|
equalities employed for splitting.
|
|
|
|
5. Prepare the extended array of keyuses to be used in the function
|
|
|
|
best_access_plan()
|
|
|
|
*/
|
|
|
|
|
|
|
|
void JOIN::add_keyuses_for_splitting()
|
|
|
|
{
|
|
|
|
uint i;
|
2021-09-03 06:38:54 +02:00
|
|
|
size_t idx;
|
2017-12-30 21:29:09 +01:00
|
|
|
KEYUSE_EXT *keyuse_ext;
|
|
|
|
KEYUSE_EXT keyuse_ext_end;
|
|
|
|
double oper_cost;
|
|
|
|
uint rec_len;
|
|
|
|
uint added_keyuse_count;
|
|
|
|
TABLE *table= select_lex->master_unit()->derived->table;
|
|
|
|
List_iterator_fast<KEY_FIELD> li(spl_opt_info->added_key_fields);
|
|
|
|
KEY_FIELD *added_key_field;
|
|
|
|
if (!spl_opt_info->added_key_fields.elements)
|
|
|
|
goto err;
|
2020-01-29 13:50:26 +01:00
|
|
|
if (!(ext_keyuses_for_splitting= new Dynamic_array<KEYUSE_EXT>(PSI_INSTRUMENT_MEM)))
|
2017-12-30 21:29:09 +01:00
|
|
|
goto err;
|
|
|
|
while ((added_key_field= li++))
|
|
|
|
{
|
|
|
|
(void) add_ext_keyuses_for_splitting_field(ext_keyuses_for_splitting,
|
|
|
|
added_key_field);
|
|
|
|
}
|
2018-02-06 13:55:58 +01:00
|
|
|
added_keyuse_count= (uint)ext_keyuses_for_splitting->elements();
|
2017-12-30 21:29:09 +01:00
|
|
|
if (!added_keyuse_count)
|
|
|
|
goto err;
|
|
|
|
sort_ext_keyuses(ext_keyuses_for_splitting);
|
|
|
|
bzero((char*) &keyuse_ext_end, sizeof(keyuse_ext_end));
|
|
|
|
if (ext_keyuses_for_splitting->push(keyuse_ext_end))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
spl_opt_info->unsplit_card= join_record_count;
|
|
|
|
|
|
|
|
rec_len= table->s->rec_buff_length;
|
|
|
|
|
|
|
|
oper_cost= spl_postjoin_oper_cost(thd, join_record_count, rec_len);
|
|
|
|
|
|
|
|
spl_opt_info->unsplit_cost= best_positions[table_count-1].read_time +
|
|
|
|
oper_cost;
|
|
|
|
|
|
|
|
if (!(save_qep= new Join_plan_state(table_count + 1)))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
save_query_plan(save_qep);
|
|
|
|
|
|
|
|
if (!keyuse.buffer &&
|
2020-02-27 11:52:20 +01:00
|
|
|
my_init_dynamic_array(PSI_INSTRUMENT_ME, &keyuse, sizeof(KEYUSE),
|
2020-01-29 13:50:26 +01:00
|
|
|
20, 64, MYF(MY_THREAD_SPECIFIC)))
|
2017-12-30 21:29:09 +01:00
|
|
|
goto err;
|
|
|
|
|
2020-01-29 13:50:26 +01:00
|
|
|
if (allocate_dynamic(&keyuse, save_qep->keyuse.elements + added_keyuse_count))
|
2017-12-30 21:29:09 +01:00
|
|
|
goto err;
|
|
|
|
|
2020-11-02 14:48:47 +01:00
|
|
|
idx= keyuse.elements= save_qep->keyuse.elements;
|
|
|
|
if (keyuse.elements)
|
|
|
|
memcpy(keyuse.buffer,
|
|
|
|
save_qep->keyuse.buffer,
|
|
|
|
(size_t) keyuse.elements * keyuse.size_of_element);
|
2017-12-30 21:29:09 +01:00
|
|
|
|
|
|
|
keyuse_ext= &ext_keyuses_for_splitting->at(0);
|
|
|
|
for (i=0; i < added_keyuse_count; i++, keyuse_ext++, idx++)
|
|
|
|
{
|
|
|
|
set_dynamic(&keyuse, (KEYUSE *) keyuse_ext, idx);
|
|
|
|
KEYUSE *added_keyuse= ((KEYUSE *) (keyuse.buffer)) + idx;
|
|
|
|
added_keyuse->validity_ref= &keyuse_ext->validity_var;
|
|
|
|
}
|
|
|
|
|
2022-06-02 18:47:23 +02:00
|
|
|
if (sort_and_filter_keyuse(this, &keyuse, true))
|
2017-12-30 21:29:09 +01:00
|
|
|
goto err;
|
|
|
|
optimize_keyuse(this, &keyuse);
|
|
|
|
|
|
|
|
for (uint i= 0; i < table_count; i++)
|
|
|
|
{
|
|
|
|
JOIN_TAB *tab= join_tab + i;
|
|
|
|
map2table[tab->table->tablenr]= tab;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
err:
|
|
|
|
if (save_qep)
|
|
|
|
restore_query_plan(save_qep);
|
|
|
|
table->deny_splitting();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief
|
|
|
|
Add KEYUSE structures that can be usable for splitting of this joined table
|
|
|
|
*/
|
|
|
|
|
|
|
|
void JOIN_TAB::add_keyuses_for_splitting()
|
|
|
|
{
|
|
|
|
DBUG_ASSERT(table->spl_opt_info != NULL);
|
|
|
|
SplM_opt_info *spl_opt_info= table->spl_opt_info;
|
|
|
|
spl_opt_info->join->add_keyuses_for_splitting();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
@brief
|
|
|
|
Find info on the splitting plan by the splitting key
|
|
|
|
*/
|
|
|
|
|
|
|
|
SplM_plan_info *SplM_opt_info::find_plan(TABLE *table, uint key, uint parts)
|
|
|
|
{
|
|
|
|
List_iterator_fast<SplM_plan_info> li(plan_cache);
|
|
|
|
SplM_plan_info *spl_plan;
|
|
|
|
while ((spl_plan= li++))
|
|
|
|
{
|
|
|
|
if (spl_plan->table == table &&
|
|
|
|
spl_plan->key == key &&
|
|
|
|
spl_plan->parts == parts)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return spl_plan;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
@breaf
|
|
|
|
Enable/Disable a keyuses that can be used for splitting
|
|
|
|
*/
|
|
|
|
|
|
|
|
static
|
|
|
|
void reset_validity_vars_for_keyuses(KEYUSE_EXT *key_keyuse_ext_start,
|
|
|
|
TABLE *table, uint key,
|
|
|
|
table_map remaining_tables,
|
|
|
|
bool validity_val)
|
|
|
|
{
|
|
|
|
KEYUSE_EXT *keyuse_ext= key_keyuse_ext_start;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (!(keyuse_ext->needed_in_prefix & remaining_tables))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
The enabling/disabling flags are set just in KEYUSE_EXT structures.
|
|
|
|
Yet keyuses that are used by best_access_path() have pointers
|
|
|
|
to these flags.
|
|
|
|
*/
|
|
|
|
keyuse_ext->validity_var= validity_val;
|
|
|
|
}
|
|
|
|
keyuse_ext++;
|
|
|
|
}
|
|
|
|
while (keyuse_ext->key == key && keyuse_ext->table == table);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief
|
|
|
|
Choose the best splitting to extend the evaluated partial join
|
|
|
|
|
|
|
|
@param
|
|
|
|
record_count estimated cardinality of the extended partial join
|
|
|
|
remaining_tables tables not joined yet
|
|
|
|
|
|
|
|
@details
|
|
|
|
This function is called during the search for the best execution
|
|
|
|
plan of the join that contains this table T. The function is called
|
|
|
|
every time when the optimizer tries to extend a partial join by
|
|
|
|
joining it with table T. Depending on what tables are already in the
|
|
|
|
partial join different equalities usable for splitting can be pushed
|
|
|
|
into T. The function evaluates different variants and chooses the
|
|
|
|
best one. Then the function finds the plan for the materializing join
|
|
|
|
with the chosen equality conditions pushed into it. If the cost of the
|
|
|
|
plan turns out to be less than the cost of the best plan without
|
|
|
|
splitting the function set it as the true plan of materialization
|
|
|
|
of the table T.
|
|
|
|
The function caches the found plans for materialization of table T
|
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 12:05:23 +02:00
|
|
|
together with the info what key was used for splitting. Next time when
|
2017-12-30 21:29:09 +01:00
|
|
|
the optimizer prefers to use the same key the plan is taken from
|
|
|
|
the cache of plans
|
|
|
|
|
|
|
|
@retval
|
|
|
|
Pointer to the info on the found plan that employs the pushed equalities
|
|
|
|
if the plan has been chosen, NULL - otherwise.
|
|
|
|
*/
|
|
|
|
|
|
|
|
SplM_plan_info * JOIN_TAB::choose_best_splitting(double record_count,
|
|
|
|
table_map remaining_tables)
|
|
|
|
{
|
|
|
|
SplM_opt_info *spl_opt_info= table->spl_opt_info;
|
|
|
|
DBUG_ASSERT(spl_opt_info != NULL);
|
|
|
|
JOIN *join= spl_opt_info->join;
|
|
|
|
THD *thd= join->thd;
|
|
|
|
table_map tables_usable_for_splitting=
|
|
|
|
spl_opt_info->tables_usable_for_splitting;
|
|
|
|
KEYUSE_EXT *keyuse_ext= &join->ext_keyuses_for_splitting->at(0);
|
2018-04-11 01:20:22 +02:00
|
|
|
KEYUSE_EXT *UNINIT_VAR(best_key_keyuse_ext_start);
|
2017-12-30 21:29:09 +01:00
|
|
|
TABLE *best_table= 0;
|
|
|
|
double best_rec_per_key= DBL_MAX;
|
|
|
|
SplM_plan_info *spl_plan= 0;
|
2018-03-25 13:05:30 +02:00
|
|
|
uint best_key= 0;
|
|
|
|
uint best_key_parts= 0;
|
2017-12-30 21:29:09 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
Check whether there are keys that can be used to join T employing splitting
|
|
|
|
and if so, select the best out of such keys
|
|
|
|
*/
|
|
|
|
for (uint tablenr= 0; tablenr < join->table_count; tablenr++)
|
|
|
|
{
|
2018-01-06 23:11:42 +01:00
|
|
|
if (!((1ULL << tablenr) & tables_usable_for_splitting))
|
2017-12-30 21:29:09 +01:00
|
|
|
continue;
|
|
|
|
JOIN_TAB *tab= join->map2table[tablenr];
|
|
|
|
TABLE *table= tab->table;
|
2018-09-18 03:49:53 +02:00
|
|
|
if (keyuse_ext->table != table)
|
|
|
|
continue;
|
2017-12-30 21:29:09 +01:00
|
|
|
do
|
|
|
|
{
|
|
|
|
uint key= keyuse_ext->key;
|
|
|
|
KEYUSE_EXT *key_keyuse_ext_start= keyuse_ext;
|
|
|
|
key_part_map found_parts= 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (keyuse_ext->needed_in_prefix & remaining_tables)
|
|
|
|
{
|
|
|
|
keyuse_ext++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!(keyuse_ext->keypart_map & found_parts))
|
|
|
|
{
|
|
|
|
if ((!found_parts && !keyuse_ext->keypart) ||
|
|
|
|
(found_parts && ((keyuse_ext->keypart_map >> 1) & found_parts)))
|
|
|
|
found_parts|= keyuse_ext->keypart_map;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
keyuse_ext++;
|
|
|
|
}
|
|
|
|
while (keyuse_ext->key == key && keyuse_ext->table == table);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
KEY *key_info= table->key_info + key;
|
|
|
|
double rec_per_key=
|
|
|
|
key_info->actual_rec_per_key(keyuse_ext->keypart);
|
|
|
|
if (rec_per_key < best_rec_per_key)
|
|
|
|
{
|
|
|
|
best_table= keyuse_ext->table;
|
|
|
|
best_key= keyuse_ext->key;
|
|
|
|
best_key_parts= keyuse_ext->keypart + 1;
|
|
|
|
best_rec_per_key= rec_per_key;
|
|
|
|
best_key_keyuse_ext_start= key_keyuse_ext_start;
|
|
|
|
}
|
|
|
|
keyuse_ext++;
|
|
|
|
}
|
|
|
|
while (keyuse_ext->key == key && keyuse_ext->table == table);
|
|
|
|
}
|
|
|
|
while (keyuse_ext->table == table);
|
|
|
|
}
|
2018-01-09 00:21:52 +01:00
|
|
|
spl_opt_info->last_plan= 0;
|
2017-12-30 21:29:09 +01:00
|
|
|
if (best_table)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
The key for splitting was chosen, look for the plan for this key
|
|
|
|
in the cache
|
|
|
|
*/
|
2021-03-28 23:33:27 +02:00
|
|
|
Json_writer_array spl_trace(thd, "choose_best_splitting");
|
2017-12-30 21:29:09 +01:00
|
|
|
spl_plan= spl_opt_info->find_plan(best_table, best_key, best_key_parts);
|
2021-05-27 08:41:59 +02:00
|
|
|
if (!spl_plan)
|
2017-12-30 21:29:09 +01:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
The plan for the chosen key has not been found in the cache.
|
|
|
|
Build a new plan and save info on it in the cache
|
|
|
|
*/
|
2020-03-24 03:20:48 +01:00
|
|
|
table_map all_table_map= (((table_map) 1) << join->table_count) - 1;
|
2017-12-30 21:29:09 +01:00
|
|
|
reset_validity_vars_for_keyuses(best_key_keyuse_ext_start, best_table,
|
|
|
|
best_key, remaining_tables, true);
|
2022-06-13 16:45:37 +02:00
|
|
|
choose_plan(join, all_table_map & ~join->const_table_map, 0);
|
2021-05-27 08:41:59 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
Check that the chosen plan is really a splitting plan.
|
|
|
|
If not or if there is not enough memory to save the plan in the cache
|
|
|
|
then just return with no splitting plan.
|
|
|
|
*/
|
|
|
|
POSITION *first_non_const_pos= join->best_positions + join->const_tables;
|
|
|
|
TABLE *table= first_non_const_pos->table->table;
|
|
|
|
key_map spl_keys= table->keys_usable_for_splitting;
|
|
|
|
if (!(first_non_const_pos->key &&
|
|
|
|
spl_keys.is_set(first_non_const_pos->key->key)) ||
|
|
|
|
!(spl_plan= (SplM_plan_info *) thd->alloc(sizeof(SplM_plan_info))) ||
|
|
|
|
!(spl_plan->best_positions=
|
|
|
|
(POSITION *) thd->alloc(sizeof(POSITION) * join->table_count)) ||
|
|
|
|
spl_opt_info->plan_cache.push_back(spl_plan))
|
|
|
|
{
|
|
|
|
reset_validity_vars_for_keyuses(best_key_keyuse_ext_start, best_table,
|
|
|
|
best_key, remaining_tables, false);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-12-30 21:29:09 +01:00
|
|
|
spl_plan->keyuse_ext_start= best_key_keyuse_ext_start;
|
|
|
|
spl_plan->table= best_table;
|
|
|
|
spl_plan->key= best_key;
|
|
|
|
spl_plan->parts= best_key_parts;
|
2018-01-31 06:12:11 +01:00
|
|
|
spl_plan->split_sel= best_rec_per_key /
|
|
|
|
(spl_opt_info->unsplit_card ?
|
|
|
|
spl_opt_info->unsplit_card : 1);
|
2017-12-30 21:29:09 +01:00
|
|
|
|
|
|
|
uint rec_len= table->s->rec_buff_length;
|
|
|
|
double split_card= spl_opt_info->unsplit_card * spl_plan->split_sel;
|
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 12:05:23 +02:00
|
|
|
double oper_cost= (split_card *
|
|
|
|
spl_postjoin_oper_cost(thd, split_card, rec_len));
|
|
|
|
spl_plan->cost= (join->best_positions[join->table_count-1].read_time +
|
|
|
|
oper_cost);
|
2017-12-30 21:29:09 +01:00
|
|
|
|
2021-03-28 23:33:27 +02:00
|
|
|
if (unlikely(thd->trace_started()))
|
|
|
|
{
|
|
|
|
Json_writer_object wrapper(thd);
|
|
|
|
Json_writer_object find_trace(thd, "best_splitting");
|
2022-01-20 14:49:01 +01:00
|
|
|
find_trace.
|
|
|
|
add("table", best_table->alias.c_ptr()).
|
|
|
|
add("key", best_table->key_info[best_key].name).
|
|
|
|
add("record_count", record_count).
|
|
|
|
add("cost", spl_plan->cost).
|
|
|
|
add("unsplit_cost", spl_opt_info->unsplit_cost);
|
2021-03-28 23:33:27 +02:00
|
|
|
}
|
2017-12-30 21:29:09 +01:00
|
|
|
memcpy((char *) spl_plan->best_positions,
|
|
|
|
(char *) join->best_positions,
|
|
|
|
sizeof(POSITION) * join->table_count);
|
|
|
|
reset_validity_vars_for_keyuses(best_key_keyuse_ext_start, best_table,
|
|
|
|
best_key, remaining_tables, false);
|
|
|
|
}
|
|
|
|
if (spl_plan)
|
|
|
|
{
|
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 12:05:23 +02:00
|
|
|
if (record_count * spl_plan->cost + COST_EPS < spl_opt_info->unsplit_cost)
|
2017-12-30 21:29:09 +01:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
The best plan that employs splitting is cheaper than
|
|
|
|
the plan without splitting
|
|
|
|
*/
|
|
|
|
spl_opt_info->last_plan= spl_plan;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the cost of the preferred materialization for this partial join */
|
|
|
|
spl_plan= spl_opt_info->last_plan;
|
|
|
|
if (spl_plan)
|
|
|
|
{
|
|
|
|
startup_cost= record_count * spl_plan->cost;
|
Update row and key fetch cost models to take into account data copy costs
Before this patch, when calculating the cost of fetching and using a
row/key from the engine, we took into account the cost of finding a
row or key from the engine, but did not consistently take into account
index only accessed, clustered key or covered keys for all access
paths.
The cost of the WHERE clause (TIME_FOR_COMPARE) was not consistently
considered in best_access_path(). TIME_FOR_COMPARE was used in
calculation in other places, like greedy_search(), but was in some
cases (like scans) done an a different number of rows than was
accessed.
The cost calculation of row and index scans didn't take into account
the number of rows that where accessed, only the number of accepted
rows.
When using a filter, the cost of index_only_reads and cost of
accessing and disregarding 'filtered rows' where not taken into
account, which made filters cost less than there actually where.
To remedy the above, the following key & row fetch related costs
has been added:
- The cost of fetching and using a row is now split into different costs:
- key + Row fetch cost (as before) but multiplied with the variable
'optimizer_cache_cost' (default to 0.5). This allows the user to
tell the optimizer the likehood of finding the key and row in the
engine cache.
- ROW_COPY_COST, The cost copying a row from the engine to the
sql layer or creating a row from the join_cache to the record
buffer. Mostly affects table scan costs.
- ROW_LOOKUP_COST, the cost of fetching a row by rowid.
- KEY_COPY_COST the cost of finding the next key and copying it from
the engine to the SQL layer. This is used when we calculate the cost
index only reads. It makes index scans more expensive than before if
they cover a lot of rows. (main.index_merge_myisam)
- KEY_LOOKUP_COST, the cost of finding the first key in a range.
This replaces the old define IDX_LOOKUP_COST, but with a higher cost.
- KEY_NEXT_FIND_COST, the cost of finding the next key (and rowid).
when doing a index scan and comparing the rowid to the filter.
Before this cost was assumed to be 0.
All of the above constants/variables are now tuned to be somewhat in
proportion of executing complexity to each other. There is tuning
need for these in the future, but that can wait until the above are
made user variables as that will make tuning much easier.
To make the usage of the above easy, there are new (not virtual)
cost calclation functions in handler:
- ha_read_time(), like read_time(), but take optimizer_cache_cost into
account.
- ha_read_and_copy_time(), like ha_read_time() but take into account
ROW_COPY_TIME
- ha_read_and_compare_time(), like ha_read_and_copy_time() but take
TIME_FOR_COMPARE into account.
- ha_rnd_pos_time(). Read row with row id, taking ROW_COPY_COST
into account. This is used with filesort where we don't need
to execute the WHERE clause again.
- ha_keyread_time(), like keyread_time() but take
optimizer_cache_cost into account.
- ha_keyread_and_copy_time(), like ha_keyread_time(), but add
KEY_COPY_COST.
- ha_key_scan_time(), like key_scan_time() but take
optimizer_cache_cost nto account.
- ha_key_scan_and_compare_time(), like ha_key_scan_time(), but add
KEY_COPY_COST & TIME_FOR_COMPARE.
I also added some setup costs for doing different types of scans and
creating temporary tables (on disk and in memory). This encourages
the optimizer to not use these for simple 'a few row' lookups if
there are adequate key lookup strategies.
- TABLE_SCAN_SETUP_COST, cost of starting a table scan.
- INDEX_SCAN_SETUP_COST, cost of starting an index scan.
- HEAP_TEMPTABLE_CREATE_COST, cost of creating in memory
temporary table.
- DISK_TEMPTABLE_CREATE_COST, cost of creating an on disk temporary
table.
When calculating cost of fetching ranges, we had a cost of
IDX_LOOKUP_COST (0.125) for doing a key div for a new range. This is
now replaced with 'io_cost * KEY_LOOKUP_COST (1.0) *
optimizer_cache_cost', which matches the cost we use for 'ref' and
other key lookups. The effect is that the cost is now a bit higher
when we have many ranges for a key.
Allmost all calculation with TIME_FOR_COMPARE is now done in
best_access_path(). 'JOIN::read_time' now includes the full
cost for finding the rows in the table.
In the result files, many of the changes are now again close to what
they where before the "Update cost for hash and cached joins" commit,
as that commit didn't fix the filter cost (too complex to do
everything in one commit).
The above changes showed a lot of a lot of inconsistencies in
optimizer cost calculation. The main objective with the other changes
was to do calculation as similar (and accurate) as possible and to make
different plans more comparable.
Detailed list of changes:
- Calculate index_only_cost consistently and correctly for all scan
and ref accesses. The row fetch_cost and index_only_cost now
takes into account clustered keys, covered keys and index
only accesses.
- cost_for_index_read now returns both full cost and index_only_cost
- Fixed cost calculation of get_sweep_read_cost() to match other
similar costs. This is bases on the assumption that data is more
often stored on SSD than a hard disk.
- Replaced constant 2.0 with new define TABLE_SCAN_SETUP_COST.
- Some scan cost estimates did not take into account
TIME_FOR_COMPARE. Now all scan costs takes this into
account. (main.show_explain)
- Added session variable optimizer_cache_hit_ratio (default 50%). By
adjusting this on can reduce or increase the cost of index or direct
record lookups. The effect of the default is that key lookups is now
a bit cheaper than before. See usage of 'optimizer_cache_cost' in
handler.h.
- JOIN_TAB::scan_time() did not take into account index only scans,
which produced a wrong cost when index scan was used. Changed
JOIN_TAB:::scan_time() to take into consideration clustered and
covered keys. The values are now cached and we only have to call
this function once. Other calls are changed to use the cached
values. Function renamed to JOIN_TAB::estimate_scan_time().
- Fixed that most index cost calculations are done the same way and
more close to 'range' calculations. The cost is now lower than
before for small data sets and higher for large data sets as we take
into account how many keys are read (main.opt_trace_selectivity,
main.limit_rows_examined).
- Ensured that index_scan_cost() ==
range(scan_of_all_rows_in_table_using_one_range) +
MULTI_RANGE_READ_INFO_CONST. One effect of this is that if there
is choice of doing a full index scan and a range-index scan over
almost the whole table then index scan will be preferred (no
range-read setup cost). (innodb.innodb, main.show_explain,
main.range)
- Fixed the EQ_REF and REF takes into account clustered and covered
keys. This changes some plans to use covered or clustered indexes
as these are much cheaper. (main.subselect_mat_cost,
main.state_tables_innodb, main.limit_rows_examined)
- Rowid filter setup cost and filter compare cost now takes into
account fetching and checking the rowid (KEY_NEXT_FIND_COST).
(main.partition_pruning heap.heap_btree main.log_state)
- Added KEY_NEXT_FIND_COST to
Range_rowid_filter_cost_info::lookup_cost to account of the time
to find and check the next key value against the container
- Introduced ha_keyread_time(rows) that takes into account finding
the next row and copying the key value to 'record'
(KEY_COPY_COST).
- Introduced ha_key_scan_time() for calculating an index scan over
all rows.
- Added IDX_LOOKUP_COST to keyread_time() as a startup cost.
- Added index_only_fetch_cost() as a convenience function to
OPT_RANGE.
- keyread_time() cost is slightly reduced to prefer shorter keys.
(main.index_merge_myisam)
- All of the above caused some index_merge combinations to be
rejected because of cost (main.index_intersect). In some cases
'ref' where replaced with index_merge because of the low
cost calculation of get_sweep_read_cost().
- Some index usage moved from PRIMARY to a covering index.
(main.subselect_innodb)
- Changed cost calculation of filter to take KEY_LOOKUP_COST and
TIME_FOR_COMPARE into account. See sql_select.cc::apply_filter().
filter parameters and costs are now written to optimizer_trace.
- Don't use matchings_records_in_range() to try to estimate the number
of filtered rows for ranges. The reason is that we want to ensure
that 'range' is calculated similar to 'ref'. There is also more work
needed to calculate the selectivity when using ranges and ranges and
filtering. This causes filtering column in EXPLAIN EXTENDED to be
100.00 for some cases where range cannot use filtering.
(main.rowid_filter)
- Introduced ha_scan_time() that takes into account the CPU cost of
finding the next row and copying the row from the engine to
'record'. This causes costs of table scan to slightly increase and
some test to changed their plan from ALL to RANGE or ALL to ref.
(innodb.innodb_mysql, main.select_pkeycache)
In a few cases where scan time of very small tables have lower cost
than a ref or range, things changed from ref/range to ALL.
(main.myisam, main.func_group, main.limit_rows_examined,
main.subselect2)
- Introduced ha_scan_and_compare_time() which is like ha_scan_time()
but also adds the cost of the where clause (TIME_FOR_COMPARE).
- Added small cost for creating temporary table for
materialization. This causes some very small tables to use scan
instead of materialization.
- Added checking of the WHERE clause (TIME_FOR_COMPARE) of the
accepted rows to ROR costs in get_best_ror_intersect()
- Removed '- 0.001' from 'join->best_read' and optimize_straight_join()
to ensure that the 'Last_query_cost' status variable contains the
same value as the one that was calculated by the optimizer.
- Take avg_io_cost() into account in handler::keyread_time() and
handler::read_time(). This should have no effect as it's 1.0 by
default, except for heap that overrides these functions.
- Some 'ref_or_null' accesses changed to 'range' because of cost
adjustments (main.order_by)
- Added scan type "scan_with_join_cache" for optimizer_trace. This is
just to show in the trace what kind of scan was used.
- When using 'scan_with_join_cache' take into account number of
preceding tables (as have to restore all fields for all previous
table combination when checking the where clause)
The new cost added is:
(row_combinations * ROW_COPY_COST * number_of_cached_tables).
This increases the cost of join buffering in proportion of the
number of tables in the join buffer. One effect is that full scans
are now done earlier as the cost is then smaller.
(main.join_outer_innodb, main.greedy_optimizer)
- Removed the usage of 'worst_seeks' in cost_for_index_read as it
caused wrong plans to be created; It prefered JT_EQ_REF even if it
would be much more expensive than a full table scan. A related
issue was that worst_seeks only applied to full lookup, not to
clustered or index only lookups, which is not consistent. This
caused some plans to use index scan instead of eq_ref (main.union)
- Changed federated block size from 4096 to 1500, which is the
typical size of an IO packet.
- Added costs for reading rows to Federated. Needed as there is no
caching of rows in the federated engine.
- Added ha_innobase::rnd_pos_time() cost function.
- A lot of extra things added to optimizer trace
- More costs, especially for materialization and index_merge.
- Make lables more uniform
- Fixed a lot of minor bugs
- Added 'trace_started()' around a lot of trace blocks.
- When calculating ORDER BY with LIMIT cost for using an index
the cost did not take into account the number of row retrivals
that has to be done or the cost of comparing the rows with the
WHERE clause. The cost calculated would be just a fraction of
the real cost. Now we calculate the cost as we do for ranges
and 'ref'.
- 'Using index for group-by' is used a bit more than before as
now take into account the WHERE clause cost when comparing
with 'ref' and prefer the method with fewer row combinations.
(main.group_min_max).
Bugs fixed:
- Fixed that we don't calculate TIME_FOR_COMPARE twice for some plans,
like in optimize_straight_join() and greedy_search()
- Fixed bug in save_explain_data where we could test for the wrong
index when displaying 'Using index'. This caused some old plans to
show 'Using index'. (main.subselect_innodb, main.subselect2)
- Fixed bug in get_best_ror_intersect() where 'min_cost' was not
updated, and the cost we compared with was not the one that was
used.
- Fixed very wrong cost calculation for priority queues in
check_if_pq_applicable(). (main.order_by now correctly uses priority
queue)
- When calculating cost of EQ_REF or REF, we added the cost of
comparing the WHERE clause with the found rows, not all row
combinations. This made ref and eq_ref to be regarded way to cheap
compared to other access methods.
- FORCE INDEX cost calculation didn't take into account clustered or
covered indexes.
- JT_EQ_REF cost was estimated as avg_io_cost(), which is half the
cost of a JT_REF key. This may be true for InnoDB primary key, but
not for other unique keys or other engines. Now we use handler
function to calculate the cost, which allows us to handle
consistently clustered, covered keys and not covered keys.
- ha_start_keyread() didn't call extra_opt() if keyread was already
enabled but still changed the 'keyread' variable (which is wrong).
Fixed by not doing anything if keyread is already enabled.
- multi_range_read_info_cost() didn't take into account io_cost when
calculating the cost of ranges.
- fix_semijoin_strategies_for_picked_join_order() used the wrong
record_count when calling best_access_path() for SJ_OPT_FIRST_MATCH
and SJ_OPT_LOOSE_SCAN.
- Hash joins didn't provide correct best_cost to the upper level, which
means that the cost for hash_joins more expensive than calculated
in best_access_path (a difference of 10x * TIME_OF_COMPARE).
This is fixed in the new code thanks to that we now include
TIME_OF_COMPARE cost in 'read_time'.
Other things:
- Added some 'if (thd->trace_started())' to speed up code
- Removed not used function Cost_estimate::is_zero()
- Simplified testing of HA_POS_ERROR in get_best_ror_intersect().
(No cost changes)
- Moved ha_start_keyread() from join_read_const_table() to join_read_const()
to enable keyread for all types of JT_CONST tables.
- Made a few very short functions inline in handler.h
Notes:
- In main.rowid_filter the join order of order and lineitem is swapped.
This is because the cost of doing a range fetch of lineitem(98 rows) is
almost as big as the whole join of order,lineitem. The filtering will
also ensure that we only have to do very small key fetches of the rows
in lineitem.
- main.index_merge_myisam had a few changes where we are now using
less keys for index_merge. This is because index scans are now more
expensive than before.
- handler->optimizer_cache_cost is updated in ha_external_lock().
This ensures that it is up to date per statements.
Not an optimal solution (for locked tables), but should be ok for now.
- 'DELETE FROM t1 WHERE t1.a > 0 ORDER BY t1.a' does not take cost of
filesort into consideration when table scan is chosen.
(main.myisam_explain_non_select_all)
- perfschema.table_aggregate_global_* has changed because an update
on a table with 1 row will now use table scan instead of key lookup.
TODO in upcomming commits:
- Fix selectivity calculation for ranges with and without filtering and
when there is a ref access but scan is chosen.
For this we have to store the lowest known value for
'accepted_records' in the OPT_RANGE structure.
- Change that records_read does not include filtered rows.
- test_if_cheaper_ordering() needs to be updated to properly calculate
costs. This will fix tests like main.order_by_innodb,
main.single_delete_update
- Extend get_range_limit_read_cost() to take into considering
cost_for_index_read() if there where no quick keys. This will reduce
the computed cost for ORDER BY with LIMIT in some cases.
(main.innodb_ext_key)
- Fix that we take into account selectivity when counting the number
of rows we have to read when considering using a index table scan to
resolve ORDER BY.
- Add new calculation for rnd_pos_time() where we take into account the
benefit of reading multiple rows from the same page.
2021-11-01 11:34:24 +01:00
|
|
|
records= (ha_rows) (spl_opt_info->unsplit_card * spl_plan->split_sel);
|
2021-03-28 23:33:27 +02:00
|
|
|
|
2022-01-20 14:49:01 +01:00
|
|
|
if (unlikely(thd->trace_started()))
|
|
|
|
{
|
|
|
|
Json_writer_object trace(thd, "lateral_derived");
|
|
|
|
trace.
|
|
|
|
add("startup_cost", startup_cost).
|
|
|
|
add("splitting_cost", spl_plan->cost).
|
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 12:05:23 +02:00
|
|
|
add("rows", records);
|
2022-01-20 14:49:01 +01:00
|
|
|
}
|
2017-12-30 21:29:09 +01:00
|
|
|
}
|
|
|
|
else
|
Update row and key fetch cost models to take into account data copy costs
Before this patch, when calculating the cost of fetching and using a
row/key from the engine, we took into account the cost of finding a
row or key from the engine, but did not consistently take into account
index only accessed, clustered key or covered keys for all access
paths.
The cost of the WHERE clause (TIME_FOR_COMPARE) was not consistently
considered in best_access_path(). TIME_FOR_COMPARE was used in
calculation in other places, like greedy_search(), but was in some
cases (like scans) done an a different number of rows than was
accessed.
The cost calculation of row and index scans didn't take into account
the number of rows that where accessed, only the number of accepted
rows.
When using a filter, the cost of index_only_reads and cost of
accessing and disregarding 'filtered rows' where not taken into
account, which made filters cost less than there actually where.
To remedy the above, the following key & row fetch related costs
has been added:
- The cost of fetching and using a row is now split into different costs:
- key + Row fetch cost (as before) but multiplied with the variable
'optimizer_cache_cost' (default to 0.5). This allows the user to
tell the optimizer the likehood of finding the key and row in the
engine cache.
- ROW_COPY_COST, The cost copying a row from the engine to the
sql layer or creating a row from the join_cache to the record
buffer. Mostly affects table scan costs.
- ROW_LOOKUP_COST, the cost of fetching a row by rowid.
- KEY_COPY_COST the cost of finding the next key and copying it from
the engine to the SQL layer. This is used when we calculate the cost
index only reads. It makes index scans more expensive than before if
they cover a lot of rows. (main.index_merge_myisam)
- KEY_LOOKUP_COST, the cost of finding the first key in a range.
This replaces the old define IDX_LOOKUP_COST, but with a higher cost.
- KEY_NEXT_FIND_COST, the cost of finding the next key (and rowid).
when doing a index scan and comparing the rowid to the filter.
Before this cost was assumed to be 0.
All of the above constants/variables are now tuned to be somewhat in
proportion of executing complexity to each other. There is tuning
need for these in the future, but that can wait until the above are
made user variables as that will make tuning much easier.
To make the usage of the above easy, there are new (not virtual)
cost calclation functions in handler:
- ha_read_time(), like read_time(), but take optimizer_cache_cost into
account.
- ha_read_and_copy_time(), like ha_read_time() but take into account
ROW_COPY_TIME
- ha_read_and_compare_time(), like ha_read_and_copy_time() but take
TIME_FOR_COMPARE into account.
- ha_rnd_pos_time(). Read row with row id, taking ROW_COPY_COST
into account. This is used with filesort where we don't need
to execute the WHERE clause again.
- ha_keyread_time(), like keyread_time() but take
optimizer_cache_cost into account.
- ha_keyread_and_copy_time(), like ha_keyread_time(), but add
KEY_COPY_COST.
- ha_key_scan_time(), like key_scan_time() but take
optimizer_cache_cost nto account.
- ha_key_scan_and_compare_time(), like ha_key_scan_time(), but add
KEY_COPY_COST & TIME_FOR_COMPARE.
I also added some setup costs for doing different types of scans and
creating temporary tables (on disk and in memory). This encourages
the optimizer to not use these for simple 'a few row' lookups if
there are adequate key lookup strategies.
- TABLE_SCAN_SETUP_COST, cost of starting a table scan.
- INDEX_SCAN_SETUP_COST, cost of starting an index scan.
- HEAP_TEMPTABLE_CREATE_COST, cost of creating in memory
temporary table.
- DISK_TEMPTABLE_CREATE_COST, cost of creating an on disk temporary
table.
When calculating cost of fetching ranges, we had a cost of
IDX_LOOKUP_COST (0.125) for doing a key div for a new range. This is
now replaced with 'io_cost * KEY_LOOKUP_COST (1.0) *
optimizer_cache_cost', which matches the cost we use for 'ref' and
other key lookups. The effect is that the cost is now a bit higher
when we have many ranges for a key.
Allmost all calculation with TIME_FOR_COMPARE is now done in
best_access_path(). 'JOIN::read_time' now includes the full
cost for finding the rows in the table.
In the result files, many of the changes are now again close to what
they where before the "Update cost for hash and cached joins" commit,
as that commit didn't fix the filter cost (too complex to do
everything in one commit).
The above changes showed a lot of a lot of inconsistencies in
optimizer cost calculation. The main objective with the other changes
was to do calculation as similar (and accurate) as possible and to make
different plans more comparable.
Detailed list of changes:
- Calculate index_only_cost consistently and correctly for all scan
and ref accesses. The row fetch_cost and index_only_cost now
takes into account clustered keys, covered keys and index
only accesses.
- cost_for_index_read now returns both full cost and index_only_cost
- Fixed cost calculation of get_sweep_read_cost() to match other
similar costs. This is bases on the assumption that data is more
often stored on SSD than a hard disk.
- Replaced constant 2.0 with new define TABLE_SCAN_SETUP_COST.
- Some scan cost estimates did not take into account
TIME_FOR_COMPARE. Now all scan costs takes this into
account. (main.show_explain)
- Added session variable optimizer_cache_hit_ratio (default 50%). By
adjusting this on can reduce or increase the cost of index or direct
record lookups. The effect of the default is that key lookups is now
a bit cheaper than before. See usage of 'optimizer_cache_cost' in
handler.h.
- JOIN_TAB::scan_time() did not take into account index only scans,
which produced a wrong cost when index scan was used. Changed
JOIN_TAB:::scan_time() to take into consideration clustered and
covered keys. The values are now cached and we only have to call
this function once. Other calls are changed to use the cached
values. Function renamed to JOIN_TAB::estimate_scan_time().
- Fixed that most index cost calculations are done the same way and
more close to 'range' calculations. The cost is now lower than
before for small data sets and higher for large data sets as we take
into account how many keys are read (main.opt_trace_selectivity,
main.limit_rows_examined).
- Ensured that index_scan_cost() ==
range(scan_of_all_rows_in_table_using_one_range) +
MULTI_RANGE_READ_INFO_CONST. One effect of this is that if there
is choice of doing a full index scan and a range-index scan over
almost the whole table then index scan will be preferred (no
range-read setup cost). (innodb.innodb, main.show_explain,
main.range)
- Fixed the EQ_REF and REF takes into account clustered and covered
keys. This changes some plans to use covered or clustered indexes
as these are much cheaper. (main.subselect_mat_cost,
main.state_tables_innodb, main.limit_rows_examined)
- Rowid filter setup cost and filter compare cost now takes into
account fetching and checking the rowid (KEY_NEXT_FIND_COST).
(main.partition_pruning heap.heap_btree main.log_state)
- Added KEY_NEXT_FIND_COST to
Range_rowid_filter_cost_info::lookup_cost to account of the time
to find and check the next key value against the container
- Introduced ha_keyread_time(rows) that takes into account finding
the next row and copying the key value to 'record'
(KEY_COPY_COST).
- Introduced ha_key_scan_time() for calculating an index scan over
all rows.
- Added IDX_LOOKUP_COST to keyread_time() as a startup cost.
- Added index_only_fetch_cost() as a convenience function to
OPT_RANGE.
- keyread_time() cost is slightly reduced to prefer shorter keys.
(main.index_merge_myisam)
- All of the above caused some index_merge combinations to be
rejected because of cost (main.index_intersect). In some cases
'ref' where replaced with index_merge because of the low
cost calculation of get_sweep_read_cost().
- Some index usage moved from PRIMARY to a covering index.
(main.subselect_innodb)
- Changed cost calculation of filter to take KEY_LOOKUP_COST and
TIME_FOR_COMPARE into account. See sql_select.cc::apply_filter().
filter parameters and costs are now written to optimizer_trace.
- Don't use matchings_records_in_range() to try to estimate the number
of filtered rows for ranges. The reason is that we want to ensure
that 'range' is calculated similar to 'ref'. There is also more work
needed to calculate the selectivity when using ranges and ranges and
filtering. This causes filtering column in EXPLAIN EXTENDED to be
100.00 for some cases where range cannot use filtering.
(main.rowid_filter)
- Introduced ha_scan_time() that takes into account the CPU cost of
finding the next row and copying the row from the engine to
'record'. This causes costs of table scan to slightly increase and
some test to changed their plan from ALL to RANGE or ALL to ref.
(innodb.innodb_mysql, main.select_pkeycache)
In a few cases where scan time of very small tables have lower cost
than a ref or range, things changed from ref/range to ALL.
(main.myisam, main.func_group, main.limit_rows_examined,
main.subselect2)
- Introduced ha_scan_and_compare_time() which is like ha_scan_time()
but also adds the cost of the where clause (TIME_FOR_COMPARE).
- Added small cost for creating temporary table for
materialization. This causes some very small tables to use scan
instead of materialization.
- Added checking of the WHERE clause (TIME_FOR_COMPARE) of the
accepted rows to ROR costs in get_best_ror_intersect()
- Removed '- 0.001' from 'join->best_read' and optimize_straight_join()
to ensure that the 'Last_query_cost' status variable contains the
same value as the one that was calculated by the optimizer.
- Take avg_io_cost() into account in handler::keyread_time() and
handler::read_time(). This should have no effect as it's 1.0 by
default, except for heap that overrides these functions.
- Some 'ref_or_null' accesses changed to 'range' because of cost
adjustments (main.order_by)
- Added scan type "scan_with_join_cache" for optimizer_trace. This is
just to show in the trace what kind of scan was used.
- When using 'scan_with_join_cache' take into account number of
preceding tables (as have to restore all fields for all previous
table combination when checking the where clause)
The new cost added is:
(row_combinations * ROW_COPY_COST * number_of_cached_tables).
This increases the cost of join buffering in proportion of the
number of tables in the join buffer. One effect is that full scans
are now done earlier as the cost is then smaller.
(main.join_outer_innodb, main.greedy_optimizer)
- Removed the usage of 'worst_seeks' in cost_for_index_read as it
caused wrong plans to be created; It prefered JT_EQ_REF even if it
would be much more expensive than a full table scan. A related
issue was that worst_seeks only applied to full lookup, not to
clustered or index only lookups, which is not consistent. This
caused some plans to use index scan instead of eq_ref (main.union)
- Changed federated block size from 4096 to 1500, which is the
typical size of an IO packet.
- Added costs for reading rows to Federated. Needed as there is no
caching of rows in the federated engine.
- Added ha_innobase::rnd_pos_time() cost function.
- A lot of extra things added to optimizer trace
- More costs, especially for materialization and index_merge.
- Make lables more uniform
- Fixed a lot of minor bugs
- Added 'trace_started()' around a lot of trace blocks.
- When calculating ORDER BY with LIMIT cost for using an index
the cost did not take into account the number of row retrivals
that has to be done or the cost of comparing the rows with the
WHERE clause. The cost calculated would be just a fraction of
the real cost. Now we calculate the cost as we do for ranges
and 'ref'.
- 'Using index for group-by' is used a bit more than before as
now take into account the WHERE clause cost when comparing
with 'ref' and prefer the method with fewer row combinations.
(main.group_min_max).
Bugs fixed:
- Fixed that we don't calculate TIME_FOR_COMPARE twice for some plans,
like in optimize_straight_join() and greedy_search()
- Fixed bug in save_explain_data where we could test for the wrong
index when displaying 'Using index'. This caused some old plans to
show 'Using index'. (main.subselect_innodb, main.subselect2)
- Fixed bug in get_best_ror_intersect() where 'min_cost' was not
updated, and the cost we compared with was not the one that was
used.
- Fixed very wrong cost calculation for priority queues in
check_if_pq_applicable(). (main.order_by now correctly uses priority
queue)
- When calculating cost of EQ_REF or REF, we added the cost of
comparing the WHERE clause with the found rows, not all row
combinations. This made ref and eq_ref to be regarded way to cheap
compared to other access methods.
- FORCE INDEX cost calculation didn't take into account clustered or
covered indexes.
- JT_EQ_REF cost was estimated as avg_io_cost(), which is half the
cost of a JT_REF key. This may be true for InnoDB primary key, but
not for other unique keys or other engines. Now we use handler
function to calculate the cost, which allows us to handle
consistently clustered, covered keys and not covered keys.
- ha_start_keyread() didn't call extra_opt() if keyread was already
enabled but still changed the 'keyread' variable (which is wrong).
Fixed by not doing anything if keyread is already enabled.
- multi_range_read_info_cost() didn't take into account io_cost when
calculating the cost of ranges.
- fix_semijoin_strategies_for_picked_join_order() used the wrong
record_count when calling best_access_path() for SJ_OPT_FIRST_MATCH
and SJ_OPT_LOOSE_SCAN.
- Hash joins didn't provide correct best_cost to the upper level, which
means that the cost for hash_joins more expensive than calculated
in best_access_path (a difference of 10x * TIME_OF_COMPARE).
This is fixed in the new code thanks to that we now include
TIME_OF_COMPARE cost in 'read_time'.
Other things:
- Added some 'if (thd->trace_started())' to speed up code
- Removed not used function Cost_estimate::is_zero()
- Simplified testing of HA_POS_ERROR in get_best_ror_intersect().
(No cost changes)
- Moved ha_start_keyread() from join_read_const_table() to join_read_const()
to enable keyread for all types of JT_CONST tables.
- Made a few very short functions inline in handler.h
Notes:
- In main.rowid_filter the join order of order and lineitem is swapped.
This is because the cost of doing a range fetch of lineitem(98 rows) is
almost as big as the whole join of order,lineitem. The filtering will
also ensure that we only have to do very small key fetches of the rows
in lineitem.
- main.index_merge_myisam had a few changes where we are now using
less keys for index_merge. This is because index scans are now more
expensive than before.
- handler->optimizer_cache_cost is updated in ha_external_lock().
This ensures that it is up to date per statements.
Not an optimal solution (for locked tables), but should be ok for now.
- 'DELETE FROM t1 WHERE t1.a > 0 ORDER BY t1.a' does not take cost of
filesort into consideration when table scan is chosen.
(main.myisam_explain_non_select_all)
- perfschema.table_aggregate_global_* has changed because an update
on a table with 1 row will now use table scan instead of key lookup.
TODO in upcomming commits:
- Fix selectivity calculation for ranges with and without filtering and
when there is a ref access but scan is chosen.
For this we have to store the lowest known value for
'accepted_records' in the OPT_RANGE structure.
- Change that records_read does not include filtered rows.
- test_if_cheaper_ordering() needs to be updated to properly calculate
costs. This will fix tests like main.order_by_innodb,
main.single_delete_update
- Extend get_range_limit_read_cost() to take into considering
cost_for_index_read() if there where no quick keys. This will reduce
the computed cost for ORDER BY with LIMIT in some cases.
(main.innodb_ext_key)
- Fix that we take into account selectivity when counting the number
of rows we have to read when considering using a index table scan to
resolve ORDER BY.
- Add new calculation for rnd_pos_time() where we take into account the
benefit of reading multiple rows from the same page.
2021-11-01 11:34:24 +01:00
|
|
|
{
|
|
|
|
/* Restore original values */
|
|
|
|
startup_cost= spl_opt_info->unsplit_cost;
|
|
|
|
records= (ha_rows) spl_opt_info->unsplit_card;
|
|
|
|
}
|
2017-12-30 21:29:09 +01:00
|
|
|
return spl_plan;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief
|
|
|
|
Inject equalities for splitting used by the materialization join
|
|
|
|
|
|
|
|
@param
|
2022-01-25 08:14:46 +01:00
|
|
|
excluded_tables used to filter out the equalities that cannot
|
2017-12-30 21:29:09 +01:00
|
|
|
be pushed.
|
|
|
|
|
|
|
|
@details
|
2022-01-25 08:14:46 +01:00
|
|
|
This function injects equalities pushed into a derived table T for which
|
|
|
|
the split optimization has been chosen by the optimizer. The function
|
|
|
|
is called by JOIN::inject_splitting_cond_for_all_tables_with_split_op().
|
|
|
|
All equalities usable for splitting T whose right parts do not depend on
|
|
|
|
any of the 'excluded_tables' can be pushed into the where clause of the
|
|
|
|
derived table T.
|
2017-12-30 21:29:09 +01:00
|
|
|
The function also marks the select that specifies T as
|
|
|
|
UNCACHEABLE_DEPENDENT_INJECTED.
|
|
|
|
|
|
|
|
@retval
|
|
|
|
false on success
|
|
|
|
true on failure
|
|
|
|
*/
|
|
|
|
|
2022-01-25 08:14:46 +01:00
|
|
|
bool JOIN::inject_best_splitting_cond(table_map excluded_tables)
|
2017-12-30 21:29:09 +01:00
|
|
|
{
|
|
|
|
Item *inj_cond= 0;
|
2021-03-24 04:54:54 +01:00
|
|
|
List<Item> *inj_cond_list= &spl_opt_info->inj_cond_list;
|
2017-12-30 21:29:09 +01:00
|
|
|
List_iterator<KEY_FIELD> li(spl_opt_info->added_key_fields);
|
|
|
|
KEY_FIELD *added_key_field;
|
|
|
|
while ((added_key_field= li++))
|
|
|
|
{
|
2022-01-25 08:14:46 +01:00
|
|
|
if (excluded_tables & added_key_field->val->used_tables())
|
2017-12-30 21:29:09 +01:00
|
|
|
continue;
|
2021-03-24 04:54:54 +01:00
|
|
|
if (inj_cond_list->push_back(added_key_field->cond, thd->mem_root))
|
2017-12-30 21:29:09 +01:00
|
|
|
return true;
|
|
|
|
}
|
2021-03-24 04:54:54 +01:00
|
|
|
DBUG_ASSERT(inj_cond_list->elements);
|
|
|
|
switch (inj_cond_list->elements) {
|
2017-12-30 21:29:09 +01:00
|
|
|
case 1:
|
2021-03-24 04:54:54 +01:00
|
|
|
inj_cond= inj_cond_list->head(); break;
|
2017-12-30 21:29:09 +01:00
|
|
|
default:
|
2021-03-24 04:54:54 +01:00
|
|
|
inj_cond= new (thd->mem_root) Item_cond_and(thd, *inj_cond_list);
|
2017-12-30 21:29:09 +01:00
|
|
|
if (!inj_cond)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (inj_cond)
|
|
|
|
inj_cond->fix_fields(thd,0);
|
|
|
|
|
2022-01-14 07:46:59 +01:00
|
|
|
if (inject_cond_into_where(inj_cond->copy_andor_structure(thd)))
|
2017-12-30 21:29:09 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
select_lex->uncacheable|= UNCACHEABLE_DEPENDENT_INJECTED;
|
|
|
|
st_select_lex_unit *unit= select_lex->master_unit();
|
|
|
|
unit->uncacheable|= UNCACHEABLE_DEPENDENT_INJECTED;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-24 04:54:54 +01:00
|
|
|
/**
|
|
|
|
@brief
|
|
|
|
Test if equality is injected for split optimization
|
|
|
|
|
|
|
|
@param
|
|
|
|
eq_item equality to to test
|
|
|
|
|
|
|
|
@retval
|
|
|
|
true eq_item is equality injected for split optimization
|
|
|
|
false otherwise
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool is_eq_cond_injected_for_split_opt(Item_func_eq *eq_item)
|
|
|
|
{
|
|
|
|
Item *left_item= eq_item->arguments()[0]->real_item();
|
|
|
|
if (left_item->type() != Item::FIELD_ITEM)
|
|
|
|
return false;
|
|
|
|
Field *field= ((Item_field *) left_item)->field;
|
|
|
|
if (!field->table->reginfo.join_tab)
|
|
|
|
return false;
|
|
|
|
JOIN *join= field->table->reginfo.join_tab->join;
|
|
|
|
if (!join->spl_opt_info)
|
|
|
|
return false;
|
|
|
|
List_iterator_fast<Item> li(join->spl_opt_info->inj_cond_list);
|
|
|
|
Item *item;
|
|
|
|
while ((item= li++))
|
|
|
|
{
|
|
|
|
if (item == eq_item)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-30 21:29:09 +01:00
|
|
|
/**
|
|
|
|
@brief
|
|
|
|
Fix the splitting chosen for a splittable table in the final query plan
|
|
|
|
|
|
|
|
@param
|
|
|
|
spl_plan info on the splitting plan chosen for the splittable table T
|
|
|
|
remaining_tables the table T is joined just before these tables
|
2019-03-05 08:10:30 +01:00
|
|
|
is_const_table the table T is a constant table
|
2017-12-30 21:29:09 +01:00
|
|
|
|
|
|
|
@details
|
|
|
|
If in the final query plan the optimizer has chosen a splitting plan
|
|
|
|
then the function sets this plan as the final execution plan to
|
|
|
|
materialized the table T. Otherwise the plan that does not use
|
|
|
|
splitting is set for the materialization.
|
|
|
|
|
|
|
|
@retval
|
|
|
|
false on success
|
|
|
|
true on failure
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool JOIN_TAB::fix_splitting(SplM_plan_info *spl_plan,
|
2019-03-05 08:10:30 +01:00
|
|
|
table_map remaining_tables,
|
|
|
|
bool is_const_table)
|
2017-12-30 21:29:09 +01:00
|
|
|
{
|
|
|
|
SplM_opt_info *spl_opt_info= table->spl_opt_info;
|
|
|
|
DBUG_ASSERT(table->spl_opt_info != 0);
|
|
|
|
JOIN *md_join= spl_opt_info->join;
|
2019-03-05 08:10:30 +01:00
|
|
|
if (spl_plan && !is_const_table)
|
2017-12-30 21:29:09 +01:00
|
|
|
{
|
|
|
|
memcpy((char *) md_join->best_positions,
|
|
|
|
(char *) spl_plan->best_positions,
|
|
|
|
sizeof(POSITION) * md_join->table_count);
|
|
|
|
/*
|
|
|
|
This is called for a proper work of JOIN::get_best_combination()
|
|
|
|
called for the join that materializes T
|
|
|
|
*/
|
|
|
|
reset_validity_vars_for_keyuses(spl_plan->keyuse_ext_start,
|
|
|
|
spl_plan->table,
|
|
|
|
spl_plan->key,
|
|
|
|
remaining_tables,
|
|
|
|
true);
|
|
|
|
}
|
2019-03-05 08:10:30 +01:00
|
|
|
else if (md_join->save_qep)
|
2017-12-30 21:29:09 +01:00
|
|
|
{
|
|
|
|
md_join->restore_query_plan(md_join->save_qep);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief
|
|
|
|
Fix the splittings chosen splittable tables in the final query plan
|
|
|
|
|
|
|
|
@details
|
|
|
|
The function calls JOIN_TAB::fix_splittins for all potentially
|
|
|
|
splittable tables in this join to set all final materialization
|
|
|
|
plans chosen for these tables.
|
|
|
|
|
|
|
|
@retval
|
|
|
|
false on success
|
|
|
|
true on failure
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool JOIN::fix_all_splittings_in_plan()
|
|
|
|
{
|
|
|
|
table_map prev_tables= 0;
|
2020-12-14 12:21:30 +01:00
|
|
|
table_map all_tables= (table_map(1) << table_count) - 1;
|
2018-01-30 08:51:04 +01:00
|
|
|
for (uint tablenr= 0; tablenr < table_count; tablenr++)
|
2017-12-30 21:29:09 +01:00
|
|
|
{
|
|
|
|
POSITION *cur_pos= &best_positions[tablenr];
|
|
|
|
JOIN_TAB *tab= cur_pos->table;
|
2019-03-05 08:10:30 +01:00
|
|
|
if (tab->table->is_splittable())
|
2017-12-30 21:29:09 +01:00
|
|
|
{
|
|
|
|
SplM_plan_info *spl_plan= cur_pos->spl_plan;
|
2022-01-25 08:14:46 +01:00
|
|
|
if (tab->fix_splitting(spl_plan,
|
|
|
|
all_tables & ~prev_tables,
|
2019-03-05 08:10:30 +01:00
|
|
|
tablenr < const_tables ))
|
2017-12-30 21:29:09 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
prev_tables|= tab->table->map;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2022-01-25 08:14:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief
|
|
|
|
Inject splitting conditions into WHERE of split derived
|
|
|
|
|
|
|
|
@details
|
|
|
|
The function calls JOIN_TAB::inject_best_splitting_cond() for each
|
|
|
|
materialized derived table T used in this join for which the split
|
|
|
|
optimization has been chosen by the optimizer. It is done in order to
|
|
|
|
inject equalities pushed into the where clause of the specification
|
|
|
|
of T that would be helpful to employ the splitting technique.
|
|
|
|
|
|
|
|
@retval
|
|
|
|
false on success
|
|
|
|
true on failure
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool JOIN::inject_splitting_cond_for_all_tables_with_split_opt()
|
|
|
|
{
|
|
|
|
table_map prev_tables= 0;
|
|
|
|
table_map all_tables= (table_map(1) << table_count) - 1;
|
|
|
|
for (uint tablenr= 0; tablenr < table_count; tablenr++)
|
|
|
|
{
|
|
|
|
POSITION *cur_pos= &best_positions[tablenr];
|
|
|
|
JOIN_TAB *tab= cur_pos->table;
|
|
|
|
prev_tables|= tab->table->map;
|
|
|
|
if (!(tab->table->is_splittable() && cur_pos->spl_plan))
|
|
|
|
continue;
|
|
|
|
SplM_opt_info *spl_opt_info= tab->table->spl_opt_info;
|
|
|
|
JOIN *join= spl_opt_info->join;
|
|
|
|
/*
|
|
|
|
Currently the equalities referencing columns of SJM tables with
|
|
|
|
look-up access cannot be pushed into materialized derived.
|
|
|
|
*/
|
|
|
|
if (join->inject_best_splitting_cond((all_tables & ~prev_tables) |
|
|
|
|
sjm_lookup_tables))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|