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;