mirror of
https://github.com/MariaDB/server.git
synced 2025-07-29 05:21:33 +03:00
MDEV-34894: Poor query plan, because range estimates are not reused for ref(const)
(Variant 4, with @@optimizer_adjust_secondary_key_costs, reuse in two places, and conditions are replaced with equivalent simpler forms in two more) In best_access_path(), ReuseRangeEstimateForRef-3, the check for whether "all used key_part_i used key_part_i=const" was incorrect: it may produced a "NO" answer for cases when we had: key_part1= const // some key parts are usable key_part2= value_not_in_join_prefix //present but unusable key_part3= non_const_value // unusable due to gap in key parts. This caused the optimizer to fail to apply ReuseRangeEstimateForRef heuristics. The consequence is poor query plan choice when the index in question has very skewed data distribution. The fix is enabled if its @@optimizer_adjust_secondary_key_costs flag is set.
This commit is contained in:
@ -1904,3 +1904,81 @@ SELECT * FROM
|
||||
SET OPTIMIZER_USE_CONDITION_SELECTIVITY=@tmp;
|
||||
DROP TABLE t1,t2;
|
||||
--echo # End of 10.6 tests
|
||||
|
||||
--source include/have_sequence.inc
|
||||
|
||||
--echo #
|
||||
--echo # MDEV-34894: Poor query plan, because range estimates are not reused for ref(const)
|
||||
--echo #
|
||||
create table t0 (
|
||||
a int,
|
||||
b int,
|
||||
dummy int
|
||||
);
|
||||
insert into t0 select seq,seq,seq from seq_1_to_10;
|
||||
|
||||
create table t1 (
|
||||
pk1 int,
|
||||
pk2 int,
|
||||
pk3 int,
|
||||
key1 int,
|
||||
key(key1),
|
||||
filler char(100),
|
||||
primary key(pk1,pk2,pk3)
|
||||
);
|
||||
|
||||
insert into t1
|
||||
select
|
||||
seq, seq, seq,
|
||||
FLOOR(seq/2),
|
||||
'filler-data'
|
||||
from seq_1_to_10000;
|
||||
analyze table t1;
|
||||
|
||||
update t1 set pk1=1 where pk1 between 1 and 200;
|
||||
|
||||
explain select * from t1 where pk1=1;
|
||||
|
||||
explain select * from t0,t1 where t1.pk1=t0.a;
|
||||
|
||||
create table t2 (
|
||||
col int
|
||||
);
|
||||
insert into t2 select seq from seq_1_to_10000;
|
||||
|
||||
set optimizer_adjust_secondary_key_costs='fix_reuse_range_for_ref';
|
||||
--echo # This must use this good query plan:
|
||||
--echo # t0 - ALL
|
||||
--echo # t1 - ref, key=key1, not PRIMARY as pk1=1 is true for 20% of all rows
|
||||
--echo # t2 - ALL
|
||||
explain select * from t0, t1, t2
|
||||
where
|
||||
t1.pk1=1 and t1.pk2=t2.col and t1.pk3=t0.dummy and
|
||||
t1.key1=t0.b;
|
||||
|
||||
set optimizer_adjust_secondary_key_costs='';
|
||||
--echo # Bad query:
|
||||
--echo # t0 - ALL
|
||||
--echo # t1 - ref, key=PRIMARY
|
||||
--echo # t2 - ALL
|
||||
explain select * from t0, t1, t2
|
||||
where
|
||||
t1.pk1=1 and t1.pk2=t2.col and t1.pk3=t0.dummy and
|
||||
t1.key1=t0.b;
|
||||
|
||||
drop table t0,t1,t2;
|
||||
|
||||
set @@optimizer_adjust_secondary_key_costs="fix_reuse_range_for_ref";
|
||||
CREATE OR REPLACE TABLE t1 (a INT NOT NULL, b INT NOT NULL, c INT, key(a,b,c)) ENGINE=Aria;
|
||||
INSERT INTO t1 select seq/10,mod(seq,2),seq from seq_1_to_1000;
|
||||
update t1 set a=10 WHERE c < 100;
|
||||
update t1 set a=12 WHERE a=11;
|
||||
insert into t1 values (11,1,11), (11,2,11);
|
||||
create or replace table t2 select seq from seq_1_to_10;
|
||||
|
||||
explain select count(*) from t1, t2 as seq where a=10 and b=seq.seq;
|
||||
# This will execute code in ReuseRangeEstimateForRef-4
|
||||
explain select count(*) from t1, t2 as seq where a=11 and b=seq.seq;
|
||||
drop table t1,t2;
|
||||
|
||||
set @@optimizer_adjust_secondary_key_costs=default;
|
||||
|
Reference in New Issue
Block a user