Commit cdeaefee authored by Yuchen Pei's avatar Yuchen Pei

MDEV-27419 [demo/check-ci] choose desc key for desc ordering

A key may be chosen in test_if_skip_sort_order(), after calling
test_if_cheaper_ordering(). The latter iterates over the keys, and
call test_if_order_by_key() to determine whether the key can be used
in ORDER BY.

We make it so that if the result of test_if_order_by_key() indicates
the current key under consideration would result in a better matching
ordering (e.g. ORDER BY DESC matches a desc key better than an asc
key), then we choose that key, given that the range_cost is the same.

TODO: consider whether it is possible to quantify the order matching
by passing direction to get_range_limit_read_cost.
parent 44af9bfc
create table t (a int, key aa(a), key ad(a desc)) engine=InnoDB;
insert into t values (1),(5),(2),(8),(4),(6),(7),(9),(3);
explain select * from t order by a desc;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t index NULL ad 5 NULL 9 Using index
flush status;
select * from t order by a desc;
a
9
8
7
6
5
4
3
2
1
show status like 'Handler_read%';
Variable_name Value
Handler_read_first 1
Handler_read_key 0
Handler_read_last 0
Handler_read_next 9
Handler_read_prev 0
Handler_read_retry 0
Handler_read_rnd 0
Handler_read_rnd_deleted 0
Handler_read_rnd_next 0
drop table t;
# Remove the include if you run the test on MySQL 8.0
--source include/have_innodb.inc
create table t (a int, key aa(a), key ad(a desc)) engine=InnoDB;
insert into t values (1),(5),(2),(8),(4),(6),(7),(9),(3);
explain select * from t order by a desc;
flush status;
select * from t order by a desc;
show status like 'Handler_read%';
# Cleanup
drop table t;
......@@ -32369,11 +32369,10 @@ test_if_cheaper_ordering(const JOIN_TAB *tab, ORDER *order, TABLE *table,
/*
We will try use the key if:
- If there is no ref key and no usable keys has yet been found and
there is either a group by or a FORCE_INDEX
- If the new cost is better than read_time
*/
if (range_cost < read_time)
if (range_cost < read_time ||
(range_cost == read_time && direction > best_key_direction))
{
read_time= range_cost;
possible_key.add("chosen", true);
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment