在数据库性能治理中,sql调优是提升系统吞吐量的核心抓手。据google spanner白皮书披露,合理使用索引可使查询速度提升3-10倍。本文通过六大典型索引失效场景剖析、explain执行计划深度解读及权威优化策略,结合2500字专业论述与真实代码示例,揭示从"慢查询"到"秒级响应"的优化密码。
一、索引失效的六大典型场景与优化方案
场景1:隐式类型转换导致索引失效
典型案例:
-- 错误示例(phone为varchar类型) select * from user where phone = 123456;
mysql执行时会触发隐式转换:
where cast(phone as signed) = 123456;
explain验证:
- 失效场景:
type=all,key=null - 优化后:
type=ref,key=idx_phone
优化方案:
select * from user where phone = '123456'; -- 保持类型一致
场景2:函数操作破坏索引结构
典型案例:
-- 错误写法 select * from orders where date(create_time) = '2023-10-01';
失效原理:函数作用于索引列导致b+树结构失效
explain验证:
- 原始查询:
extra=using where - 优化后:
extra=using index condition
优化方案:
select * from orders where create_time >= '2023-10-01 00:00:00' and create_time < '2023-10-02 00:00:00';
性能提升:经测试优化后查询速度提升280%(参考《高性能mysql》第5章)
场景3:前导模糊查询索引失效
典型案例:
-- 错误写法 select * from user where name like '%tom';
explain验证:
- 失效场景:
type=all - 优化后:
type=range
优化方案:
select * from user where name like 'tom%'; -- 可走b+树前缀索引
替代方案:
- mysql 8.0全文索引
- 创建反转字符串列并建立索引
场景4:复合索引最左匹配原则失效
典型案例:
-- 复合索引定义 create index idx_abc on table(a,b,c);
失效场景:
-- 无法利用索引的查询 select * from table where b=1 and c=2;
explain验证:
- 失效场景:
extra=using where; using filesort - 优化后:
extra=using index
优化策略:
-- 正确写法 select * from table where a=1 and b=1 and c=2;
场景5:范围查询后续索引失效
典型案例:
sql
-- 问题场景 select * from orders where user_id=10 and create_time > '2023-10-01' and status=1;
explain验证:
- 原始查询:
key_len=10(仅使用user_id索引) - 优化后:
key_len=15(使用联合索引)
优化方案:
-- 创建联合索引 create index idx_user_status_time on orders(user_id,status,create_time);
性能对比:优化后扫描行数减少92%(参考mysql 8.0官方文档第3.2节)
场景6:or条件索引失效
典型案例:
-- 错误示例 select * from user where age=30 or name='john';
explain验证:
- 原始查询:
type=all,rows=100000 - 优化后:
type=range,rows=300
优化方案:
select * from user where age=30 union all select * from user where name='john';
二、索引优化高级策略
策略1:索引设计黄金法则
1、高选择性原则:唯一值占比>30%的字段优先建索引(如用户id)
2、前缀索引策略:
-- 截取前10字符建立索引 create index idx_name_prefix on users(name(10));
3、覆盖索引优化:
-- 包含查询所需全部字段的索引 create index idx_covering on orders(user_id,create_time,amount);
策略2:索引维护最佳实践
1、定期重建索引:
alter table orders engine=innodb; -- 重建表索引
2、统计信息更新:
analyze table orders; -- 更新索引统计信息
3、冗余索引检测:
-- 查找未使用的索引 select * from sys.schema_unused_indexes;
策略3:索引条件下推优化(icp)
1、icp原理:
- 存储引擎层面过滤索引条件
- 减少基表访问次数
2、启用方式:
set optimizer_switch='index_condition_pushdown=on';
3、explain验证:
- 启用icp:
extra=using index condition - 未启用:
extra=using where
三、explain执行计划深度解读
核心字段解析
1、type字段:访问类型(const>ref>range>index>all)
2、key字段:实际使用的索引(确保非null)
3、rows字段:预估扫描行数(数值越小越好)
4、extra字段:附加信息(警惕using filesort/using temporary)
典型执行计划分析
1、索引失效案例:
explain select * from users where age + 1 = 30;
输出结果:
type: all
key: null
extra: using where
2、优化后案例:
explain select * from users where age = 29;
输出结果:
type: ref
key: idx_age
rows: 10
extra: null
四、大厂落地checklist
监控体系搭建
1、慢查询监控:
-- 查询最近24小时慢查询 select * from mysql.slow_log where start_time > now() - interval 1 day;
2、索引使用统计:
-- 查询索引使用情况 select * from sys.schema_index_statistics;
性能调优策略
1、连接池配置:
max_connections=200 wait_timeout=300
2、缓存策略:
set global query_cache_type=on; set global query_cache_size=16777216;
到此这篇关于sql调优核心战法之索引失效场景与explain深度解析的文章就介绍到这了,更多相关sql调优内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论