mirror of
https://github.com/MariaDB/server.git
synced 2025-08-08 11:22:35 +03:00
MDEV-35180: ref_to_range rewrite causes poor query plan
(Variant 2: only allow rewrite for ref(const)) make_join_select() has a "ref_to_range" rewrite: it would rewrite any ref access to a range access on the same index if the latter uses more keyparts. It seems, he initial intent of this was to fix poor query plan choice in cases like t.keypart1=const AND t.keypart2 < 'foo' Due to deficiency in cost model, ref access could be picked while range would enumerate fewer rows and be cheaper. However, the condition also forces a rewrite in cases like: t.keypart1=prev_table.col AND t.keypart1<='foo' AND t.keypart2<'bar' Here, it can be that * keypart1=prev_table.col is highly selective * (keypart1, keypart2) <= ('foo', 'bar') is not at all selective. Still, the rewrite would be made and poor query plan chosen. Fixed this by only doing the rewrite if ref access was ref(const) so we can be certain that quick select also used these restrictions and will scan a subset of rows that ref access would scan.
This commit is contained in:
@@ -12969,11 +12969,41 @@ make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
|
||||
|
||||
used_tables|=current_map;
|
||||
|
||||
/*
|
||||
Change from using ref access to using quick select on the same index
|
||||
if the quick select uses more key parts.
|
||||
|
||||
There are two cases.
|
||||
A. ref access is ref(const). quick select was also constructed using
|
||||
equality restrictions that ref used, and so it will scan a subset of
|
||||
rows that ref access scans.
|
||||
Example: suppose the index is INDEX(kp1, kp2) and the WHERE has:
|
||||
|
||||
kp1='foo' and kp2 <= 10
|
||||
|
||||
here, ref access will use kp1='foo' and quick select will use
|
||||
(foo) <= (kp1,kp2) <=(foo,10)
|
||||
|
||||
B. ref access is not constant. In this case, quick select was
|
||||
constructed from some other restriction and in general will scan
|
||||
totally different set of rows (it maybe larger or smaller).
|
||||
Example: for INDEX(kp1, kp2) and the WHERE:
|
||||
|
||||
kp1 <='foo' and kp1=prev_table.col and kp2 <= 10
|
||||
|
||||
the ref access will use kp1=prev_table.col, while quick select will
|
||||
use (-inf) < (kp1, kp2) <= ('foo',10).
|
||||
|
||||
Because of the above, we perform the rewrite ONLY when ref is
|
||||
ref(const).
|
||||
*/
|
||||
if (tab->type == JT_REF && tab->quick &&
|
||||
(((uint) tab->ref.key == tab->quick->index &&
|
||||
tab->ref.key_length < tab->quick->max_used_key_length) ||
|
||||
(!is_hash_join_key_no(tab->ref.key) &&
|
||||
tab->table->intersect_keys.is_set(tab->ref.key))))
|
||||
tab->table->intersect_keys.is_set(tab->ref.key))) &&
|
||||
tab->ref.const_ref_part_map == // (ref-is-const)
|
||||
make_prev_keypart_map(tab->ref.key_parts)) // (ref-is-const)
|
||||
{
|
||||
/* Range uses longer key; Use this instead of ref on key */
|
||||
Json_writer_object ref_to_range(thd);
|
||||
|
Reference in New Issue
Block a user