Skip to content

Commit a461e4d

Browse files
author
Varun Gupta
committed
MDEV-19474: Histogram statistics are used even with optimizer_use_condition_selectivity=3
The issue here was histogram statistics were being used even when the level of optimizer_use_condition_selectivity doesn't allow usage of statistics from histogram. The histogram statistics are read for a table only when optimizer_use_condition_selectivity > 3. But the TABLE structure can be stored in the internal table cache and be reused for the next query. So in this case the histogram statistics will be available for the next query. The fix would be to make sure to use the histogram statistics only when optimizer_use_condition_selectivity > 3.
1 parent e926964 commit a461e4d

File tree

5 files changed

+111
-4
lines changed

5 files changed

+111
-4
lines changed

mysql-test/r/selectivity.result

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1910,5 +1910,39 @@ HEX(a) b
19101910
set optimizer_use_condition_selectivity= @save_optimizer_use_condition_selectivity;
19111911
set histogram_size=@save_histogram_size;
19121912
DROP TABLE t1;
1913+
#
1914+
# MDEV-19474: Histogram statistics are used even with optimizer_use_condition_selectivity=3
1915+
#
1916+
CREATE TABLE t1(a int);
1917+
INSERT INTO t1 values (1),(2),(2),(3),(4);
1918+
SET optimizer_use_condition_selectivity=4;
1919+
SET histogram_size= 255;
1920+
set use_stat_tables='preferably';
1921+
ANALYZE TABLE t1 PERSISTENT FOR ALL;
1922+
Table Op Msg_type Msg_text
1923+
test.t1 analyze status Engine-independent statistics collected
1924+
test.t1 analyze status OK
1925+
EXPLAIN EXTENDED SELECT * FROM t1 WHERE a=2;
1926+
id select_type table type possible_keys key key_len ref rows filtered Extra
1927+
1 SIMPLE t1 ALL NULL NULL NULL NULL 5 39.84 Using where
1928+
Warnings:
1929+
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` = 2
1930+
SET optimizer_use_condition_selectivity=3;
1931+
# filtered should show 25 %
1932+
EXPLAIN EXTENDED SELECT * FROM t1 WHERE a=2;
1933+
id select_type table type possible_keys key key_len ref rows filtered Extra
1934+
1 SIMPLE t1 ALL NULL NULL NULL NULL 5 25.00 Using where
1935+
Warnings:
1936+
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` = 2
1937+
FLUSH TABLES;
1938+
EXPLAIN EXTENDED SELECT * FROM t1 WHERE a=2;
1939+
id select_type table type possible_keys key key_len ref rows filtered Extra
1940+
1 SIMPLE t1 ALL NULL NULL NULL NULL 5 25.00 Using where
1941+
Warnings:
1942+
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` = 2
1943+
set optimizer_use_condition_selectivity= @save_optimizer_use_condition_selectivity;
1944+
set histogram_size=@save_histogram_size;
1945+
set use_stat_tables= @save_use_stat_tables;
1946+
DROP TABLE t1;
19131947
# End of 10.2 tests
19141948
set @@global.histogram_size=@save_histogram_size;

mysql-test/r/selectivity_innodb.result

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1920,6 +1920,40 @@ HEX(a) b
19201920
set optimizer_use_condition_selectivity= @save_optimizer_use_condition_selectivity;
19211921
set histogram_size=@save_histogram_size;
19221922
DROP TABLE t1;
1923+
#
1924+
# MDEV-19474: Histogram statistics are used even with optimizer_use_condition_selectivity=3
1925+
#
1926+
CREATE TABLE t1(a int);
1927+
INSERT INTO t1 values (1),(2),(2),(3),(4);
1928+
SET optimizer_use_condition_selectivity=4;
1929+
SET histogram_size= 255;
1930+
set use_stat_tables='preferably';
1931+
ANALYZE TABLE t1 PERSISTENT FOR ALL;
1932+
Table Op Msg_type Msg_text
1933+
test.t1 analyze status Engine-independent statistics collected
1934+
test.t1 analyze status OK
1935+
EXPLAIN EXTENDED SELECT * FROM t1 WHERE a=2;
1936+
id select_type table type possible_keys key key_len ref rows filtered Extra
1937+
1 SIMPLE t1 ALL NULL NULL NULL NULL 5 39.84 Using where
1938+
Warnings:
1939+
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` = 2
1940+
SET optimizer_use_condition_selectivity=3;
1941+
# filtered should show 25 %
1942+
EXPLAIN EXTENDED SELECT * FROM t1 WHERE a=2;
1943+
id select_type table type possible_keys key key_len ref rows filtered Extra
1944+
1 SIMPLE t1 ALL NULL NULL NULL NULL 5 25.00 Using where
1945+
Warnings:
1946+
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` = 2
1947+
FLUSH TABLES;
1948+
EXPLAIN EXTENDED SELECT * FROM t1 WHERE a=2;
1949+
id select_type table type possible_keys key key_len ref rows filtered Extra
1950+
1 SIMPLE t1 ALL NULL NULL NULL NULL 5 25.00 Using where
1951+
Warnings:
1952+
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` = 2
1953+
set optimizer_use_condition_selectivity= @save_optimizer_use_condition_selectivity;
1954+
set histogram_size=@save_histogram_size;
1955+
set use_stat_tables= @save_use_stat_tables;
1956+
DROP TABLE t1;
19231957
# End of 10.2 tests
19241958
set @@global.histogram_size=@save_histogram_size;
19251959
set optimizer_switch=@save_optimizer_switch_for_selectivity_test;

