From 70a5fb49a74e2dfd57fa79ebbc0fc780ce898841 Mon Sep 17 00:00:00 2001 From: Varun Gupta Date: Thu, 16 May 2019 13:49:47 +0530 Subject: [PATCH] Fixed the case when statistics were not getting read because we had the statistics tables in the FROM list of the select. The statistics for tables are not read in such cases, so we need to check this case separately. --- mysql-test/r/stat_tables.result | 11 +++++++++++ mysql-test/r/stat_tables_innodb.result | 11 +++++++++++ mysql-test/t/stat_tables.test | 6 ++++++ sql/opt_range.cc | 2 +- 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/mysql-test/r/stat_tables.result b/mysql-test/r/stat_tables.result index afb3e12a474..d26221b5f8d 100644 --- a/mysql-test/r/stat_tables.result +++ b/mysql-test/r/stat_tables.result @@ -728,6 +728,17 @@ pk 4 3 drop table t1,t2; +create table t1(a int,b int, key k1(a) ); +insert into t1 values(1,1),(2,2),(3,3); +analyze table t1; +Table Op Msg_type Msg_text +test.t1 analyze status Engine-independent statistics collected +test.t1 analyze status OK +select * from mysql.index_stats, t1 where index_name='k1' and t1.a > 1 and t1.b > 1; +db_name table_name index_name prefix_arity avg_frequency a b +test t1 k1 1 1.0000 2 2 +test t1 k1 1 1.0000 3 3 +drop table t1; set @@optimizer_use_condition_selectivity= @save_optimizer_use_condition_selectivity; set @save_optimizer_switch=@@optimizer_switch; set use_stat_tables=@save_use_stat_tables; diff --git a/mysql-test/r/stat_tables_innodb.result b/mysql-test/r/stat_tables_innodb.result index 643a7aa55ea..b8bed681465 100644 --- a/mysql-test/r/stat_tables_innodb.result +++ b/mysql-test/r/stat_tables_innodb.result @@ -755,6 +755,17 @@ pk 4 3 drop table t1,t2; +create table t1(a int,b int, key k1(a) ); +insert into t1 values(1,1),(2,2),(3,3); +analyze table t1; +Table Op Msg_type Msg_text +test.t1 analyze status Engine-independent statistics collected +test.t1 analyze status OK +select * from mysql.index_stats, t1 where index_name='k1' and t1.a > 1 and t1.b > 1; +db_name table_name index_name prefix_arity avg_frequency a b +test t1 k1 1 1.0000 2 2 +test t1 k1 1 1.0000 3 3 +drop table t1; set @@optimizer_use_condition_selectivity= @save_optimizer_use_condition_selectivity; set @save_optimizer_switch=@@optimizer_switch; set use_stat_tables=@save_use_stat_tables; diff --git a/mysql-test/t/stat_tables.test b/mysql-test/t/stat_tables.test index 1e4d261c3f3..e9f37698a73 100644 --- a/mysql-test/t/stat_tables.test +++ b/mysql-test/t/stat_tables.test @@ -492,6 +492,12 @@ CREATE TABLE t2 SELECT pk FROM t1 WHERE pk>2; select * from t2; drop table t1,t2; +create table t1(a int,b int, key k1(a) ); +insert into t1 values(1,1),(2,2),(3,3); +analyze table t1; +select * from mysql.index_stats, t1 where index_name='k1' and t1.a > 1 and t1.b > 1; +drop table t1; + set @@optimizer_use_condition_selectivity= @save_optimizer_use_condition_selectivity; set @save_optimizer_switch=@@optimizer_switch; diff --git a/sql/opt_range.cc b/sql/opt_range.cc index 71e8055394c..e8421ad052a 100644 --- a/sql/opt_range.cc +++ b/sql/opt_range.cc @@ -3050,7 +3050,7 @@ bool calculate_cond_selectivity_for_table(THD *thd, TABLE *table, Item **cond) if (thd->variables.optimizer_use_condition_selectivity > 2 && !bitmap_is_clear_all(used_fields) && - thd->variables.use_stat_tables > 0) + thd->variables.use_stat_tables > 0 && table->stats_is_read) { PARAM param; MEM_ROOT alloc;