Skip to content

Commit

Permalink
Fixed some issues with FORCE INDEX
Browse files Browse the repository at this point in the history
Added code to support that force index can be used to force an index scan
instead of a full table scan. Currently this code is disable but I added
a test to verify that things works if the code is ever enabled.

Other things:

- FORCE INDEX will now work with "Range checked for each record" and
  join cache (see main/type_time_6065)
- Removed code ifdef with BAD_OPTIMIZATION (New cost calculations should
  fix this).
- Removed TABLE_LIST->force_index and comment that it should be removed
- Added TABLE->force_index_join and use in the corresponding places.
  This means that FORCE INDEX FOR ORDER BY will not affect keys used
  in joins anymore.
  Remove TODO that the above should be added.
  I still kept TABLE->force_index as it's used in
  test_if_cheaper_ordering() and opt_range.cc
- Removed setting table->force_index when calling test_quick_select() as
  it's not needed (force_index is an argument to test_quick_select())
  • Loading branch information
montywi authored and spetrunia committed Feb 2, 2023
1 parent 013ba37 commit 33fc803
Show file tree
Hide file tree
Showing 9 changed files with 236 additions and 105 deletions.
41 changes: 40 additions & 1 deletion mysql-test/main/key.result
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index NULL a 5 NULL 6
SHOW STATUS LIKE 'Last_query_cost';
Variable_name Value
Last_query_cost 11.003515
Last_query_cost 8.506592
DROP TABLE t1;
#
# MDEV-21480: Unique key using ref access though eq_ref access can be used
Expand Down Expand Up @@ -692,3 +692,42 @@ drop table t1,t2;
#
create table t1 (a int, b int, key(a), key(a desc));
drop table t1;
# Check some issues with FORCE INDEX and full index scans
# (Does FORCE INDEX force an index scan)
#
create table t1 (a int primary key, b int, c int, d int,
key k1 (b) using BTREE, key k2 (c,d) using btree) engine=heap;
insert into t1 select seq as a, seq as b, seq as c, seq as d
from seq_1_to_100;
explain select sum(a+b) from t1 force index (k1) where b>0 and a=99;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 range k1 k1 5 NULL 100 Using where
explain select sum(a+b) from t1 force index (k1) where a>0;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 100 Using where
explain select sum(a+b) from t1 force index (k1);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 100
explain select sum(a+b) from t1 force index for join (k1);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 100
explain select sum(a+b) from t1 force index for order by (k1);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 100
explain select sum(a+b) from t1 force index (k1,k2);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 100
select sum(a+b) from t1 force index (k1);
sum(a+b)
10100
explain select sum(a+b) from t1 force index (primary);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 100
select sum(a+b) from t1 force index (primary);
sum(a+b)
10100
explain select straight_join sum(a+b) from seq_1_to_10 as s, t1 force index (k2) where t1.a=s.seq;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE s index PRIMARY PRIMARY 8 NULL 10 Using index
1 SIMPLE t1 ALL NULL NULL NULL NULL 100 Using where; Using join buffer (flat, BNL join)
drop table t1;
20 changes: 20 additions & 0 deletions mysql-test/main/key.test
Original file line number Diff line number Diff line change
Expand Up @@ -611,3 +611,23 @@ drop table t1,t2;
--echo #
create table t1 (a int, b int, key(a), key(a desc));
drop table t1;

--echo # Check some issues with FORCE INDEX and full index scans
--echo # (Does FORCE INDEX force an index scan)
--echo #

create table t1 (a int primary key, b int, c int, d int,
key k1 (b) using BTREE, key k2 (c,d) using btree) engine=heap;
insert into t1 select seq as a, seq as b, seq as c, seq as d
from seq_1_to_100;
explain select sum(a+b) from t1 force index (k1) where b>0 and a=99;
explain select sum(a+b) from t1 force index (k1) where a>0;
explain select sum(a+b) from t1 force index (k1);
explain select sum(a+b) from t1 force index for join (k1);
explain select sum(a+b) from t1 force index for order by (k1);
explain select sum(a+b) from t1 force index (k1,k2);
select sum(a+b) from t1 force index (k1);
explain select sum(a+b) from t1 force index (primary);
select sum(a+b) from t1 force index (primary);
explain select straight_join sum(a+b) from seq_1_to_10 as s, t1 force index (k2) where t1.a=s.seq;
drop table t1;
64 changes: 32 additions & 32 deletions mysql-test/main/type_time_6065.result

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sql/sql_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ inline void setup_table_map(TABLE *table, TABLE_LIST *table_list, uint tablenr)
}
table->tablenr= tablenr;
table->map= (table_map) 1 << tablenr;
table->force_index= table_list->force_index;
table->force_index= table->force_index_join= 0;
table->force_index_order= table->force_index_group= 0;
table->covering_keys= table->s->keys_for_keyread;
}
Expand Down
2 changes: 0 additions & 2 deletions sql/sql_parse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8214,8 +8214,6 @@ TABLE_LIST *st_select_lex::add_table_to_list(THD *thd,
ptr->mdl_type= mdl_type;
ptr->table_options= table_options;
ptr->updating= MY_TEST(table_options & TL_OPTION_UPDATING);
/* TODO: remove TL_OPTION_FORCE_INDEX as it looks like it's not used */
ptr->force_index= MY_TEST(table_options & TL_OPTION_FORCE_INDEX);
ptr->ignore_leaves= MY_TEST(table_options & TL_OPTION_IGNORE_LEAVES);
ptr->sequence= MY_TEST(table_options & TL_OPTION_SEQUENCE);
ptr->derived= table->sel;
Expand Down
Loading

0 comments on commit 33fc803

Please sign in to comment.