Commit 4e968b1e authored by Michael Widenius's avatar Michael Widenius

Fix based on suggestion by Zardosht Kasheff and Richard Prohaska) for making...

Fix based on suggestion by Zardosht Kasheff and Richard Prohaska) for making all clustered indexes equal in test_if_skip_sort_order()


sql/sql_select.cc:
  - Threat all clustered indexes equal in test_if_skip_sort_order().
    This is a temporary fix as the current code doesn't do proper cost analyizes for which index to use.
    I will address this later as the change required is not trivial.
  - Fixed a bug where select_limit was changed in the loop, which made different indexes see diferent values of select_limit
parent 36311464
...@@ -16310,7 +16310,7 @@ find_field_in_item_list (Field *field, void *data) ...@@ -16310,7 +16310,7 @@ find_field_in_item_list (Field *field, void *data)
*/ */
static bool static bool
test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit, test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit_arg,
bool no_changes, const key_map *map) bool no_changes, const key_map *map)
{ {
int ref_key; int ref_key;
...@@ -16322,8 +16322,11 @@ test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit, ...@@ -16322,8 +16322,11 @@ test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit,
key_map usable_keys; key_map usable_keys;
QUICK_SELECT_I *save_quick= select ? select->quick : 0; QUICK_SELECT_I *save_quick= select ? select->quick : 0;
int best_key= -1; int best_key= -1;
ha_rows best_select_limit;
DBUG_ENTER("test_if_skip_sort_order"); DBUG_ENTER("test_if_skip_sort_order");
LINT_INIT(ref_key_parts); LINT_INIT(ref_key_parts);
LINT_INIT(best_select_limit);
/* /*
Keys disabled by ALTER TABLE ... DISABLE KEYS should have already Keys disabled by ALTER TABLE ... DISABLE KEYS should have already
...@@ -16488,7 +16491,7 @@ test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit, ...@@ -16488,7 +16491,7 @@ test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit,
resolved with a key; This is because filesort() is usually faster than resolved with a key; This is because filesort() is usually faster than
retrieving all rows through an index. retrieving all rows through an index.
*/ */
if (select_limit >= table_records) if (select_limit_arg >= table_records)
{ {
keys= *table->file->keys_to_use_for_scanning(); keys= *table->file->keys_to_use_for_scanning();
keys.merge(table->covering_keys); keys.merge(table->covering_keys);
...@@ -16516,6 +16519,7 @@ test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit, ...@@ -16516,6 +16519,7 @@ test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit,
for (nr=0; nr < table->s->keys ; nr++) for (nr=0; nr < table->s->keys ; nr++)
{ {
int direction; int direction;
ha_rows select_limit= select_limit_arg;
if (keys.is_set(nr) && if (keys.is_set(nr) &&
(direction= test_if_order_by_key(order, table, nr, &used_key_parts))) (direction= test_if_order_by_key(order, table, nr, &used_key_parts)))
...@@ -16608,7 +16612,8 @@ test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit, ...@@ -16608,7 +16612,8 @@ test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit,
select_limit= table_records; select_limit= table_records;
else else
select_limit= (ha_rows) (select_limit*rec_per_key); select_limit= (ha_rows) (select_limit*rec_per_key);
} } /* group */
/* /*
If tab=tk is not the last joined table tn then to get first If tab=tk is not the last joined table tn then to get first
L records from the result set we can expect to retrieve L records from the result set we can expect to retrieve
...@@ -16652,8 +16657,7 @@ test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit, ...@@ -16652,8 +16657,7 @@ test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit,
*/ */
index_scan_time= select_limit/rec_per_key * index_scan_time= select_limit/rec_per_key *
min(rec_per_key, table->file->scan_time()); min(rec_per_key, table->file->scan_time());
if ((ref_key < 0 && is_covering) || if ((ref_key < 0 && (group || table->force_index || is_covering)) ||
(ref_key < 0 && (group || table->force_index)) ||
index_scan_time < read_time) index_scan_time < read_time)
{ {
ha_rows quick_records= table_records; ha_rows quick_records= table_records;
...@@ -16665,7 +16669,8 @@ test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit, ...@@ -16665,7 +16669,8 @@ test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit,
if (best_key < 0 || if (best_key < 0 ||
(select_limit <= min(quick_records,best_records) ? (select_limit <= min(quick_records,best_records) ?
keyinfo->key_parts < best_key_parts : keyinfo->key_parts < best_key_parts :
quick_records < best_records)) quick_records < best_records) ||
(!is_best_covering && is_covering))
{ {
best_key= nr; best_key= nr;
best_key_parts= keyinfo->key_parts; best_key_parts= keyinfo->key_parts;
...@@ -16673,6 +16678,7 @@ test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit, ...@@ -16673,6 +16678,7 @@ test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit,
best_records= quick_records; best_records= quick_records;
is_best_covering= is_covering; is_best_covering= is_covering;
best_key_direction= direction; best_key_direction= direction;
best_select_limit= select_limit;
} }
} }
} }
...@@ -16685,7 +16691,7 @@ test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit, ...@@ -16685,7 +16691,7 @@ test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit,
index is clustered key. index is clustered key.
*/ */
if (best_key < 0 || if (best_key < 0 ||
((select_limit >= table_records) && ((select_limit_arg >= table_records) &&
(tab->type == JT_ALL && (tab->type == JT_ALL &&
tab->join->tables > tab->join->const_tables + 1) && tab->join->tables > tab->join->const_tables + 1) &&
!(table->file->index_flags(best_key, 0, 1) & HA_CLUSTERED_INDEX))) !(table->file->index_flags(best_key, 0, 1) & HA_CLUSTERED_INDEX)))
...@@ -16788,8 +16794,8 @@ test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit, ...@@ -16788,8 +16794,8 @@ test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit,
{ {
tab->ref.key= -1; tab->ref.key= -1;
tab->ref.key_parts= 0; tab->ref.key_parts= 0;
if (select_limit < table->file->stats.records) if (best_select_limit < table->file->stats.records)
tab->limit= select_limit; tab->limit= best_select_limit;
} }
} }
else if (tab->type != JT_ALL) else if (tab->type != JT_ALL)
......
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