mysql-test/t/selectivity.test

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,6 +1302,33 @@ set optimizer_use_condition_selectivity= @save_optimizer_use_condition_selectivi
13021302
set histogram_size=@save_histogram_size;
13031303
DROP TABLE t1;
13041304

1305+
--echo #
1306+
--echo # MDEV-19474: Histogram statistics are used even with optimizer_use_condition_selectivity=3
1307+
--echo #
1308+
1309+
CREATE TABLE t1(a int);
1310+
INSERT INTO t1 values (1),(2),(2),(3),(4);
1311+
SET optimizer_use_condition_selectivity=4;
1312+
SET histogram_size= 255;
1313+
1314+
set use_stat_tables='preferably';
1315+
1316+
ANALYZE TABLE t1 PERSISTENT FOR ALL;
1317+
1318+
EXPLAIN EXTENDED SELECT * FROM t1 WHERE a=2;
1319+
SET optimizer_use_condition_selectivity=3;
1320+
1321+
--echo # filtered should show 25 %
1322+
EXPLAIN EXTENDED SELECT * FROM t1 WHERE a=2;
1323+
FLUSH TABLES;
1324+
1325+
EXPLAIN EXTENDED SELECT * FROM t1 WHERE a=2;
1326+
set optimizer_use_condition_selectivity= @save_optimizer_use_condition_selectivity;
1327+
set histogram_size=@save_histogram_size;
1328+
set use_stat_tables= @save_use_stat_tables;
1329+
1330+
DROP TABLE t1;
1331+
13051332
--echo # End of 10.2 tests
13061333

13071334
#

sql/sql_statistics.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3732,6 +3732,7 @@ double get_column_range_cardinality(Field *field,
37323732
if (!table->stats_is_read)
37333733
return tab_records;
37343734

3735+
THD *thd= table->in_use;
37353736
double col_nulls= tab_records * col_stats->get_nulls_ratio();
37363737

37373738
double col_non_nulls= tab_records - col_nulls;
@@ -3762,7 +3763,7 @@ double get_column_range_cardinality(Field *field,
37623763
col_stats->min_max_values_are_provided())
37633764
{
37643765
Histogram *hist= &col_stats->histogram;
3765-
if (hist->is_available())
3766+
if (hist->is_usable(thd))
37663767
{
37673768
store_key_image_to_rec(field, (uchar *) min_endp->key,
37683769
field->key_length());
@@ -3806,10 +3807,10 @@ double get_column_range_cardinality(Field *field,
38063807
max_mp_pos= 1.0;
38073808

38083809
Histogram *hist= &col_stats->histogram;
3809-
if (!hist->is_available())
3810-
sel= (max_mp_pos - min_mp_pos);
3811-
else
3810+
if (hist->is_usable(thd))
38123811
sel= hist->range_selectivity(min_mp_pos, max_mp_pos);
3812+
else
3813+
sel= (max_mp_pos - min_mp_pos);
38133814
res= col_non_nulls * sel;
38143815
set_if_bigger(res, col_stats->get_avg_frequency());
38153816
}

sql/sql_statistics.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,17 @@ class Histogram
212212

213213
bool is_available() { return get_size() > 0 && get_values(); }
214214

215+
/*
216+
This function checks that histograms should be usable only when
217+
1) the level of optimizer_use_condition_selectivity > 3
218+
2) histograms have been collected
219+
*/
220+
bool is_usable(THD *thd)
221+
{
222+
return thd->variables.optimizer_use_condition_selectivity > 3 &&
223+
is_available();
224+
}
225+
215226
void set_value(uint i, double val)
216227
{
217228
switch (type) {

0 commit comments

Comments
 (0)