diff --git a/mysql-test/r/statistics.result b/mysql-test/r/statistics.result index ba0390f98db..a22402ccd00 100644 --- a/mysql-test/r/statistics.result +++ b/mysql-test/r/statistics.result @@ -1072,9 +1072,19 @@ test t2 idx4 4 1.0000 ALTER TABLE t2 CHANGE COLUMN b b varchar(32); SELECT * FROM mysql.index_stats ORDER BY index_name, prefix_arity, table_name; db_name table_name index_name prefix_arity avg_frequency +test t2 PRIMARY 1 1.0000 +test t2 PRIMARY 2 1.0000 test t2 idx2 1 7.0000 test t2 idx2 2 2.3846 +test t2 idx2 3 1.0000 +test t2 idx2 4 1.0000 test t2 idx3 1 8.5000 +test t2 idx3 2 1.0000 +test t2 idx3 3 1.0000 +test t2 idx4 1 6.2000 +test t2 idx4 2 1.7222 +test t2 idx4 3 1.1154 +test t2 idx4 4 1.0000 ANALYZE TABLE t2 PERSISTENT FOR COLUMNS ALL INDEXES ALL; Table Op Msg_type Msg_text test.t2 analyze status OK @@ -1131,13 +1141,13 @@ test t2 f 1 5 0.2000 1.0000 6.4000 SELECT * FROM mysql.index_stats; db_name table_name index_name prefix_arity avg_frequency test t2 idx3 1 8.5000 -test t2 idx3 2 1.0000 -test t2 idx2 3 1.0000 test t2 idx2 1 7.0000 test t2 idx2 2 2.3846 -test t2 idx4 3 1.0000 +test t2 idx2 3 1.0000 test t2 idx4 1 6.2000 test t2 idx4 2 2.2308 +test t2 idx4 3 1.0000 +test t2 idx3 2 1.0000 test t2 PRIMARY 1 1.0000 ANALYZE TABLE t1; Table Op Msg_type Msg_text @@ -1157,17 +1167,17 @@ test t1 f 1 5 0.2000 1.0000 6.4000 test t1 b NULL NULL 0.2000 17.1250 NULL SELECT * FROM mysql.index_stats; db_name table_name index_name prefix_arity avg_frequency -test t2 idx3 1 8.5000 -test t2 idx3 2 1.0000 -test t2 idx2 3 1.0000 -test t2 idx2 1 7.0000 -test t2 idx2 2 2.3846 test t1 idx2 1 7.0000 +test t2 idx3 1 8.5000 test t1 idx3 1 8.5000 test t1 PRIMARY 1 1.0000 -test t2 idx4 3 1.0000 +test t2 idx2 1 7.0000 +test t2 idx2 2 2.3846 +test t2 idx2 3 1.0000 test t2 idx4 1 6.2000 test t2 idx4 2 2.2308 +test t2 idx4 3 1.0000 +test t2 idx3 2 1.0000 test t2 PRIMARY 1 1.0000 test t1 idx2 2 2.3846 test t1 idx1 1 NULL diff --git a/sql/sql_statistics.cc b/sql/sql_statistics.cc index 14efb0265cc..c872069ed28 100644 --- a/sql/sql_statistics.cc +++ b/sql/sql_statistics.cc @@ -2990,7 +2990,10 @@ int rename_column_in_stat_tables(THD *thd, TABLE *tab, Field *col, int rc= 0; DBUG_ENTER("rename_column_in_stat_tables"); - + + if (tab->s->tmp_table != NO_TMP_TABLE) + DBUG_RETURN(0); + if (open_single_stat_table(thd, &tables, &stat_table_name[1], &open_tables_backup, TRUE)) { diff --git a/sql/sql_statistics.h b/sql/sql_statistics.h index 17f22cec4e5..2b40d56fea4 100644 --- a/sql/sql_statistics.h +++ b/sql/sql_statistics.h @@ -93,6 +93,17 @@ void set_statistics_for_table(THD *thd, TABLE *table); class Columns_statistics; class Index_statistics; +static inline +int rename_table_in_stat_tables(THD *thd, const char *db, const char *tab, + const char *new_db, const char *new_tab) +{ + LEX_STRING od= { const_cast(db), strlen(db) }; + LEX_STRING ot= { const_cast(tab), strlen(tab) }; + LEX_STRING nd= { const_cast(new_db), strlen(new_db) }; + LEX_STRING nt= { const_cast(new_tab), strlen(new_tab) }; + return rename_table_in_stat_tables(thd, &od, &ot, &nd, &nt); +} + /* Statistical data on a table */ diff --git a/sql/sql_table.cc b/sql/sql_table.cc index e62786945f8..d58a76903a2 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -5432,6 +5432,27 @@ static bool fill_alter_inplace_info(THD *thd, case IS_EQUAL_NO: /* New column type is incompatible with old one. */ ha_alter_info->handler_flags|= Alter_inplace_info::ALTER_COLUMN_TYPE; + if (table->s->tmp_table == NO_TMP_TABLE) + { + delete_statistics_for_column(thd, table, field); + KEY *key_info= table->key_info; + for (uint i=0; i < table->s->keys; i++, key_info++) + { + if (field->part_of_key.is_set(i)) + { + uint key_parts= table->actual_n_key_parts(key_info); + for (uint j= 0; j < key_parts; j++) + { + if (key_info->key_part[j].fieldnr-1 == field->field_index) + { + delete_statistics_for_index(thd, table, key_info, + j >= key_info->user_defined_key_parts); + break; + } + } + } + } + } break; case IS_EQUAL_YES: /* @@ -5464,6 +5485,8 @@ static bool fill_alter_inplace_info(THD *thd, { field->flags|= FIELD_IS_RENAMED; ha_alter_info->handler_flags|= Alter_inplace_info::ALTER_COLUMN_NAME; + rename_column_in_stat_tables(thd, table, field, + new_field->field_name); } /* Check that NULL behavior is same for old and new fields */ @@ -6352,6 +6375,8 @@ static bool mysql_inplace_alter_table(THD *thd, alter_ctx->db, alter_ctx->alias, 0); DBUG_RETURN(true); } + rename_table_in_stat_tables(thd, alter_ctx->db,alter_ctx->alias, + alter_ctx->new_db, alter_ctx->new_alias); } DBUG_RETURN(false); @@ -8205,21 +8230,25 @@ bool mysql_alter_table(THD *thd,char *new_db, char *new_name, } // Check if we renamed the table and if so update trigger files. - if (alter_ctx.is_table_renamed() && - Table_triggers_list::change_table_name(thd, - alter_ctx.db, - alter_ctx.alias, - alter_ctx.table_name, - alter_ctx.new_db, - alter_ctx.new_alias)) + if (alter_ctx.is_table_renamed()) { - // Rename succeeded, delete the new table. - (void) quick_rm_table(thd, new_db_type, - alter_ctx.new_db, alter_ctx.new_alias, 0); - // Restore the backup of the original table to the old name. - (void) mysql_rename_table(old_db_type, alter_ctx.db, backup_name, - alter_ctx.db, alter_ctx.alias, FN_FROM_IS_TMP); - goto err_with_mdl; + if (Table_triggers_list::change_table_name(thd, + alter_ctx.db, + alter_ctx.alias, + alter_ctx.table_name, + alter_ctx.new_db, + alter_ctx.new_alias)) + { + // Rename succeeded, delete the new table. + (void) quick_rm_table(thd, new_db_type, + alter_ctx.new_db, alter_ctx.new_alias, 0); + // Restore the backup of the original table to the old name. + (void) mysql_rename_table(old_db_type, alter_ctx.db, backup_name, + alter_ctx.db, alter_ctx.alias, FN_FROM_IS_TMP); + goto err_with_mdl; + } + rename_table_in_stat_tables(thd, alter_ctx.db,alter_ctx.alias, + alter_ctx.new_db, alter_ctx.new_alias); } // ALTER TABLE succeeded, delete the backup of the old table.