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 ...
随机推荐
- JVM 监控工具 jstack 和 jvisualvm 的使用
Java线程状态 线程的五种状态 * 新建:new(时间很短) * 运行:runnable * 等待:waitting(无限期等待),timed waitting(限期等待) * 阻塞:blocked ...
- Maven install [WARNING] The artifact aspectj:aspectjrt:jar:1.5.4 has been relocated to org.aspectj:aspectjrt:jar:1.5.4
一.背景 最近在给项目打包的时候,在控制台老是出现一行警告:[WARNING] The artifact aspectj:aspectjrt:jar:1.5.4 has been relocated ...
- Python学习--23 内建模块及第三方库
本文将介绍python里常用的模块.如未特殊说明,所有示例均以python3.4为例: $ python -V Python 3.4.3 网络请求 urllib urllib提供了一系列用于操作URL ...
- vmrun 批量创建vmware虚拟机
1 准备模板机 具体步骤如下: 1. 下载镜像安装系统 https://mirrors.aliyun.com/centos/7.5.1804/isos/x86_64/ 2. 安装完成配置好IP ,关闭 ...
- Chapter 3 Phenomenon——23
Charlie put one arm behind my back, not quite touching me, and led me to the glass doors of the exit ...
- 详解C#委托和事件(二)
一.当我们使用关键字delegate声明一个自定义委托类型时,实际上是声明了一个该名称的类类型,继承自抽象类System.MulticastDelegate,还包含实例方法Invoke.BeginIn ...
- Java1.8 JDK源码中,对两个类进行 按位与 操作是什么意思
Java容器类库中的Map接口(java\util\Map.java)中有一个Entry接口(java\util\Map.java),其中有几个接口方法用到了类和类的按位与操作,即类和类之间有 &am ...
- apache 服务器概述--安装(一)
一.安装httpd,elinks浏览器 [root@ ~]# yum install elinks httpd -y [root@ ~]# elinks www.baidu.com 二.配置文件 # ...
- 深入Spring:自定义事务管理
转自: http://www.jianshu.com/p/5347a462b3a5 前言 上一篇文章讲了Spring的Aop,这里讲一下Spring的事务管理,Spring的事务管理是建立在Aop的基 ...
- GCD之定时器dispatch_source_t(转载暂时未完全理解)
#import "ViewController.h" @interface ViewController (){ IBOutlet UIButton *l_timeButton; ...