Skip to content
/ server Public

Commit 2ce3f0d

Browse files
committed
MDEV-38240: Selectivity sampling not performed when the table has no indexed conditions
Don't skip range analysis step in such cases.
1 parent 997d0c4 commit 2ce3f0d

File tree

4 files changed

+91
-8
lines changed

4 files changed

+91
-8
lines changed

mysql-test/main/selectivity.result

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2114,4 +2114,32 @@ a b c d e
21142114
2 NULL NULL NULL NULL
21152115
set join_cache_level= default;
21162116
DROP TABLE t1, t2, t3;
2117+
#
2118+
# MDEV-38240: Selectivity sampling not performed when the table has no indexed conditions
2119+
#
2120+
set use_stat_tables=PREFERABLY;
2121+
create table t1 (a varchar(100), b int);
2122+
insert into t1 select seq, seq from seq_1_to_1000;
2123+
analyze table t1;
2124+
Table Op Msg_type Msg_text
2125+
test.t1 analyze status Engine-independent statistics collected
2126+
test.t1 analyze status OK
2127+
set optimizer_use_condition_selectivity=5;
2128+
# Must produce filtered=1% or so:
2129+
explain extended
2130+
select * from t1 where a like '%99%';
2131+
id select_type table type possible_keys key key_len ref rows filtered Extra
2132+
1 SIMPLE t1 ALL NULL NULL NULL NULL 1000 1.00 Using where
2133+
Warnings:
2134+
Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b` from `test`.`t1` where `test`.`t1`.`a` like '%99%'
2135+
# Must have same filtered as above
2136+
explain extended
2137+
select * from t1 where a like '%99%' and (b is null or b is not null);
2138+
id select_type table type possible_keys key key_len ref rows filtered Extra
2139+
1 SIMPLE t1 ALL NULL NULL NULL NULL 1000 1.00 Using where
2140+
Warnings:
2141+
Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b` from `test`.`t1` where `test`.`t1`.`a` like '%99%' and (`test`.`t1`.`b` is null or `test`.`t1`.`b` is not null)
2142+
drop table t1;
2143+
set optimizer_use_condition_selectivity= @save_optimizer_use_condition_selectivity;
2144+
set use_stat_tables= @save_use_stat_tables;
21172145
ALTER DATABASE test CHARACTER SET utf8mb4 COLLATE utf8mb4_uca1400_ai_ci;

mysql-test/main/selectivity.test

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,4 +1456,25 @@ SELECT * FROM t1 LEFT JOIN (t2 JOIN t3 ON t2.b = t3.e) ON t2.c = 1;
14561456
set join_cache_level= default;
14571457
DROP TABLE t1, t2, t3;
14581458

1459+
--echo #
1460+
--echo # MDEV-38240: Selectivity sampling not performed when the table has no indexed conditions
1461+
--echo #
1462+
set use_stat_tables=PREFERABLY;
1463+
create table t1 (a varchar(100), b int);
1464+
insert into t1 select seq, seq from seq_1_to_1000;
1465+
analyze table t1;
1466+
1467+
set optimizer_use_condition_selectivity=5;
1468+
--echo # Must produce filtered=1% or so:
1469+
explain extended
1470+
select * from t1 where a like '%99%';
1471+
1472+
--echo # Must have same filtered as above
1473+
explain extended
1474+
select * from t1 where a like '%99%' and (b is null or b is not null);
1475+
drop table t1;
1476+
1477+
set optimizer_use_condition_selectivity= @save_optimizer_use_condition_selectivity;
1478+
set use_stat_tables= @save_use_stat_tables;
1479+
14591480
--source include/test_db_charset_restore.inc

mysql-test/main/selectivity_innodb.result

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2121,6 +2121,34 @@ a b c d e
21212121
2 NULL NULL NULL NULL
21222122
set join_cache_level= default;
21232123
DROP TABLE t1, t2, t3;
2124+
#
2125+
# MDEV-38240: Selectivity sampling not performed when the table has no indexed conditions
2126+
#
2127+
set use_stat_tables=PREFERABLY;
2128+
create table t1 (a varchar(100), b int);
2129+
insert into t1 select seq, seq from seq_1_to_1000;
2130+
analyze table t1;
2131+
Table Op Msg_type Msg_text
2132+
test.t1 analyze status Engine-independent statistics collected
2133+
test.t1 analyze status OK
2134+
set optimizer_use_condition_selectivity=5;
2135+
# Must produce filtered=1% or so:
2136+
explain extended
2137+
select * from t1 where a like '%99%';
2138+
id select_type table type possible_keys key key_len ref rows filtered Extra
2139+
1 SIMPLE t1 ALL NULL NULL NULL NULL 1000 1.00 Using where
2140+
Warnings:
2141+
Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b` from `test`.`t1` where `test`.`t1`.`a` like '%99%'
2142+
# Must have same filtered as above
2143+
explain extended
2144+
select * from t1 where a like '%99%' and (b is null or b is not null);
2145+
id select_type table type possible_keys key key_len ref rows filtered Extra
2146+
1 SIMPLE t1 ALL NULL NULL NULL NULL 1000 1.00 Using where
2147+
Warnings:
2148+
Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b` from `test`.`t1` where `test`.`t1`.`a` like '%99%' and (`test`.`t1`.`b` is null or `test`.`t1`.`b` is not null)
2149+
drop table t1;
2150+
set optimizer_use_condition_selectivity= @save_optimizer_use_condition_selectivity;
2151+
set use_stat_tables= @save_use_stat_tables;
21242152
ALTER DATABASE test CHARACTER SET utf8mb4 COLLATE utf8mb4_uca1400_ai_ci;
21252153
set optimizer_switch=@save_optimizer_switch_for_selectivity_test;
21262154
set @tmp_ust= @@use_stat_tables;

sql/sql_select.cc

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6220,15 +6220,21 @@ make_join_statistics(JOIN *join, List<TABLE_LIST> &tables_list,
62206220
s->table->opt_range_condition_rows /
62216221
s->table->used_stat_records);
62226222
/*
6223-
Perform range analysis if there are keys it could use (1).
6224-
Don't do range analysis for materialized subqueries (2).
6225-
Don't do range analysis for materialized derived tables/views (3)
6223+
Perform range analysis if we could infer something from it.
6224+
(1) There are indexes for which we have range conditions,
6225+
(2) Or there are sargable conditions on the table's columns that we
6226+
could use for selectivity estimation,
6227+
(3) Or selectivity estimation via sampling is enabled.
6228+
6229+
(4) Don't do range analysis for materialized subqueries.
6230+
(5) Don't do range analysis for materialized derived tables/views.
62266231
*/
6227-
if ((!s->const_keys.is_clear_all() ||
6228-
!bitmap_is_clear_all(&s->table->cond_set)) && // (1)
6229-
!s->table->is_filled_at_execution() && // (2)
6230-
!(s->table->pos_in_table_list->derived && // (3)
6231-
s->table->pos_in_table_list->is_materialized_derived())) // (3)
6232+
if ((!s->const_keys.is_clear_all() || // (1)
6233+
!bitmap_is_clear_all(&s->table->cond_set) || // (2)
6234+
thd->variables.optimizer_use_condition_selectivity >= 5) && // (3)
6235+
!s->table->is_filled_at_execution() && // (4)
6236+
!(s->table->pos_in_table_list->derived && // (5)
6237+
s->table->pos_in_table_list->is_materialized_derived())) // (5)
62326238
{
62336239
bool impossible_range= FALSE;
62346240
ha_rows records= HA_ROWS_MAX;

0 commit comments

Comments
 (0)