mariadb/mysql-test/include/check_qep.inc
Monty 515b9ad05a Added EQ_REF chaining to the greedy_optimizer
MDEV-28073 Slow query performance in MariaDB when using many table

The idea is to prefer and chain EQ_REF tables (tables that uses an
unique key to find a row) when searching for the best table combination.
This significantly reduces row combinations that has to be examined.
This is optimization is enabled when setting optimizer_prune_level=2
(which is now default).

Implementation:
- optimizer_prune_level has a new level, 2, which enables EQ_REF
  optimization in addition to the pruning done by level 1.
  Level 2 is now default.
- Added JOIN::eq_ref_tables that contains bits of tables that could use
  potentially use EQ_REF access in the query.  This is calculated
  in sort_and_filter_keyuse()

Under optimizer_prune_level=2:
- When the greedy_optimizer notices that the preceding table was an
  EQ_REF table, it tries to add an EQ_REF table next. If an EQ_REF
  table exists, only this one will be considered at this level.
  We also collect all EQ_REF tables chained by the next levels and these
  are ignored on the starting level as we have already examined these.
  If no EQ_REF table exists, we continue as normal.

This optimization speeds up the greedy_optimizer combination test with
~25%

Other things:
- I ported the changes in MySQL 5.7 to greedy_optimizer.test to MariaDB
  to be able to ensure we can handle all cases that MySQL can do.
- I have run all tests with --mysqld=--optimizer_prune_level=1 to verify that
  there where no test changes.
2022-07-26 22:27:29 +07:00

57 lines
1.6 KiB
PHP

# include/check_qep.inc
#
# SUMMARY
#
# Designed to be used together with include/expect_qep.inc
#
# $query should be assigned a select statement using
# straight_join to force the tables to be joined in most
# optimal order.
#
# expect_qep.inc will then store the estimated 'Last_query_cost'
# and total # 'Handler_read%' for this straight_joined query.
#
# We should then assign a non-straight_join'ed version of
# the same query to $query and execute it using
# 'include/check_qep.inc'. Its estimated cost and
# #handler_reads will then be verified against the
# previous straight_joined query.
#
# USAGE
#
# let $query= <select straight_join optimal statement>;
# --source include/expect_qep.inc
# let $query= <select statement>;
# --source include/check_qep.inc
#
# EXAMPLE
# t/greedy_optimizer.test
#
flush status;
eval EXPLAIN $query;
eval $query;
let $cost=
query_get_value(SHOW STATUS LIKE 'Last_query_cost', Value, 1);
--disable_warnings
let $reads=
`select sum(variable_value)
from information_schema.session_status
where VARIABLE_NAME like 'Handler_read%'`;
--enable_warnings
#echo Cost: $cost, Handler_reads: $reads;
if ($cost != $best_cost)
{ echo ### FAILED: Query_cost: $cost, expected: $best_cost ###;
}
# Difference in handler reads are ok as tables in MariaDB are sorted according
# to order in the query and the tables in greedy_optimizer.inc has reference to
# rows that does not exists, so different table orders will do different
# number of reads
if ($reads != $best_reads)
{ echo ### NOTE: Handler_reads: $reads, expected: $best_reads ###;
}