mirror of
https://github.com/MariaDB/server.git
synced 2025-01-31 11:01:52 +01:00
dc2f0d138d
calculate_cond_selectivity_for_table() is largely rewritten: - Process keys in the order of rows found, smaller ranges first. If two ranges has equal number of rows, use the one with more key parts. This helps us to mark more used fields to not be used for further selectivity calculations. See cmp_quick_ranges(). - Ignore keys with fields that where used by previous keys - Don't use rec_per_key[] to calculate selectivity for smaller secondary key parts. This does not work as rec_per_key[] value is calculated in the context of the previous key parts, not for the key part itself. The one exception is if the previous key parts are all constants. Other things: - Ensure that select->cond_selectivity is always between 0 and 1. - Ensure that select->opt_range_condition_rows is never updated to a higher value. It is initially set to the number of rows in table. - We now store in table->opt_range_condition_rows the lowest number of rows that any row-read-method has found so far. Before it was only done for QUICK_SELECT_I::QS_TYPE_ROR_UNION and QUICK_SELECT_I::QS_TYPE_INDEX_MERGE. Now it is done for a lot more methods. See calculate_cond_selectivity_for_table() for details. - Calculate and use selectivity for the first key part of a multiple key part if the first key part is a constant. WHERE key1_part1=5 and key2_part1=5. IF key1 is used, then we can still use selectivity for key2 Changes in test results: - 'filtered' is slightly changed, usually to something slightly smaller. - A few cases where for group by queries the table order changed. This was because the number of resulting rows from a group by query with MIN/MAX is now set to be smaller. - A few index was changed as we now prefer index with more key parts if the number of resulting rows is the same.
52 lines
2.8 KiB
Text
52 lines
2.8 KiB
Text
--source include/have_sequence.inc
|
|
--source include/not_embedded.inc
|
|
|
|
#
|
|
# Test changes in calculate_cond_selectivity_for_table()
|
|
#
|
|
create or replace table t1 (a int, b int, c int, key(a,c), key(b,c), key (c,b)) engine=aria;
|
|
insert into t1 select seq/100+1, mod(seq,10), mod(seq,15) from seq_1_to_10000;
|
|
insert into t1 select seq/100+1, mod(seq,10), 10 from seq_1_to_1000;
|
|
optimize table t1;
|
|
|
|
select count(*) from t1 where a=2;
|
|
select count(*) from t1 where b=5;
|
|
select count(*) from t1 where c=5;
|
|
select count(*) from t1 where c=10;
|
|
select count(*) from t1 where a=2 and b=5;
|
|
select count(*) from t1 where c=10 and b=5;
|
|
select count(*) from t1 where c=5 and b=5;
|
|
|
|
set optimizer_trace="enabled=on";
|
|
select count(*) from t1 where a=2 and b=5 and c=10;
|
|
select JSON_DETAILED(JSON_EXTRACT(trace, '$**.considered_execution_plans')) from INFORMATION_SCHEMA.OPTIMIZER_TRACE;
|
|
select JSON_DETAILED(JSON_EXTRACT(trace, '$**.selectivity_for_indexes')) from INFORMATION_SCHEMA.OPTIMIZER_TRACE;
|
|
|
|
select count(*) from t1 where a=2 and b=5 and c=5;
|
|
select JSON_DETAILED(JSON_EXTRACT(trace, '$**.considered_execution_plans')) from INFORMATION_SCHEMA.OPTIMIZER_TRACE;
|
|
select JSON_DETAILED(JSON_EXTRACT(trace, '$**.selectivity_for_indexes')) from INFORMATION_SCHEMA.OPTIMIZER_TRACE;
|
|
|
|
--echo # Ensure that we only use selectivity from non used index for simple cases
|
|
|
|
|
|
select count(*) from t1 where (a=2 and b= 5);
|
|
select JSON_DETAILED(JSON_EXTRACT(trace, '$**.selectivity_for_indexes')) from INFORMATION_SCHEMA.OPTIMIZER_TRACE;
|
|
|
|
--echo # All of the following should have selectivity=1 for index 'b'
|
|
select count(*) from t1 where (a=2 and b between 0 and 100);
|
|
select JSON_DETAILED(JSON_EXTRACT(trace, '$**.selectivity_for_indexes')) from INFORMATION_SCHEMA.OPTIMIZER_TRACE;
|
|
select count(*) from t1 where (a in (2,3) and b between 0 and 100);
|
|
select JSON_DETAILED(JSON_EXTRACT(trace, '$**.selectivity_for_indexes')) from INFORMATION_SCHEMA.OPTIMIZER_TRACE;
|
|
select count(*) from t1 where (a>2 and b between 0 and 100);
|
|
select JSON_DETAILED(JSON_EXTRACT(trace, '$**.selectivity_for_indexes')) from INFORMATION_SCHEMA.OPTIMIZER_TRACE;
|
|
select count(*) from t1 where (a>=2 and b between 0 and 100);
|
|
select JSON_DETAILED(JSON_EXTRACT(trace, '$**.selectivity_for_indexes')) from INFORMATION_SCHEMA.OPTIMIZER_TRACE;
|
|
select count(*) from t1 where (a<=2 and b between 0 and 100);
|
|
select JSON_DETAILED(JSON_EXTRACT(trace, '$**.selectivity_for_indexes')) from INFORMATION_SCHEMA.OPTIMIZER_TRACE;
|
|
select count(*) from t1 where (a<2 and b between 0 and 100);
|
|
select JSON_DETAILED(JSON_EXTRACT(trace, '$**.selectivity_for_indexes')) from INFORMATION_SCHEMA.OPTIMIZER_TRACE;
|
|
select count(*) from t1 where (a between 2 and 3 and b between 0 and 100);
|
|
select JSON_DETAILED(JSON_EXTRACT(trace, '$**.selectivity_for_indexes')) from INFORMATION_SCHEMA.OPTIMIZER_TRACE;
|
|
|
|
drop table t1;
|
|
set optimizer_trace='enabled=off';
|