MySQL之索引使用与失效情况
索引使用
验证索引效率
在未建立索引之前,执行如下SQL语句,查看SQL的耗时。
SELECT * FROM tb_sku WHERE sn ="100000003145001';
create index idx_sku_sn on tb sku(sn) ;
然后再次执行相同的SQL语句,再次查看SQL的耗时
SELECTFROM tb_sku WHERE sn =100000003145001';
最左前缀法则
如果索引了多列(联合索引),要遵守最左前缀法则。最左前缀法则指的是查询从索引的最左列开始,并且不跳过索引中的列。如果跳跃某一列,索引将部分失效(后面的字段索引失效)。
explain select * from tb_user where profession = '软件工程' and age = 31 and status = '0'; # 走联合索引,因为联合索引最左边的pro在,并且没跳过age
explain select * from tb_user where profession = '软件工程' and age = 31; # 走联合索引,因为联合索引最左边的pro在,并且没跳过age
explain select * from tb_user where profession = '软件工程'; # 走联合索引
explain select * from tb_user where age = 31 and status = '0'; # 不走联合索引,因为联合索引最左边的pro没在
explain select * from tb_user where status = '0'; # 不走联合索引,因为联合索引最左边的pro没在
联合索引中最左边的索引必须存在,跟放的位置无关
范围查询
联合索引中,出现范围查询(>,<),范围查询右侧的列索引失效
explain select * from tb_user where profession = 软件工程' and age > 30 and status = '0'; explain select * from tb_user where profession = 软件工程' and age >= 30 and status = '0';
如果业务允许,尽量使用>= | <= ,因为带=不会使索引失效
索引列运算
不要在索引列上进行运算操作,索引将失效。
explain select * from tb_user where substring(phone,10,2) = '15';
字符串不加引号
字符串类型字段使用时,不加引号,索引将失效
explain select* from tb_user where profession = 软件工程' and age = 31 and status = 0; explain select * from tb_user where phone = 17799990015;
模糊查询
如果仅仅是尾部模糊匹配,索引不会失效,如果是头部模糊匹配,索引失效
explain select * from tb_user where profession like 软件%'; # 不会失效,走索引 explain select * from tb_user where profession like '%工程'; # 索引失效 explain select * from tb_user where profession like '%工%'; # 索引失效,前面有%
or连接的条件
用or分隔开的条件,如果or前的条件中列有索引,而后面的列中没有索引,那么涉及的索引都不会被用到。
explain select * from tb_user where id = 10 or age = 23; explain select * from tb_user where phone = '17799990017' or age = 23:
由于age没有索引,所以即使id、phone有索引,索引也会失效。所以需要针对于age也要建立索引
数据分布影响
如果MySQL评估使用索引比全表更慢,则不使用索引
select * from tb_user where phone >='17799990005'; select * from tb_user where phone >='17799990015';
SQL提示
SQL提示,是优化数据库的一个重要手段,简单来说,就是在SQL语句中加入一些人为的提示来达到优化操作的目的。
use index:
explain select * from tb_user use index(idx_user_pro) where profession = '软件工程‘;
ignore index:
explain select * from tb_user ignore index(idx_user_pro) where profession = ’软件工程‘;
force index:
explain select* from tb_user force index(idx_user_pro) where profession = '软件工程';
覆盖索引
尽量使用覆盖索引(查询使用了索引,并且需要返回的列,在该索引中已经全部能找到),减少select * 。
explain select id, profession from tb——user where profession = 软件工程 and age = 31 and status = '0' ; explain select id,profession,age, status from tb_user where profession = 软件工程'and age = 31 and status = '0' ; explain select id,profession,age, status, name from tb——user where profession = 工程 and age = 31 and status ='0' ; explain select * from tb_user where profession = 软件工程' and age = 31 and status = '0' ;
注意:
using index condition : 查找使用了索引,但是需要回表查询数据。 using where; using index : 查找使用了索引,但是需要的数据都在索引列中能找到,所以不需要回表查询数据。
图解:
前缀索引
当字段类型为字符串(varchar,text等)时,有时候需要索引很长的字符串,这会让索引变得很大,查询时,浪费大量的磁盘10,影响查询效率。此时可以只将字符串的一部分前缀,建立索引,这样可以大大节约索引空间,从而提高索引效率。
语法
create index idx_xxxx on table_name(column(n)) ;
前缀长度
可以根据索引的选择性来决定,而选择性是指不重复的索引值(基数)和数据表的记录总数的比值,索引选择性越高则查询效率越高,
唯一索引的选择性是1,这是最好的索引选择性,性能也是最好的。
select count(distinct email) / count(*) from tb_user ; select count(distinct substring(email,1,5)) / count(*) from tb_user;
单列索引和联合索引
单列索引: 即一个索引只包含单个列。 联合索引: 即一个索引包含了多个列。 在业务场景中,如果存在多个查询条件,考虑针对于查询字段建立索引时,建议建立联合索引,而非单列索引。
单列索引情况:
explain select id, phone, name from tb_user where phone = '17799990010' and name = 韩信';
多条件联合查询时,MySQL优化器会评估哪个字段的索引效率更高,会选择该索引完成本次查询。
联合索引情况:
索引设计原则
-
针对于数据量较大,且查询比较频繁的表建立索引。
-
针对于常作为查询条件(where)、排序 (order by)、分组 (group by) 操作的字段建立索引
-
尽量选择区分度高的列作为索引,尽量建立唯一索引,区分度越高,使用索引的效率越高
-
如果是字符串类型的字段,字段的长度较长,可以针对于字段的特点,建立前缀索引。
-
尽量使用联合索引,减少单列索引,查询时,联合索引很多时候可以覆盖索引,节省存储空间,避免回表,提高查询效率。
-
要控制索引的数量,索引并不是多多益善,索引越多,维护索引结构的代价也就越大,会影响增删改的效率。
-
如果索引列不能存储NULL值,请在创建表时使用NOT NULL约束它。当优化器知道每列是否包含NULL值时,它可以更好地确定哪个索引最有效地用于查询。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~