Commit 7d8d37c3 authored by Varun Gupta's avatar Varun Gupta

MDEV-17039: Query plan changes when we use GROUP BY optimization with...

MDEV-17039: Query plan changes when we use GROUP BY optimization with optimizer_use_condition_selectivity=4
            and use_stat_tables= PREFERABLY

Currently the code that calculates selectivity for a table does not take into account the case when
we can have GROUP BY optimization (looses index scan).
parent bcc677bb
......@@ -3733,5 +3733,33 @@ id MIN(a) MAX(a)
4 2001-01-04 2001-01-04
DROP TABLE t1;
#
# Query plan changes when we use GROUP BY optimization with optimizer_use_condition_selectivity=4
# and use_stat_tables= PREFERABLY
#
CREATE TABLE t1 (a INT, b INT,c INT DEFAULT 0, INDEX (a,b));
INSERT INTO t1 (a, b) VALUES (1,1), (1,2), (1,3), (1,4), (1,5),
(2,2), (2,3), (2,1), (3,1), (4,1), (4,2), (4,3), (4,4), (4,5), (4,6);
set @save_optimizer_use_condition_selectivity= @@optimizer_use_condition_selectivity;
set @save_use_stat_tables= @@use_stat_tables;
set @@optimizer_use_condition_selectivity=4;
set @@use_stat_tables=PREFERABLY;
explain extended SELECT a FROM t1 AS t1_outer WHERE a IN (SELECT max(b) FROM t1 GROUP BY a);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY <subquery2> ALL distinct_key NULL NULL NULL 8 100.00
1 PRIMARY t1_outer ref a a 5 <subquery2>.max(b) 2 100.00 Using index
2 MATERIALIZED t1 range NULL a 5 NULL 8 100.00 Using index for group-by
Warnings:
Note 1003 select `test`.`t1_outer`.`a` AS `a` from <materialize> (select max(`test`.`t1`.`b`) from `test`.`t1` group by `test`.`t1`.`a`) join `test`.`t1` `t1_outer` where (`test`.`t1_outer`.`a` = `<subquery2>`.`max(b)`)
set @@optimizer_use_condition_selectivity=@save_optimizer_use_condition_selectivity;
set @@use_stat_tables=@save_use_stat_tables;
explain extended SELECT a FROM t1 AS t1_outer WHERE a IN (SELECT max(b) FROM t1 GROUP BY a);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY <subquery2> ALL distinct_key NULL NULL NULL 8 100.00
1 PRIMARY t1_outer ref a a 5 <subquery2>.max(b) 2 100.00 Using index
2 MATERIALIZED t1 range NULL a 5 NULL 8 100.00 Using index for group-by
Warnings:
Note 1003 select `test`.`t1_outer`.`a` AS `a` from <materialize> (select max(`test`.`t1`.`b`) from `test`.`t1` group by `test`.`t1`.`a`) join `test`.`t1` `t1_outer` where (`test`.`t1_outer`.`a` = `<subquery2>`.`max(b)`)
drop table t1;
#
# End of 10.0 tests
#
......@@ -1519,6 +1519,23 @@ ALTER TABLE t1 ADD KEY(id,a);
SELECT id,MIN(a),MAX(a) FROM t1 WHERE a>=DATE'2001-01-04' GROUP BY id;
DROP TABLE t1;
--echo #
--echo # Query plan changes when we use GROUP BY optimization with optimizer_use_condition_selectivity=4
--echo # and use_stat_tables= PREFERABLY
--echo #
CREATE TABLE t1 (a INT, b INT,c INT DEFAULT 0, INDEX (a,b));
INSERT INTO t1 (a, b) VALUES (1,1), (1,2), (1,3), (1,4), (1,5),
(2,2), (2,3), (2,1), (3,1), (4,1), (4,2), (4,3), (4,4), (4,5), (4,6);
set @save_optimizer_use_condition_selectivity= @@optimizer_use_condition_selectivity;
set @save_use_stat_tables= @@use_stat_tables;
set @@optimizer_use_condition_selectivity=4;
set @@use_stat_tables=PREFERABLY;
explain extended SELECT a FROM t1 AS t1_outer WHERE a IN (SELECT max(b) FROM t1 GROUP BY a);
set @@optimizer_use_condition_selectivity=@save_optimizer_use_condition_selectivity;
set @@use_stat_tables=@save_use_stat_tables;
explain extended SELECT a FROM t1 AS t1_outer WHERE a IN (SELECT max(b) FROM t1 GROUP BY a);
drop table t1;
--echo #
--echo # End of 10.0 tests
......
......@@ -3501,7 +3501,18 @@ bool calculate_cond_selectivity_for_table(THD *thd, TABLE *table, Item *cond)
table->cond_selectivity= 1.0;
if (!cond || table_records == 0)
if (table_records == 0)
DBUG_RETURN(FALSE);
QUICK_SELECT_I *quick;
if ((quick=table->reginfo.join_tab->quick) &&
quick->get_type() == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX)
{
table->cond_selectivity*= (quick->records/table_records);
DBUG_RETURN(FALSE);
}
if (!cond)
DBUG_RETURN(FALSE);
if (table->pos_in_table_list->schema_table)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment