MDEV-9959: A serious MariaDB server performance bug
If a derived table has SELECT DISTINCT, provide index statistics for it so that the join optimizer in the
upper select knows that ref access to the table will produce one row.
3 UNION t2 ALL NULL NULL NULL NULL 6 6.00 100.00 100.00
NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL 6.00 NULL NULL
select * from t1 , ( (select t2.a from t2 order by c) union (select t2.a from t2 order by c))q where t1.a=q.a;
a a
1 1
2 2
3 3
4 4
5 5
6 6
# UNION ALL and EXCEPT
analyze select * from t1 , ( (select t2.a from t2 order by c) union all (select t2.a from t2 order by c) except(select t3.a from t3 order by b))q where t1.a=q.a;
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 6 6.00 100.00 100.00 Using where
3 UNION t2 ALL NULL NULL NULL NULL 6 6.00 100.00 100.00
4 EXCEPT t3 ALL NULL NULL NULL NULL 6 6.00 100.00 100.00
NULL UNIT RESULT <unit2,3,4> ALL NULL NULL NULL NULL NULL 3.00 NULL NULL
select * from t1 , ( (select t2.a from t2 order by c) union all (select t2.a from t2 order by c) except(select t3.a from t3 order by b))q where t1.a=q.a;
--echo # MDEV-9959: A serious MariaDB server performance bug
--echo #
create table t1(a int);
insert into t1 values (1),(2),(3),(4),(5),(6);
create table t2(a int, b int,c int);
insert into t2(a,b,c) values (1,1,2),(2,2,3),(3,1,4),(4,2,2),(5,1,1),(6,2,5);
create table t3(a int, b int);
insert into t3(a,b) values (1,1),(2,2),(2,1),(1,2),(5,1),(9,2);
--echo table "<derived2>" should have type=ref and rows=1
--echo one select in derived table
--echo with distinct
analyze select * from t1 , ((select distinct t2.a from t2 order by c))q where t1.a=q.a;
analyze select * from t1 , ((select distinct t2.a, t2.b from t2 order by c))q where t1.a=q.a;
--echo # multiple selects in derived table
--echo # NO UNION ALL
analyze select * from t1 , ( (select t2.a from t2 order by c) union (select t2.a from t2 order by c))q where t1.a=q.a;
select * from t1 , ( (select t2.a from t2 order by c) union (select t2.a from t2 order by c))q where t1.a=q.a;
--echo # UNION ALL and EXCEPT
analyze select * from t1 , ( (select t2.a from t2 order by c) union all (select t2.a from t2 order by c) except(select t3.a from t3 order by b))q where t1.a=q.a;
select * from t1 , ( (select t2.a from t2 order by c) union all (select t2.a from t2 order by c) except(select t3.a from t3 order by b))q where t1.a=q.a;