mysql 强制使用索引和强制不使用索引
2017-11-27 22:00
411 查看
前一段面试时问过这个问题,当时不太懂,今天刚好看到这一块的内容就梳理下
首先说个概念
关于索引有三种sql提示
如在test1中查询
test1 有两个字段
主键id
索引 name
explain select id from test1;
+------+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
| 1 | SIMPLE | test1 | index | NULL | PRIMARY | 4 | NULL | 18 | Using index |
+------+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
这里用到了主键索引
我用use来限制下索引的使用,则出现下面的结果,优先使用我允许的索引
MariaDB [fei]> explain select id from test1 use index(name);
+------+-------------+-------+-------+---------------+------------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-------+-------+---------------+------------+---------+------+------+-------------+
| 1 | SIMPLE | test1 | index | NULL | name_index | 6 | NULL | 18 | Using index |
+------+-------------+-------+-------+---------------+------------+---------+------+------+-------------+
忽略某个索引(不使用某个索引)
MariaDB [fei]> explain select name from test1 ;
+------+-------------+-------+-------+---------------+------------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-------+-------+---------------+------------+---------+------+------+-------------+
| 1 | SIMPLE | test1 | index | NULL | name_index | 6 | NULL | 18 | Using index |
+------+-------------+-------+-------+---------------+------------+---------+------+------+-------------+
当我使用ignore限制时出现下面的情况,不在使用该了
MariaDB [fei]> explain select name from test1 ignore index(name);
+------+-------------+-------+------+---------------+------+---------+------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-------+------+---------------+------+---------+------+------+-------+
| 1 | SIMPLE | test1 | ALL | NULL | NULL | NULL | NULL | 18 | |
+------+-------------+-------+------+---------------+------+---------+------+------+-------+
但是我对主键进行了测试,貌似ignore对主键并不适用
MariaDB [fei]> explain select id from test1 force index(name);
+------+-------------+-------+-------+---------------+------------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-------+-------+---------------+------------+---------+------+------+-------------+
| 1 | SIMPLE | test1 | index | NULL | name_index | 6 | NULL | 18 | Using index |
+------+-------------+-------+-------+---------------+------------+---------+------+------+-------------+
force index 和 use index 的区别
use index 只是给mysql优化器一种选择的可能,具体的mysql优化器再进行优化选择
但是 force index 是强制mysql优化器使用某个索引
ae73
首先说个概念
SQL提示
就是在sql中加入我自己的提示来达到我想要的效果关于索引有三种sql提示
use index(index_name)
希望mysql参考的索引列表(也就是我想让mysql优先使用这些索引,其它索引不再考虑)如在test1中查询
test1 有两个字段
主键id
索引 name
explain select id from test1;
+------+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
| 1 | SIMPLE | test1 | index | NULL | PRIMARY | 4 | NULL | 18 | Using index |
+------+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
这里用到了主键索引
我用use来限制下索引的使用,则出现下面的结果,优先使用我允许的索引
MariaDB [fei]> explain select id from test1 use index(name);
+------+-------------+-------+-------+---------------+------------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-------+-------+---------------+------------+---------+------+------+-------------+
| 1 | SIMPLE | test1 | index | NULL | name_index | 6 | NULL | 18 | Using index |
+------+-------------+-------+-------+---------------+------------+---------+------+------+-------------+
ignore index (index_name)
忽略某个索引(不使用某个索引)MariaDB [fei]> explain select name from test1 ;
+------+-------------+-------+-------+---------------+------------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-------+-------+---------------+------------+---------+------+------+-------------+
| 1 | SIMPLE | test1 | index | NULL | name_index | 6 | NULL | 18 | Using index |
+------+-------------+-------+-------+---------------+------------+---------+------+------+-------------+
当我使用ignore限制时出现下面的情况,不在使用该了
MariaDB [fei]> explain select name from test1 ignore index(name);
+------+-------------+-------+------+---------------+------+---------+------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-------+------+---------------+------+---------+------+------+-------+
| 1 | SIMPLE | test1 | ALL | NULL | NULL | NULL | NULL | 18 | |
+------+-------------+-------+------+---------------+------+---------+------+------+-------+
但是我对主键进行了测试,貌似ignore对主键并不适用
force index(index_name)
强制使用索引MariaDB [fei]> explain select id from test1 force index(name);
+------+-------------+-------+-------+---------------+------------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-------+-------+---------------+------------+---------+------+------+-------------+
| 1 | SIMPLE | test1 | index | NULL | name_index | 6 | NULL | 18 | Using index |
+------+-------------+-------+-------+---------------+------------+---------+------+------+-------------+
force index 和 use index 的区别
use index 只是给mysql优化器一种选择的可能,具体的mysql优化器再进行优化选择
但是 force index 是强制mysql优化器使用某个索引
ae73
相关文章推荐
- MYSQL强制使用索引和禁止使用索引
- 在MySQL查询中忽略或强制使用某索引
- MYSQL强制使用索引和禁止使用索引
- mysql强制使用指定索引查询
- MYSQL强制使用索引和禁止使用索引
- MYSQL强制使用索引和禁止使用索引
- mysql强制使用索引
- MYSQL强制使用索引和禁止使用索引
- MYSQL强制使用索引和禁止使用索引
- Mysql: 强制走索引:mysql between 日期索引 索引问题-日期索引使用
- mysql select中子查询中使用强制索引的优化案例
- mysql强制使用索引与不使用索引
- mysql force Index 查询强制使用索引
- MySQL索引使用方法及实例学
- mysql慢查询Slow Query Log和未使用索引(Not Using Indexes)查询配置和使用
- MySQL优化之索引的使用
- mysql 索引的使用
- where条件对mysql多列索引的使用
- Mysql innodb引擎和myisam引擎 使用索引区别
- MySQL优化之索引的使用