From 68fb755c8d48350ef5d62ca31e7a3355be60bf63 Mon Sep 17 00:00:00 2001 From: hcduan Date: Tue, 19 Nov 2024 20:37:44 +0800 Subject: [PATCH] [bugfix] Incorrect last_update time of innodb_index_stats Problem: ======== When adding a new index, the statistics for the new index are triggered, generating a new index statistics entry. At this point, other indexes have not been triggered to update statistical information. However, the last update time for all existing indexes in the table's index statistics is also being refreshed, which can be misleading when troubleshooting issues. Solution: ========= Fix the code bug to ensure that updating one index does not affect the statistics of other indexes. --- mysql-test/r/index_last_update_time.result | 8 ++++++++ mysql-test/t/index_last_update_time.test | 18 ++++++++++++++++++ storage/innobase/dict/dict0stats.cc | 4 ++-- 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 mysql-test/r/index_last_update_time.result create mode 100644 mysql-test/t/index_last_update_time.test diff --git a/mysql-test/r/index_last_update_time.result b/mysql-test/r/index_last_update_time.result new file mode 100644 index 00000000000..e6ed3757139 --- /dev/null +++ b/mysql-test/r/index_last_update_time.result @@ -0,0 +1,8 @@ +create table t1 (c1 int primary key,c2 int ,c3 int, index idx1(c2)); +insert into t1 values (1,1,1),(2,2,2),(3,3,3),(4,4,4); +analyze table t1; +Table Op Msg_type Msg_text +test.t1 analyze status OK +alter table t1 add key idx2(c3); +include/assert.inc [current last_update equal to the value saved before.] +drop table t1; diff --git a/mysql-test/t/index_last_update_time.test b/mysql-test/t/index_last_update_time.test new file mode 100644 index 00000000000..4e6f469eb4e --- /dev/null +++ b/mysql-test/t/index_last_update_time.test @@ -0,0 +1,18 @@ +create table t1 (c1 int primary key,c2 int ,c3 int, index idx1(c2)); + +insert into t1 values (1,1,1),(2,2,2),(3,3,3),(4,4,4); + +analyze table t1; + +# Save value of 'last_update_time' before alter new index +--let $last_update_time= `select last_update from mysql.innodb_index_stats where table_name = 't1' and index_name = 'PRIMARY' and stat_name = 'n_diff_pfx01'` + +sleep 2; + +alter table t1 add key idx2(c3); + +--let $assert_text= current last_update equal to the value saved before. +--let $assert_cond= [select count(*) from mysql.innodb_index_stats where table_name = "t1" and index_name = "PRIMARY" and stat_name = "n_diff_pfx01" and last_update = "$last_update_time"] = 1 +--source include/assert.inc + +drop table t1; \ No newline at end of file diff --git a/storage/innobase/dict/dict0stats.cc b/storage/innobase/dict/dict0stats.cc index b8c2b6b0d9a..b9522c27e0e 100644 --- a/storage/innobase/dict/dict0stats.cc +++ b/storage/innobase/dict/dict0stats.cc @@ -2560,8 +2560,8 @@ dberr_t dict_stats_save(dict_table_t *table_orig, index = it->second; if (only_for_index != nullptr && - index->space != only_for_index->m_space_id && - index->id != only_for_index->m_index_id) { + (index->space != only_for_index->m_space_id || + index->id != only_for_index->m_index_id)) { continue; } -- 2.30.1