summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVarun Gupta <varun.gupta@mariadb.com>2020-05-31 14:40:33 +0530
committerVarun Gupta <varun.gupta@mariadb.com>2020-06-01 15:54:25 +0530
commite485ac2b6014b2b6b521870e04d9f16a18630e31 (patch)
treef4235d69cbea5e358a59c31d1ff5b19dd4c793ac
parent71a51eb3fa9374bafe97d0696d3c2d4370f2dbf3 (diff)
downloadmariadb-git-e485ac2b6014b2b6b521870e04d9f16a18630e31.tar.gz
MDEV-19474: Histogram statistics are used even with optimizer_use_condition_selectivity=3
Make sure to use the histogram statistics only when optimizer_use_condition_selectivity > 3 even if they were read (this may happen that the histogram statistics were read when optimizer_use_condition_selectivity was > 3 and then optimizer_use_condition_selectivity was set to < 4.
-rw-r--r--mysql-test/r/selectivity.result32
-rw-r--r--mysql-test/r/selectivity_innodb.result32
-rw-r--r--mysql-test/t/selectivity.test25
-rw-r--r--sql/sql_statistics.cc14
-rw-r--r--sql/table.cc1
5 files changed, 98 insertions, 6 deletions
diff --git a/mysql-test/r/selectivity.result b/mysql-test/r/selectivity.result
index 9bb197ad056..8f2226d8a6c 100644
--- a/mysql-test/r/selectivity.result
+++ b/mysql-test/r/selectivity.result
@@ -1881,4 +1881,36 @@ a b
5 5
set optimizer_use_condition_selectivity= @save_optimizer_use_condition_selectivity;
drop table t1;
+#
+# MDEV-19474: Histogram statistics are used even with optimizer_use_condition_selectivity=3
+#
+CREATE TABLE t1(a int);
+INSERT INTO t1 values (1),(2),(2),(3),(4);
+SET use_stat_tables='preferably';
+SET optimizer_use_condition_selectivity=4;
+SET histogram_size= 255;
+ANALYZE TABLE t1 PERSISTENT FOR ALL;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+EXPLAIN EXTENDED SELECT * FROM t1 WHERE a=2;
+id select_type table type possible_keys key key_len ref rows filtered Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 39.84 Using where
+Warnings:
+Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where (`test`.`t1`.`a` = 2)
+SET optimizer_use_condition_selectivity=3;
+# filtered should show 25 %
+EXPLAIN EXTENDED SELECT * FROM t1 WHERE a=2;
+id select_type table type possible_keys key key_len ref rows filtered Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 25.00 Using where
+Warnings:
+Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where (`test`.`t1`.`a` = 2)
+FLUSH TABLES;
+EXPLAIN EXTENDED SELECT * FROM t1 WHERE a=2;
+id select_type table type possible_keys key key_len ref rows filtered Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 25.00 Using where
+Warnings:
+Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where (`test`.`t1`.`a` = 2)
+set optimizer_use_condition_selectivity= @save_optimizer_use_condition_selectivity;
+DROP TABLE t1;
# End of 10.1 tests
diff --git a/mysql-test/r/selectivity_innodb.result b/mysql-test/r/selectivity_innodb.result
index d0e31374784..205d248bfa4 100644
--- a/mysql-test/r/selectivity_innodb.result
+++ b/mysql-test/r/selectivity_innodb.result
@@ -1891,6 +1891,38 @@ a b
5 5
set optimizer_use_condition_selectivity= @save_optimizer_use_condition_selectivity;
drop table t1;
+#
+# MDEV-19474: Histogram statistics are used even with optimizer_use_condition_selectivity=3
+#
+CREATE TABLE t1(a int);
+INSERT INTO t1 values (1),(2),(2),(3),(4);
+SET use_stat_tables='preferably';
+SET optimizer_use_condition_selectivity=4;
+SET histogram_size= 255;
+ANALYZE TABLE t1 PERSISTENT FOR ALL;
+Table Op Msg_type Msg_text
+test.t1 analyze status Engine-independent statistics collected
+test.t1 analyze status OK
+EXPLAIN EXTENDED SELECT * FROM t1 WHERE a=2;
+id select_type table type possible_keys key key_len ref rows filtered Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 39.84 Using where
+Warnings:
+Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where (`test`.`t1`.`a` = 2)
+SET optimizer_use_condition_selectivity=3;
+# filtered should show 25 %
+EXPLAIN EXTENDED SELECT * FROM t1 WHERE a=2;
+id select_type table type possible_keys key key_len ref rows filtered Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 25.00 Using where
+Warnings:
+Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where (`test`.`t1`.`a` = 2)
+FLUSH TABLES;
+EXPLAIN EXTENDED SELECT * FROM t1 WHERE a=2;
+id select_type table type possible_keys key key_len ref rows filtered Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 25.00 Using where
+Warnings:
+Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where (`test`.`t1`.`a` = 2)
+set optimizer_use_condition_selectivity= @save_optimizer_use_condition_selectivity;
+DROP TABLE t1;
# End of 10.1 tests
set optimizer_switch=@save_optimizer_switch_for_selectivity_test;
set @tmp_ust= @@use_stat_tables;
diff --git a/mysql-test/t/selectivity.test b/mysql-test/t/selectivity.test
index 1d0256b1b7e..bd36767db1f 100644
--- a/mysql-test/t/selectivity.test
+++ b/mysql-test/t/selectivity.test
@@ -1278,5 +1278,30 @@ SELECT * from t1 WHERE a = 5 and b = 5;
set optimizer_use_condition_selectivity= @save_optimizer_use_condition_selectivity;
drop table t1;
+--echo #
+--echo # MDEV-19474: Histogram statistics are used even with optimizer_use_condition_selectivity=3
+--echo #
+
+CREATE TABLE t1(a int);
+INSERT INTO t1 values (1),(2),(2),(3),(4);
+SET use_stat_tables='preferably';
+SET optimizer_use_condition_selectivity=4;
+SET histogram_size= 255;
+
+ANALYZE TABLE t1 PERSISTENT FOR ALL;
+
+EXPLAIN EXTENDED SELECT * FROM t1 WHERE a=2;
+SET optimizer_use_condition_selectivity=3;
+
+--echo # filtered should show 25 %
+
+EXPLAIN EXTENDED SELECT * FROM t1 WHERE a=2;
+FLUSH TABLES;
+
+EXPLAIN EXTENDED SELECT * FROM t1 WHERE a=2;
+set optimizer_use_condition_selectivity= @save_optimizer_use_condition_selectivity;
+
+DROP TABLE t1;
+
--echo # End of 10.1 tests
diff --git a/sql/sql_statistics.cc b/sql/sql_statistics.cc
index 11e2db9ae44..cafe11dbea6 100644
--- a/sql/sql_statistics.cc
+++ b/sql/sql_statistics.cc
@@ -3091,7 +3091,8 @@ bool statistics_for_tables_is_needed(THD *thd, TABLE_LIST *tables)
return TRUE;
if (table_share->stats_cb.stats_is_read)
tl->table->stats_is_read= TRUE;
- if (table_share->stats_cb.histograms_are_read)
+ if (table_share->stats_cb.histograms_are_read &&
+ thd->variables.optimizer_use_condition_selectivity > 3)
tl->table->histograms_are_read= TRUE;
}
}
@@ -3282,7 +3283,8 @@ int read_statistics_for_tables(THD *thd, TABLE_LIST *tables)
(void) read_histograms_for_table(thd, tl->table, stat_tables);
table_share->stats_cb.histograms_are_read= TRUE;
}
- if (table_share->stats_cb.histograms_are_read)
+ if (table_share->stats_cb.histograms_are_read &&
+ thd->variables.optimizer_use_condition_selectivity > 3)
tl->table->histograms_are_read= TRUE;
}
}
@@ -3857,7 +3859,7 @@ double get_column_range_cardinality(Field *field,
col_stats->min_max_values_are_provided())
{
Histogram *hist= &col_stats->histogram;
- if (hist->is_available())
+ if (table->histograms_are_read && hist->is_available())
{
store_key_image_to_rec(field, (uchar *) min_endp->key,
field->key_length());
@@ -3901,10 +3903,10 @@ double get_column_range_cardinality(Field *field,
max_mp_pos= 1.0;
Histogram *hist= &col_stats->histogram;
- if (!hist->is_available())
- sel= (max_mp_pos - min_mp_pos);
- else
+ if (table->histograms_are_read && hist->is_available())
sel= hist->range_selectivity(min_mp_pos, max_mp_pos);
+ else
+ sel= (max_mp_pos - min_mp_pos);
res= col_non_nulls * sel;
set_if_bigger(res, col_stats->get_avg_frequency());
}
diff --git a/sql/table.cc b/sql/table.cc
index 192faa0d00e..48817de4215 100644
--- a/sql/table.cc
+++ b/sql/table.cc
@@ -4229,6 +4229,7 @@ void TABLE::init(THD *thd, TABLE_LIST *tl)
cond_selectivity_sampling_explain= NULL;
quick_condition_rows=0;
initialize_quick_structures();
+ histograms_are_read= FALSE;
#ifdef HAVE_REPLICATION
/* used in RBR Triggers */
master_had_triggers= 0;