mirror of
https://github.com/MariaDB/server.git
synced 2025-07-29 05:21:33 +03:00
Bug#37830 : ORDER BY ASC/DESC - no difference
Range scan in descending order for c <= <col> <= c type of ranges was ignoring the DESC flag. However some engines like InnoDB have the primary key parts as a suffix for every secondary key. When such primary key suffix is used for ordering ignoring the DESC is not valid. But we generally would like to do this because it's faster. Fixed by performing only reverse scan if the primary key is used. Removed some dead code in the process.
This commit is contained in:
@ -12088,26 +12088,25 @@ part_of_refkey(TABLE *table,Field *field)
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
Test if one can use the key to resolve ORDER BY
|
||||
/**
|
||||
Test if a key can be used to resolve ORDER BY
|
||||
|
||||
SYNOPSIS
|
||||
test_if_order_by_key()
|
||||
order Sort order
|
||||
table Table to sort
|
||||
idx Index to check
|
||||
used_key_parts Return value for used key parts.
|
||||
used_key_parts is set to correct key parts used if return value != 0
|
||||
(On other cases, used_key_part may be changed).
|
||||
Note that the value may actually be greater than the number of index
|
||||
key parts. This can happen for storage engines that have the primary
|
||||
key parts as a suffix for every secondary key.
|
||||
|
||||
@param order Sort order
|
||||
@param table Table to sort
|
||||
@param idx Index to check
|
||||
@param[out] used_key_parts Return value for used key parts.
|
||||
|
||||
NOTES
|
||||
used_key_parts is set to correct key parts used if return value != 0
|
||||
(On other cases, used_key_part may be changed)
|
||||
|
||||
RETURN
|
||||
1 key is ok.
|
||||
0 Key can't be used
|
||||
-1 Reverse key can be used
|
||||
*****************************************************************************/
|
||||
@return indication if the key can be used for sorting
|
||||
@retval 1 key can be used for reading data in order.
|
||||
@retval 0 Key can't be used
|
||||
@retval -1 Reverse read on the key can be used
|
||||
*/
|
||||
|
||||
static int test_if_order_by_key(ORDER *order, TABLE *table, uint idx,
|
||||
uint *used_key_parts)
|
||||
@ -12172,11 +12171,27 @@ static int test_if_order_by_key(ORDER *order, TABLE *table, uint idx,
|
||||
reverse=flag; // Remember if reverse
|
||||
key_part++;
|
||||
}
|
||||
*used_key_parts= on_primary_key ? table->key_info[idx].key_parts :
|
||||
(uint) (key_part - table->key_info[idx].key_part);
|
||||
if (reverse == -1 && !(table->file->index_flags(idx, *used_key_parts-1, 1) &
|
||||
HA_READ_PREV))
|
||||
reverse= 0; // Index can't be used
|
||||
if (on_primary_key)
|
||||
{
|
||||
uint used_key_parts_secondary= table->key_info[idx].key_parts;
|
||||
uint used_key_parts_pk=
|
||||
(uint) (key_part - table->key_info[table->s->primary_key].key_part);
|
||||
*used_key_parts= used_key_parts_pk + used_key_parts_secondary;
|
||||
|
||||
if (reverse == -1 &&
|
||||
(!(table->file->index_flags(idx, used_key_parts_secondary - 1, 1) &
|
||||
HA_READ_PREV) ||
|
||||
!(table->file->index_flags(table->s->primary_key,
|
||||
used_key_parts_pk - 1, 1) & HA_READ_PREV)))
|
||||
reverse= 0; // Index can't be used
|
||||
}
|
||||
else
|
||||
{
|
||||
*used_key_parts= (uint) (key_part - table->key_info[idx].key_part);
|
||||
if (reverse == -1 &&
|
||||
!(table->file->index_flags(idx, *used_key_parts-1, 1) & HA_READ_PREV))
|
||||
reverse= 0; // Index can't be used
|
||||
}
|
||||
DBUG_RETURN(reverse);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user