From 9020baf126c7d4068b40c57bc2d7dcb747b4b341 Mon Sep 17 00:00:00 2001 From: Sergei Petrunia Date: Sat, 24 Aug 2024 19:01:06 +0300 Subject: [PATCH] Trivial fix: Make test_if_cheaper_ordering() use actual_rec_per_key() Discovered this while working on MDEV-34720: test_if_cheaper_ordering() uses rec_per_key, while the original estimate for the access method is produced in best_access_path() by using actual_rec_per_key(). Make test_if_cheaper_ordering() also use actual_rec_per_key(). Also make several getter function "const" to make this compile. Also adjusted the testcase to handle this (the change backported from 11.0) --- mysql-test/main/subselect_innodb.result | 1 + mysql-test/main/subselect_innodb.test | 2 ++ sql/sql_select.cc | 3 ++- sql/sql_statistics.h | 2 +- sql/structs.h | 2 +- sql/table.cc | 2 +- 6 files changed, 8 insertions(+), 4 deletions(-) diff --git a/mysql-test/main/subselect_innodb.result b/mysql-test/main/subselect_innodb.result index ea0affd575f..c8c0fd693f2 100644 --- a/mysql-test/main/subselect_innodb.result +++ b/mysql-test/main/subselect_innodb.result @@ -554,6 +554,7 @@ id select_type table type possible_keys key key_len ref rows Extra # # MDEV-6081: ORDER BY+ref(const): selectivity is very incorrect (MySQL Bug#14338686) # +insert into t2 select seq,seq,seq from seq_10000_to_11000; alter table t2 add key2 int; update t2 set key2=key1; alter table t2 add key(key2); diff --git a/mysql-test/main/subselect_innodb.test b/mysql-test/main/subselect_innodb.test index f675dda91b4..12ce5cabbeb 100644 --- a/mysql-test/main/subselect_innodb.test +++ b/mysql-test/main/subselect_innodb.test @@ -558,7 +558,9 @@ from --echo # --echo # MDEV-6081: ORDER BY+ref(const): selectivity is very incorrect (MySQL Bug#14338686) --echo # +--source include/have_sequence.inc +insert into t2 select seq,seq,seq from seq_10000_to_11000; alter table t2 add key2 int; update t2 set key2=key1; alter table t2 add key(key2); diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 176310224a2..d8958041160 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -30391,7 +30391,8 @@ test_if_cheaper_ordering(const JOIN_TAB *tab, ORDER *order, TABLE *table, else { const KEY *ref_keyinfo= table->key_info + ref_key; - refkey_rows_estimate= ref_keyinfo->rec_per_key[tab->ref.key_parts - 1]; + refkey_rows_estimate= + (ha_rows)ref_keyinfo->actual_rec_per_key(tab->ref.key_parts - 1); } set_if_bigger(refkey_rows_estimate, 1); } diff --git a/sql/sql_statistics.h b/sql/sql_statistics.h index 166e7a75c79..6d7dd3618ff 100644 --- a/sql/sql_statistics.h +++ b/sql/sql_statistics.h @@ -472,7 +472,7 @@ public: bool avg_frequency_is_inited() { return avg_frequency != NULL; } - double get_avg_frequency(uint i) + double get_avg_frequency(uint i) const { return (double) avg_frequency[i] / Scale_factor_avg_frequency; } diff --git a/sql/structs.h b/sql/structs.h index 1641b8b3cbd..76ae013ca3b 100644 --- a/sql/structs.h +++ b/sql/structs.h @@ -166,7 +166,7 @@ typedef struct st_key { engine_option_value *option_list; ha_index_option_struct *option_struct; /* structure with parsed options */ - double actual_rec_per_key(uint i); + double actual_rec_per_key(uint i) const; bool without_overlaps; /* diff --git a/sql/table.cc b/sql/table.cc index 373c70c5983..60345ac5a21 100644 --- a/sql/table.cc +++ b/sql/table.cc @@ -10034,7 +10034,7 @@ uint TABLE_SHARE::actual_n_key_parts(THD *thd) } -double KEY::actual_rec_per_key(uint i) +double KEY::actual_rec_per_key(uint i) const { if (rec_per_key == 0) return 0;