Skip to content

Commit 70a5fb4

Browse files
author
Varun Gupta
committed
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.
1 parent 6ab9d16 commit 70a5fb4

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

mysql-test/r/stat_tables.result

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,17 @@ pk
728728
4
729729
3
730730
drop table t1,t2;
731+
create table t1(a int,b int, key k1(a) );
732+
insert into t1 values(1,1),(2,2),(3,3);
733+
analyze table t1;
734+
Table Op Msg_type Msg_text
735+
test.t1 analyze status Engine-independent statistics collected
736+
test.t1 analyze status OK
737+
select * from mysql.index_stats, t1 where index_name='k1' and t1.a > 1 and t1.b > 1;
738+
db_name table_name index_name prefix_arity avg_frequency a b
739+
test t1 k1 1 1.0000 2 2
740+
test t1 k1 1 1.0000 3 3
741+
drop table t1;
731742
set @@optimizer_use_condition_selectivity= @save_optimizer_use_condition_selectivity;
732743
set @save_optimizer_switch=@@optimizer_switch;
733744
set use_stat_tables=@save_use_stat_tables;

mysql-test/r/stat_tables_innodb.result

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,17 @@ pk
755755
4
756756
3
757757
drop table t1,t2;
758+
create table t1(a int,b int, key k1(a) );
759+
insert into t1 values(1,1),(2,2),(3,3);
760+
analyze table t1;
761+
Table Op Msg_type Msg_text
762+
test.t1 analyze status Engine-independent statistics collected
763+
test.t1 analyze status OK
764+
select * from mysql.index_stats, t1 where index_name='k1' and t1.a > 1 and t1.b > 1;
765+
db_name table_name index_name prefix_arity avg_frequency a b
766+
test t1 k1 1 1.0000 2 2
767+
test t1 k1 1 1.0000 3 3
768+
drop table t1;
758769
set @@optimizer_use_condition_selectivity= @save_optimizer_use_condition_selectivity;
759770
set @save_optimizer_switch=@@optimizer_switch;
760771
set use_stat_tables=@save_use_stat_tables;

mysql-test/t/stat_tables.test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,12 @@ CREATE TABLE t2 SELECT pk FROM t1 WHERE pk>2;
492492
select * from t2;
493493
drop table t1,t2;
494494

495+
create table t1(a int,b int, key k1(a) );
496+
insert into t1 values(1,1),(2,2),(3,3);
497+
analyze table t1;
498+
select * from mysql.index_stats, t1 where index_name='k1' and t1.a > 1 and t1.b > 1;
499+
drop table t1;
500+
495501
set @@optimizer_use_condition_selectivity= @save_optimizer_use_condition_selectivity;
496502

497503
set @save_optimizer_switch=@@optimizer_switch;

sql/opt_range.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3050,7 +3050,7 @@ bool calculate_cond_selectivity_for_table(THD *thd, TABLE *table, Item **cond)
30503050

30513051
if (thd->variables.optimizer_use_condition_selectivity > 2 &&
30523052
!bitmap_is_clear_all(used_fields) &&
3053-
thd->variables.use_stat_tables > 0)
3053+
thd->variables.use_stat_tables > 0 && table->stats_is_read)
30543054
{
30553055
PARAM param;
30563056
MEM_ROOT alloc;

0 commit comments

Comments
 (0)