diff --git a/mysql-test/r/stat_tables.result b/mysql-test/r/stat_tables.result index 08fb5ee825e..df2c61498de 100644 --- a/mysql-test/r/stat_tables.result +++ b/mysql-test/r/stat_tables.result @@ -118,6 +118,41 @@ dbt3_s001 partsupp i_ps_suppkey 1 70 dbt3_s001 region PRIMARY 1 1 dbt3_s001 supplier PRIMARY 1 1 dbt3_s001 supplier i_s_nationkey 1 1.1111111111111112 +select * from mysql.table_stat where table_name='orders'; +db_name table_name cardinality +dbt3_s001 orders 1500 +select * from mysql.index_stat where table_name='orders'; +db_name table_name index_name prefix_arity avg_frequency +dbt3_s001 orders PRIMARY 1 1 +dbt3_s001 orders i_o_orderdate 1 1.3321492007104796 +dbt3_s001 orders i_o_custkey 1 15 +select (select cardinality from mysql.table_stat where table_name='orders') / +(select avg_frequency from mysql.index_stat +where index_name='i_o_orderdate' and prefix_arity=1) as n_distinct; +n_distinct +1126 +select count(distinct o_orderdate) from orders; +count(distinct o_orderdate) +1126 +select (select cardinality from mysql.table_stat where table_name='orders') / +(select avg_frequency from mysql.index_stat +where index_name='i_o_custkey' and prefix_arity=1) as n_distinct; +n_distinct +100 +select count(distinct o_custkey) from orders; +count(distinct o_custkey) +100 +show index from orders; +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment +orders 0 PRIMARY 1 o_orderkey A 1500 NULL NULL BTREE +orders 1 i_o_orderdate 1 o_orderDATE A 1127 NULL NULL YES BTREE +orders 1 i_o_custkey 1 o_custkey A 100 NULL NULL YES BTREE +select index_name, column_name, cardinality from information_schema.statistics +where table_name='orders'; +index_name column_name cardinality +PRIMARY o_orderkey 1500 +i_o_orderdate o_orderDATE 1127 +i_o_custkey o_custkey 100 set @save_optimizer_switch=@@optimizer_switch; set optimizer_switch='index_condition_pushdown=off'; EXPLAIN select n_name, sum(l_extendedprice * (1 - l_discount)) as revenue diff --git a/mysql-test/r/stat_tables_innodb.result b/mysql-test/r/stat_tables_innodb.result index 99e3a2ab083..e59aaf1fc66 100644 --- a/mysql-test/r/stat_tables_innodb.result +++ b/mysql-test/r/stat_tables_innodb.result @@ -143,6 +143,43 @@ dbt3_s001 region PRIMARY 1 1 dbt3_s001 supplier PRIMARY 1 1 dbt3_s001 supplier i_s_nationkey 1 1.1111111111111112 dbt3_s001 supplier i_s_nationkey 2 1 +select * from mysql.table_stat where table_name='orders'; +db_name table_name cardinality +dbt3_s001 orders 1500 +select * from mysql.index_stat where table_name='orders'; +db_name table_name index_name prefix_arity avg_frequency +dbt3_s001 orders PRIMARY 1 1 +dbt3_s001 orders i_o_orderdate 1 1.3321492007104796 +dbt3_s001 orders i_o_orderdate 2 1 +dbt3_s001 orders i_o_custkey 1 15 +dbt3_s001 orders i_o_custkey 2 1 +select (select cardinality from mysql.table_stat where table_name='orders') / +(select avg_frequency from mysql.index_stat +where index_name='i_o_orderdate' and prefix_arity=1) as n_distinct; +n_distinct +1126 +select count(distinct o_orderdate) from orders; +count(distinct o_orderdate) +1126 +select (select cardinality from mysql.table_stat where table_name='orders') / +(select avg_frequency from mysql.index_stat +where index_name='i_o_custkey' and prefix_arity=1) as n_distinct; +n_distinct +100 +select count(distinct o_custkey) from orders; +count(distinct o_custkey) +100 +show index from orders; +Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment +orders 0 PRIMARY 1 o_orderkey A 1500 NULL NULL BTREE +orders 1 i_o_orderdate 1 o_orderDATE A 1127 NULL NULL YES BTREE +orders 1 i_o_custkey 1 o_custkey A 100 NULL NULL YES BTREE +select index_name, column_name, cardinality from information_schema.statistics +where table_name='orders'; +index_name column_name cardinality +PRIMARY o_orderkey 1500 +i_o_orderdate o_orderDATE 1127 +i_o_custkey o_custkey 100 set @save_optimizer_switch=@@optimizer_switch; set optimizer_switch='index_condition_pushdown=off'; EXPLAIN select n_name, sum(l_extendedprice * (1 - l_discount)) as revenue diff --git a/mysql-test/t/stat_tables.test b/mysql-test/t/stat_tables.test index 47ec0696552..d85c93b6732 100644 --- a/mysql-test/t/stat_tables.test +++ b/mysql-test/t/stat_tables.test @@ -69,6 +69,20 @@ FLUSH TABLE mysql.table_stat, mysql.index_stat; select * from mysql.table_stat; select * from mysql.index_stat; +select * from mysql.table_stat where table_name='orders'; +select * from mysql.index_stat where table_name='orders'; +select (select cardinality from mysql.table_stat where table_name='orders') / + (select avg_frequency from mysql.index_stat + where index_name='i_o_orderdate' and prefix_arity=1) as n_distinct; +select count(distinct o_orderdate) from orders; +select (select cardinality from mysql.table_stat where table_name='orders') / + (select avg_frequency from mysql.index_stat + where index_name='i_o_custkey' and prefix_arity=1) as n_distinct; +select count(distinct o_custkey) from orders; +show index from orders; +select index_name, column_name, cardinality from information_schema.statistics + where table_name='orders'; + set @save_optimizer_switch=@@optimizer_switch; set optimizer_switch='index_condition_pushdown=off'; diff --git a/sql/sql_select.cc b/sql/sql_select.cc index e482b6a8c96..9aa44a5a417 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -14102,6 +14102,7 @@ create_tmp_table(THD *thd, TMP_TABLE_PARAM *param, List &fields, table->intersect_keys.init(); table->keys_in_use_for_query.init(); table->no_rows_with_nulls= param->force_not_null_cols; + table->read_stat.cardinality_is_null= TRUE; table->s= share; init_tmp_table_share(thd, share, "", 0, tmpname, tmpname); diff --git a/sql/sql_show.cc b/sql/sql_show.cc index b5ed327c193..ec0749f4693 100644 --- a/sql/sql_show.cc +++ b/sql/sql_show.cc @@ -5441,9 +5441,12 @@ static int get_schema_stat_record(THD *thd, TABLE_LIST *tables, TABLE *show_table= tables->table; KEY *key_info=show_table->s->key_info; if (show_table->file) + { show_table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK | HA_STATUS_TIME); + set_statistics_for_table(thd, show_table); + } for (uint i=0 ; i < show_table->s->keys ; i++,key_info++) { KEY_PART_INFO *key_part= key_info->key_part; @@ -5474,8 +5477,8 @@ static int get_schema_stat_record(THD *thd, TABLE_LIST *tables, KEY *key=show_table->key_info+i; if (key->rec_per_key[j]) { - ha_rows records=(show_table->file->stats.records / - key->rec_per_key[j]); + ha_rows records=((double) show_table->stat_records() / + key->real_rec_per_key(j)); table->field[9]->store((longlong) records, TRUE); table->field[9]->set_notnull(); }