From 7215fe789480c228a91f50ff4f047ea89c16f041 Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Thu, 17 Jul 2025 15:15:36 +1000 Subject: [PATCH] MDEV-37252 Do not check is_key_used in get_index_for_order This allows UPDATE to use ORDER BY indexes in the update columns. --- mysql-test/main/explain_non_select.result | 2 +- mysql-test/main/update.result | 4 ++-- sql/sql_select.cc | 3 +-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/mysql-test/main/explain_non_select.result b/mysql-test/main/explain_non_select.result index ca8aa246c98..aa3c4787dc7 100644 --- a/mysql-test/main/explain_non_select.result +++ b/mysql-test/main/explain_non_select.result @@ -72,7 +72,7 @@ id select_type table type possible_keys key key_len ref rows Extra # This should use an index, possible_keys=NULL because there is no WHERE explain update t1 set a=a+1 order by a limit 2; id select_type table type possible_keys key key_len ref rows Extra -1 SIMPLE t1 ALL NULL NULL NULL NULL 512 Using filesort +1 SIMPLE t1 index NULL a 5 NULL 2 Using buffer # This should use range, possible_keys={a,b} explain update t1 set filler='fooo' where a<20 and b < 10; id select_type table type possible_keys key key_len ref rows Extra diff --git a/mysql-test/main/update.result b/mysql-test/main/update.result index 57814754e8e..4e5287ab6c8 100644 --- a/mysql-test/main/update.result +++ b/mysql-test/main/update.result @@ -288,7 +288,7 @@ update t1 set a=9999 order by a limit 1; update t1 set b=9999 order by a limit 1; show status like 'handler_read%'; Variable_name Value -Handler_read_first 1 +Handler_read_first 2 Handler_read_key 0 Handler_read_last 0 Handler_read_next 0 @@ -296,7 +296,7 @@ Handler_read_prev 0 Handler_read_retry 0 Handler_read_rnd 2 Handler_read_rnd_deleted 0 -Handler_read_rnd_next 9 +Handler_read_rnd_next 0 flush status; delete from t1 order by a limit 1; show status like 'handler_read%'; diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 5223150ca97..23da54feae0 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -33411,8 +33411,7 @@ uint get_index_for_order(ORDER *order, TABLE *table, SQL_SELECT *select, double new_cost; if (test_if_cheaper_ordering(FALSE, NULL, order, table, table->keys_in_use_for_order_by, -1, limit, - &key, &direction, &limit, &new_cost) && - !is_key_used(table, key, table->write_set)) + &key, &direction, &limit, &new_cost)) { *need_sort= FALSE; *scanned_limit= limit;