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 ...
随机推荐
- do {...} while (0) 的用途汇总(欢迎补充)
在一些Linux内核和其它的开源代码中,我们经常看到像下面这样的代码: do{ ... }while(0) 该代码片段并非循环,这样想想似乎使用do…while没有任何意义,那么为什么还要使用它呢? ...
- 搭建laravel到nginx
一.laravel的安装 搭建的第一步当然是安装好laravel,这里推介composer安装,由于国内的问题,极其推介使用国内的镜像去搭建,我在终端里本已经设置好常规的https和http之类的FQ ...
- MySQL Json类型的数据处理
新建表 CREATE TABLE `user_copy` ( `id` ) NOT NULL, `name` ) DEFAULT NULL, `lastlogininfo` json DEFAULT ...
- CustomSqlSessionFactoryBean
import java.io.File; import java.net.JarURLConnection; import java.net.URL; import java.util.ArrayLi ...
- 二叉树的递归,非递归遍历(java)
import java.util.Stack; import java.util.HashMap; public class BinTree { private char date; private ...
- java json-lib配置
用法 项目中要用到json-lib,mvnrepository.com查找它的dependency时结果如下: import net.sf.json.JSONArray; import net.sf. ...
- 并发编程之 Exchanger 源码分析
前言 JUC 包中除了 CountDownLatch, CyclicBarrier, Semaphore, 还有一个重要的工具,只不过相对而言使用的不多,什么呢? Exchange -- 交换器.用于 ...
- Spring学习之路-SpringBoot简单入门
简单讲SpringBoot是对spring和springMVC的二次封装和整合,新添加了一些注解和功能,不算是一个新框架. 学习来源是官方文档,虽然很详细,但是太墨迹了… 地址:https://doc ...
- jQuery补充知识点
链式编程 通常情况下,只有设置操作才能把链式变成延续下去.因为获取 操作的时候,会返回获取到的响应的值,无法反对jQuery对象. //end(); 筛选选择器会改变jQuery对象的DOM对象,想要 ...
- 集合框架(TreeSet原理)
特点: TreeSet是用来排序的,可以指定一个顺序,对象存入之后会按照指定的顺序排列 使用方式: 自然排序(Comparable) TreeSet类的add()方法中会把存入的对象提升为Compar ...