mysql index hint 在index不存在时的处理

关于index_hint

   在mysql查询语句中能够经过指定index_hint来告诉优化器如何使用索引,详细能够参考这里html

index_hint:
    USE {INDEX|KEY}
      [FOR {JOIN|ORDER BY|GROUP BY}] ([index_list])
  | IGNORE {INDEX|KEY}
      [FOR {JOIN|ORDER BY|GROUP BY}] (index_list)
  | FORCE {INDEX|KEY}
      [FOR {JOIN|ORDER BY|GROUP BY}] (index_list)

 

问题

  查询语句中使用index_hint时,若是index_hint指定的索引不存在,那么服务器会返回错误ERROR 1176 (42000)。能够看下面的例子mysql

drop table t1;
create table t1(id int auto_increment, a char(2), primary key (id), key idx1(a)) engine=innodb;
insert into t1 values (1,'ab');
select * from t1 force index (idx1) where a='ab';
+----+------+
| id | a    |
+----+------+
|  1 | ab   |
+----+------+
1 row in set (0.00 sec)
alter table t1 drop key idx1;
select * from t1 force index (idx1) where a='ab';
ERROR 1176 (42000): Key 'idx1' doesn't exist in table 't1'

    在实际应用中,若是查询语句使用index_hint指定了索引,随着业务的发展,这个索引能变得多余了,但删除这个索引会致使应用报错。因而要么保留着个无用的索引,要么修改sql从新发布应用。这两个作法都不太友好。sql

改进

  若是index_hint指定的索引不存在,服务器不返回错误,而是选择报warining,那么删除这个索引就不会致使应用报错。服务器

  改进:优化

    新增sql_mode的类型INDEX_HINE_ERROR;spa

    当sql_mode设置了INDEX_HINE_ERROR类型,若是index_hint指定的索引不存在,服务器返回错误。code

    当sql_mode没有设置INDEX_HINE_ERROR类型,若是index_hint指定的索引不存在,服务器不返回错误,而是选择报warininghtm

  看下面的例子:blog

 1)sql_mode没有设置INDEX_HINE_ERROR类型索引

set sql_mode='';
select * from t1 force index (idx1) where a='ab';
+----+------+
| id | a    |
+----+------+
|  1 | ab   |
+----+------+
1 row in set, 1 warning (0.00 sec)
show warnings;
+---------+------+----------------------------------------+
| Level   | Code | Message                                |
+---------+------+----------------------------------------+
| Warning | 1176 | Key 'idx1' doesn't exist in table 't1' |
+---------+------+----------------------------------------+
1 row in set (0.00 sec)
explain select * from t1 force index (idx1) where a='ab';
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | t1    | ALL  | NULL          | NULL | NULL    | NULL |    1 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
1 row in set, 1 warning (0.00 sec)
show warnings;
+---------+------+----------------------------------------+
| Level   | Code | Message                                |
+---------+------+----------------------------------------+
| Warning | 1176 | Key 'idx1' doesn't exist in table 't1' |
+---------+------+----------------------------------------+
1 row in set (0.00 sec)

 

 2)sql_mode设置了INDEX_HINE_ERROR类型

set sql_mode='index_hint_error';
select * from t1 force index (idx1) where a='ab';
ERROR 1176 (42000): Key 'idx1' doesn't exist in table 't1'
explain select * from t1 force index (idx1) where a='ab';
ERROR 1176 (42000): Key 'idx1' doesn't exist in table 't1'
相关文章
相关标签/搜索