mysql use index用法_mysql use index、ignore index、force index用法

原创你去了哪里 最后发布于2019-10-18 14:05:48 阅读数 121  收藏

展开

1:use index:在你查询语句表名的后面,添加use index来提供你希望mysql去参考的索引列表,就可以让mysql不再考虑其他可用的索引。如:select * from table use index(name,age);

2:IGNORE INDEX 提示会禁止查询优化器使用指定的索引。在具有多个索引的查询时,可以用来指定不需要优化器使用的那个索引,还可以在删除不必要的索引之前在查询中禁止使用该索引。如:select * from table ignore index(name,age);

3:force index:强制mysql使用一个特定的索引。一般情况下mysql会根据统计信息选择正确的索引,但是当查询优化器选择了错误的索引或根本没有使用索引的时候,这个提示将非常有用。

需要注意的是use/ignore/force index(index)这里括号里的index是索引名,而不是列名。而且后面必须要加上where条件。

先查看表索引信息:

show index from student2\G

*************************** 1. row ***************************

Table: student2

Non_unique: 0

Key_name: PRIMARY

Seq_in_index: 1

Column_name: id

Collation: A

Cardinality: 4

Sub_part: NULL

Packed: NULL

Null:

Index_type: BTREE

Comment:

Index_comment:

Visible: YES

Expression: NULL

*************************** 2. row ***************************

Table: student2

Non_unique: 1

Key_name: se

Seq_in_index: 1

Column_name: sex

Collation: A

Cardinality: 4

Sub_part: NULL

Packed: NULL

Null: YES

Index_type: BTREE

Comment:

Index_comment:

Visible: YES

Expression: NULL

2 rows in set (0.07 sec)

可以看出列sex上有个普通索引,索引名是se。

explain select * from student2 where sex='a';

+----+-------------+----------+------------+------+---------------+------+---------+-------+------+----------+-------+

| id | select_type | table    | partitions | type | possible_keys | key  | key_len | ref   | rows | filtered | Extra |

+----+-------------+----------+------------+------+---------------+------+---------+-------+------+----------+-------+

|  1 | SIMPLE      | student2 | NULL       | ref  | se            | se   | 2       | const |    1 |   100.00 | NULL  |

+----+-------------+----------+------------+------+---------------+------+---------+-------+------+----------+-------+

1 row in set, 1 warning (0.00 sec)

使用到了sex列的索引,如果我们不想它sql语句使用sex列的索引,可以这样写:

explain select * from student2 ignore index(se) where sex='a';

+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------------+

| id | select_type | table    | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |

+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------------+

|  1 | SIMPLE      | student2 | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    4 |    33.33 | Using where |

+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------------+

1 row in set, 1 warning (0.00 sec)

可以看出这次并没有使用到索引。

如果你这样写sql就会报错:explain select * from student2 ignore index(sex) where sex='a';

ERROR 1176 (42000): Key 'sex' doesn't exist in table 'student2'

因为ignore index括号里要写索引名,而不是列明。

————————————————

版权声明:本文为CSDN博主「你去了哪里」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/weixin_43740552/article/details/102623930

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值