mysql index hint 在index不存在时的处理
关于index_hint
在mysql查询语句中可以通过指定index_hint来告诉优化器如何使用索引,详细可以参考这里
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)。可以看下面的例子
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重新发布应用。这两个做法都不太友好。
改进
如果index_hint指定的索引不存在,服务器不返回错误,而是选择报warining,那么删除这个索引就不会导致应用报错。
改进:
新增sql_mode的类型INDEX_HINE_ERROR;
当sql_mode设置了INDEX_HINE_ERROR类型,如果index_hint指定的索引不存在,服务器返回错误。
当sql_mode没有设置INDEX_HINE_ERROR类型,如果index_hint指定的索引不存在,服务器不返回错误,而是选择报warining
看下面的例子:
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'
mysql index hint 在index不存在时的处理的更多相关文章
- Oracle index hint syntax
Question: I added an index hint in my query, but the hint is being ignored. What is the correct sy ...
- MySQL中的索引提示Index Hint
MySQL数据库支持索引提示(INDEX HINT)显式的高速优化器使用了哪个索引.以下是可能需要用到INDEX HINT的情况 a)MySQL数据库的优化器错误的选择了某个索引,导致SQL运行很慢. ...
- MySQL 优化之 ICP (index condition pushdown:索引条件下推)
ICP技术是在MySQL5.6中引入的一种索引优化技术.它能减少在使用 二级索引 过滤where条件时的回表次数 和 减少MySQL server层和引擎层的交互次数.在索引组织表中,使用二级索引进行 ...
- 无语的index hint:手工分配哈希区,5小时不出结果,优化后20分钟
同事说,有个语句5个小时不出结果,叫我帮忙看看,于是叫同事发过来.不看不知道,一看吓一跳,3个表关联,强制使用了2个index hint,当中一个表9g,一个表67g,另一个小表40Mb.开发者,总以 ...
- Mysql中Key与Index的区别
mysql的key和index多少有点令人迷惑,这实际上考察对数据库体系结构的了解的. 1 key 是数据库的物理结构,它包含两层意义,一是约束(偏重于约束和规范数据库的结构完整性),二是索引(辅助查 ...
- MySQL 执行计划中Extra(Using where,Using index,Using index condition,Using index,Using where)的浅析
关于如何理解MySQL执行计划中Extra列的Using where.Using Index.Using index condition,Using index,Using where这四者的区别 ...
- MySQL: Building the best INDEX for a given SELECT
Table of Contents The ProblemAlgorithmDigressionFirst, some examplesAlgorithm, Step 1 (WHERE "c ...
- hint指定index的深入理解
http://czmmiao.iteye.com/blog/1480247创建一个表,含有位图index和b-tree index SQL> create table t as select o ...
- mysql 索引查询 、创建 create index 与 add index 的区别
1.索引查询 ------TABLE_SCHEMA 库名:TABLE 表名 ------AND UPPER(INDEX_NAME) != 'PRIMARY' 只查询索引,不需要主键 SELECT ...
随机推荐
- 这个拖后腿的“in”
问题之源 C# 7.2推出了全新的参数修饰符in,据说是能提升一定的性能,官方MSDN文档描述是: Add the in modifier to pass an argument by referen ...
- SimpleVisitorMemberType类的visitClassType解读
举个例子,如下: class CA<T>{ public T getVal(){ return null; } } interface IA{} interface IB{} public ...
- Gen类的字符串操作
public void t(String d){ final String str = "b"; String s = "a"+"c"+st ...
- 逆向工程-对native层的一次简单逆向实践
关注一款app很久了,这款app为了防止别人逆向破解拉取数据做了很多工作: 防止别人修改apk包,执行关键动作时对dex文件进行md5验证: 防止用户调用接口批量拉数据,对返回的web网页里个人信息进 ...
- es6学习笔记6--Generator 函数
基本概念 Generator函数有多种理解角度.从语法上,首先可以把它理解成,Generator函数是一个状态机,封装了多个内部状态. 执行Generator函数会返回一个遍历器对象,也就是说,Gen ...
- 读jQuery源码释疑笔记3
1.在jQuery.fn=jQuery.prototype中定义了方法:init, map, each , toArray, get, pushStack, ready, slice,first ...
- map映照容器(常用的使用方法总结)
map映照容器的数据元素是由一个键值和一个映照数据组成的,键值和映照数据之间具有一一对应的关系.map与set集合容器一样,不允许插入的元素的键值重复. /*关于C++STL中map映照容器的学习,看 ...
- 微信公众号支付回调页面处理asp.net
1.在商家微信商户通中配置回调url 2.在提交订单时传入的回调页面中获取支付成功后或支付失败后的参数,对订单进行处理 public partial class gzpayCallback : Sys ...
- [转]Windows 下 Nginx+IIS 使用
本文转自:https://blog.csdn.net/chihen/article/details/52698594 Windows 下 Nginx+IIS 使用 一.Nginx简介 Nginx (& ...
- 36.Linux驱动调试-根据oops定位错误代码行
1.当驱动有误时,比如,访问的内存地址是非法的,便会打印一大串的oops出来 1.1以LED驱动为例 将open()函数里的ioremap()屏蔽掉,直接使用物理地址的GPIOF,如下图所示: 1.2 ...