最左前缀法则
如果索引了多列(联合索引),要遵守最左前缀法则。最左前缀法则指的是查询从索引的最左列开始,并且不跳过索引中的列。如果跳过某一列,索引将部分失效(后面的字段索引失效)。
假设user
表有username
varchar(50)、phone
varchar(20)、email
varchar(50)三个字段组成的联合索引idx_user_username_phone_email
按照最左前缀法则有以下情况
-
使用索引最左侧字段进行条件查询,命中索引,且索引长度为152,表示只用到索引中的
username
字段1 2 3 4 5 6 7
mysql> explain select * from `user` where `username` = 'Bob'; +----+-------------+-------+------------+------+-------------------------------+-------------------------------+---------+-------+------+----------+-------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+-------------------------------+-------------------------------+---------+-------+------+----------+-------+ | 1 | SIMPLE | user | NULL | ref | idx_user_username_phone_email | idx_user_username_phone_email | 152 | const | 1 | 100.00 | NULL | +----+-------------+-------+------------+------+-------------------------------+-------------------------------+---------+-------+------+----------+-------+
-
使用索引中的全部三个字段,命中索引 且
key_len
为366,说明三个字段全部命中1 2 3 4 5 6 7
mysql> explain select * from `user` where `username` = 'Bob' and `phone`='18888888888' and `email`='1888888888@163.com'; +----+-------------+-------+------------+------+-------------------------------+-------------------------------+---------+-------------------+------+----------+-------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+-------------------------------+-------------------------------+---------+-------------------+------+----------+-------+ | 1 | SIMPLE | user | NULL | ref | idx_user_username_phone_email | idx_user_username_phone_email | 366 | const,const,const | 1 | 100.00 | NULL | +----+-------------+-------+------------+------+-------------------------------+-------------------------------+---------+-------------------+------+----------+-------+
-
使用
username
和email
查询,结果 命中索引,但只有username
字段是有效的,因为按照最左前缀原则,如果跳过中间列(phone
),则索引将部分失效1 2 3 4 5 6 7
mysql> explain select * from `user` where `username` = 'Bob' and `email`='1888888888@163.com'; +----+-------------+-------+------------+------+-------------------------------+-------------------------------+---------+-------+------+----------+-----------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+-------------------------------+-------------------------------+---------+-------+------+----------+-----------------------+ | 1 | SIMPLE | user | NULL | ref | idx_user_username_phone_email | idx_user_username_phone_email | 152 | const | 1 | 10.00 | Using index condition | +----+-------------+-------+------------+------+-------------------------------+-------------------------------+---------+-------+------+----------+-----------------------+
索引失效情况
-
使用索引中的第二列
phone
和第三列email
查询,未命中索引,因为查询条件中的字段没有从索引的最左列开始(最左前缀法则)1 2 3 4 5 6 7
mysql> explain select * from `user` where `phone`='18888888888' and `email`='1888888888@163.com'; +----+-------------+-------+------------+------+---------------+------+---------+------+-------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+---------------+------+---------+------+-------+----------+-------------+ | 1 | SIMPLE | user | NULL | ALL | NULL | NULL | NULL | NULL | 99677 | 1.00 | Using where | +----+-------------+-------+------------+------+---------------+------+---------+------+-------+----------+-------------+
-
使用模糊匹配进行查询,未命中索引
1 2 3 4 5 6 7
mysql> explain select * from `user` where `phone` like '188%'; +----+-------------+-------+------------+------+---------------+------+---------+------+-------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+---------------+------+---------+------+-------+----------+-------------+ | 1 | SIMPLE | user | NULL | ALL | NULL | NULL | NULL | NULL | 99677 | 11.11 | Using where | +----+-------------+-------+------------+------+---------------+------+---------+------+-------+----------+-------------+
-
条件语句中使用函数,截取用户名
username
的前缀进行查询,未命中索引1 2 3 4 5 6 7
mysql> explain select * from `user` where substring(username, 1, 3) = 'dev'; +----+-------------+-------+------------+------+---------------+------+---------+------+-------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+---------------+------+---------+------+-------+----------+-------------+ | 1 | SIMPLE | user | NULL | ALL | NULL | NULL | NULL | NULL | 99677 | 100.00 | Using where | +----+-------------+-------+------------+------+---------------+------+---------+------+-------+----------+-------------+
-
字符串不加单引号
''
,只命中了最左侧的索引字段 未加单引号的字段 未命中索引1 2 3 4 5 6 7
mysql> explain select * from user where username = 'bob' and phone = 18895766335; +----+-------------+-------+------------+------+-------------------------------+-------------------------------+---------+-------+------+----------+-----------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+-------------------------------+-------------------------------+---------+-------+------+----------+-----------------------+ | 1 | SIMPLE | user | NULL | ref | idx_user_username_phone_email | idx_user_username_phone_email | 152 | const | 1 | 10.00 | Using index condition | +----+-------------+-------+------------+------+-------------------------------+-------------------------------+---------+-------+------+----------+-----------------------+
-
OR
作为连接的条件,用OR
分开的条件,如果OR
前的条件中的列有索引,而后面的列中没有索引,那么涉及的索引都 不会命中。1 2 3 4 5 6 7
mysql> explain select * from user where id = 10 or phone = '18895766335'; +----+-------------+-------+------------+------+---------------+------+---------+------+-------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+---------------+------+---------+------+-------+----------+-------------+ | 1 | SIMPLE | user | NULL | ALL | PRIMARY | NULL | NULL | NULL | 99677 | 10.00 | Using where | +----+-------------+-------+------------+------+---------------+------+---------+------+-------+----------+-------------+
数据分布影响
如果MySQL评估使用索引比全表更慢,则不使用索引。
|
|
SQL提示
SQL提示,是优化数据库的一个重要手段,简单来说,就是在SQL语句中加入一些人为的提示来达到优化操作的目的。
- use index
|
|
- ignore index
|
|
- force index
|
|
覆盖索引
尽量使用覆盖索引(查询使用了覆盖索引,并且需要返回的列,在该索引中已经全部能够找到),减少select *。
|
|
前缀索引
当字段类型为字符串(varchar, text等)时,有时候需要索引很长的字符串,这会让索引变的很大,查询时,浪费大量的磁盘IO,影响查询效率。此时可以只将字符串的一部分前缀,建立索引,这样可以大大节约索引空间,从而提高索引效率。
- 语法:
|
|
- 前缀长度
可以根据索引的选择性来决定,而选择性是指不重复的索引值(基数)和数据表的记录总数的比值,索引选择性越高则查询效率越高,唯一索引的选择性是1,这是最好的索引选择性,性能也是最好的。可根据一下结果做参考:
|
|
单列索引和联合索引
- 单列索引:即一个索引值包含单个列
- 联合索引:即一个索引包含了多个列
😀 提示: 在业务场景中,如果存在多个查询条件,考虑对查询字段建立索引时,建议建立联合索引,而非单列索引。
索引设计原则
- 1.针对数据量较大,且查询比较频繁的表建立索引。
- 2.针对常作为查询条件(
WHERE
)、排序(ORDER BY
)、分组(GROUP BY
)操作的字段建立索引。 - 3.尽量选择区分度高的列作为索引,尽量建立唯一索引,区分度越高,使用索引的频率越高。
- 4.如果是字符串类型的字段,字段的长度较长,可以针对字段的特点,建立前缀索引。
- 5.尽量使用联合索引,减少单列索引,查询时,联合索引很多时候可以覆盖索引,节省存储空间,避免回表,提高查询效率。
- 6.要控制索引的数量,索引并不是多多益善,索引越多,维护索引结构的代价就越大,会影响增删改的效率。
- 7.如果索引列不能存储
NULL
值,请在创建表时使用NOT NULL
约束它。当优化器知道每列是否包含NULL
值时,它可以更好的确定哪个索引最有效的用于查询。