Skip to content

Commit 38b3e52

Browse files
committed
MDEV-16695: Estimate for rows of derived tables is very high when we are using index_merge union
For index merge union[or sort union], the estimates are not taken into account while calculating the selectivity of a condition. So instead of showing the estimates of the index merge union[or sort union], it shows estimates equal to all the records of the table. The fix for the issue is to include the selectivity of index merge union[or sort union] while calculating the selectivity of a condition.
1 parent c4c738e commit 38b3e52

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

mysql-test/r/index_merge_myisam.result

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,3 +1712,53 @@ id select_type table type possible_keys key key_len ref rows Extra
17121712
1 SIMPLE t1 ALL PRIMARY,c1,i,c2 NULL NULL NULL 69 Using where
17131713
DROP TABLE t1;
17141714
set optimizer_switch= @optimizer_switch_save;
1715+
#
1716+
# MDEV-16695: Estimate for rows of derived tables is very high when we are using index_merge union
1717+
#
1718+
create table t0
1719+
(
1720+
key1 int not null,
1721+
INDEX i1(key1)
1722+
);
1723+
insert into t0 values (1),(2),(3),(4),(5),(6),(7),(8);
1724+
set @d=8;
1725+
insert into t0 select key1+ @d from t0;
1726+
set @d=@d*2;
1727+
insert into t0 select key1+ @d from t0;
1728+
set @d=@d*2;
1729+
insert into t0 select key1+ @d from t0;
1730+
set @d=@d*2;
1731+
insert into t0 select key1+ @d from t0;
1732+
set @d=@d*2;
1733+
insert into t0 select key1+ @d from t0;
1734+
set @d=@d*2;
1735+
insert into t0 select key1+ @d from t0;
1736+
set @d=@d*2;
1737+
insert into t0 select key1+ @d from t0;
1738+
set @d=@d*2;
1739+
alter table t0 add key2 int not null, add index i2(key2);
1740+
alter table t0 add key3 int not null, add index i3(key3);
1741+
alter table t0 add key8 int not null, add index i8(key8);
1742+
update t0 set key2=key1,key3=key1,key8=1024-key1;
1743+
analyze table t0;
1744+
Table Op Msg_type Msg_text
1745+
test.t0 analyze status OK
1746+
set @optimizer_switch_save=@@optimizer_switch;
1747+
set optimizer_switch='derived_merge=off,derived_with_keys=off';
1748+
explain select * from (select * from t0 where key1 = 3 or key2 =3) as Z where Z.key8 > 5;
1749+
id select_type table type possible_keys key key_len ref rows Extra
1750+
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 2 Using where
1751+
2 DERIVED t0 index_merge i1,i2 i1,i2 4,4 NULL 2 Using union(i1,i2); Using where
1752+
select * from (select * from t0 where key1 = 3 or key2 =3) as Z where Z.key8 > 5;
1753+
key1 key2 key3 key8
1754+
3 3 3 1021
1755+
set optimizer_use_condition_selectivity=2;
1756+
explain select * from (select * from t0 where key1 = 3 or key2 =3) as Z where Z.key8 > 5;
1757+
id select_type table type possible_keys key key_len ref rows Extra
1758+
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 2 Using where
1759+
2 DERIVED t0 index_merge i1,i2 i1,i2 4,4 NULL 2 Using union(i1,i2); Using where
1760+
select * from (select * from t0 where key1 = 3 or key2 =3) as Z where Z.key8 > 5;
1761+
key1 key2 key3 key8
1762+
3 3 3 1021
1763+
set @@optimizer_switch= @optimizer_switch_save;
1764+
drop table t0;

mysql-test/t/index_merge_myisam.test

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,38 @@ DROP TABLE t1;
243243

244244
set optimizer_switch= @optimizer_switch_save;
245245

246+
--echo #
247+
--echo # MDEV-16695: Estimate for rows of derived tables is very high when we are using index_merge union
248+
--echo #
249+
250+
create table t0
251+
(
252+
key1 int not null,
253+
INDEX i1(key1)
254+
);
255+
256+
insert into t0 values (1),(2),(3),(4),(5),(6),(7),(8);
257+
let $1=7;
258+
set @d=8;
259+
while ($1)
260+
{
261+
eval insert into t0 select key1+ @d from t0;
262+
eval set @d=@d*2;
263+
dec $1;
264+
}
265+
alter table t0 add key2 int not null, add index i2(key2);
266+
alter table t0 add key3 int not null, add index i3(key3);
267+
alter table t0 add key8 int not null, add index i8(key8);
268+
269+
update t0 set key2=key1,key3=key1,key8=1024-key1;
270+
analyze table t0;
271+
272+
set @optimizer_switch_save=@@optimizer_switch;
273+
set optimizer_switch='derived_merge=off,derived_with_keys=off';
274+
explain select * from (select * from t0 where key1 = 3 or key2 =3) as Z where Z.key8 > 5;
275+
select * from (select * from t0 where key1 = 3 or key2 =3) as Z where Z.key8 > 5;
276+
set optimizer_use_condition_selectivity=2;
277+
explain select * from (select * from t0 where key1 = 3 or key2 =3) as Z where Z.key8 > 5;
278+
select * from (select * from t0 where key1 = 3 or key2 =3) as Z where Z.key8 > 5;
279+
set @@optimizer_switch= @optimizer_switch_save;
280+
drop table t0;

sql/opt_range.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3725,6 +3725,12 @@ bool calculate_cond_selectivity_for_table(THD *thd, TABLE *table, Item *cond)
37253725

37263726
}
37273727

3728+
if (quick && (quick->get_type() == QUICK_SELECT_I::QS_TYPE_ROR_UNION ||
3729+
quick->get_type() == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE))
3730+
{
3731+
table->cond_selectivity*= (quick->records/table_records);
3732+
}
3733+
37283734
bitmap_union(used_fields, &handled_columns);
37293735

37303736
/* Check if we can improve selectivity estimates by using sampling */

0 commit comments

Comments
 (0)