mysql索引是提升数据库性能的关键因素,正确使用索引可以将查询效率提高几十倍甚至上百倍。
然而,在实际开发中,即使创建了索引,却经常出现索引不生效的情况,
本文将分享mysql索引失效的12种典型场景,通过示例帮助开发者理解索引失效的原理,并掌握相应的优化方法。
一、在索引列上使用函数或表达式
这是最常见的索引失效场景之一,当我们在where子句中对索引列使用函数时,mysql无法利用索引进行查询优化。
问题示例
-- 创建索引 create index idx_create_time on orders(create_time); -- 以下查询无法使用索引 select * from orders where year(create_time) = 2023;
原理解释
当对索引列应用函数时,mysql必须对表中的每一行都应用该函数,然后再与条件比较,这就导致了全表扫描。索引的b+树结构是基于列的原始值构建的,而不是函数计算后的值。
解决方案
将函数应用于条件值而不是列:
-- 优化后的查询,可以使用索引 select * from orders where create_time >= '2023-01-01 00:00:00' and create_time < '2024-01-01 00:00:00';
二、使用类型隐式转换
当条件中的值与索引列的类型不匹配时,mysql会进行隐式类型转换,导致索引失效。
问题示例
-- 创建表和索引 create table users ( id int primary key, phone varchar(20), index idx_phone (phone) ); -- 以下查询可能无法使用索引 select * from users where phone = 13800138000;
原理解释
在上面的例子中,phone是varchar类型,而条件值13800138000是整数。mysql会将字符串类型的phone隐式转换为数字类型进行比较,导致无法使用索引。
解决方案
确保条件值与索引列类型一致:
-- 正确的查询方式,可以使用索引 select * from users where phone = '13800138000';
三、使用不等于或不包含操作符
使用!=、<>、not in、not like等否定条件时,通常会导致索引失效。
问题示例
-- 创建索引 create index idx_status on orders(status); -- 以下查询可能无法有效利用索引 select * from orders where status != 'completed'; select * from orders where status not in ('completed', 'shipped');
原理解释
mysql的索引是为了快速查找满足条件的记录,而否定条件通常意味着要查找的范围太大。mysql优化器可能判断使用索引的代价大于全表扫描,因此选择不使用索引。
解决方案
尽量使用肯定条件替代否定条件:
-- 优化后的查询 select * from orders where status in ('pending', 'processing', 'cancelled');
如果必须使用否定条件,可以考虑重新设计索引或添加适当的统计信息帮助优化器做出更好的决策。
四、使用or操作符连接不同的索引列
当使用or连接多个条件,且这些条件分别在不同的索引上时,可能导致索引失效。
问题示例
-- 创建单列索引 create index idx_name on customers(name); create index idx_email on customers(email); -- 以下查询可能无法充分利用索引 select * from customers where name = 'john' or email = 'john@example.com';
原理解释
mysql在处理or条件时,需要分别获取满足每个条件的记录,然后合并结果。在某些情况下,优化器会认为这种操作的成本高于全表扫描,从而选择不使用索引。
解决方案
1. 使用union替代or:
-- 使用union优化 select * from customers where name = 'john' union select * from customers where email = 'john@example.com';
2. 创建复合索引或使用索引合并:
-- 在mysql 5.6及以上版本,可能会使用索引合并 -- 也可以创建覆盖索引 create index idx_name_email on customers(name, email);
五、使用like操作符且以通配符开头
当使用like操作符进行模糊查询,且模式以通配符(%)开头时,索引通常会失效。
问题示例
-- 创建索引 create index idx_product_name on products(product_name); -- 以下查询无法使用索引 select * from products where product_name like '%phone%';
原理解释
b+树索引是按照索引列的值排序的,当使用前缀通配符(如'%phone')时,mysql无法利用索引的有序性来定位数据,只能进行全表扫描。
解决方案
1. 避免使用前缀通配符,改用后缀通配符:
-- 可以使用索引的查询 select * from products where product_name like 'phone%';
2. 对于必须使用前缀通配符的场景,考虑使用全文索引:
-- 创建全文索引 alter table products add fulltext index ft_product_name(product_name); -- 使用全文索引查询 select * from products where match(product_name) against('phone' in boolean mode);
3. 考虑使用专门的搜索引擎,如elasticsearch。
六、对索引列进行运算
在where子句中对索引列进行算术运算同样会导致索引失效。
问题示例
-- 创建索引 create index idx_price on products(price); -- 以下查询无法使用索引 select * from products where price + 100 > 500;
原理解释
与函数使用类似,当对索引列进行运算时,mysql需要对表中的每一行数据都进行计算,然后再与条件值比较,导致无法利用索引。
解决方案
将运算应用于条件值,而不是列:
-- 优化后的查询,可以使用索引 select * from products where price > 500 - 100;
七、查询条件中的字段顺序与复合索引的顺序不一致
在使用复合索引(多列索引)时,如果查询条件中的字段顺序与索引创建时的顺序不一致,可能导致索引无法充分利用。
问题示例
-- 创建复合索引 create index idx_name_age_city on users(name, age, city); -- 以下查询无法充分利用索引 select * from users where age = 25 and city = 'new york' and name = 'john';
原理解释
mysql复合索引遵循"最左前缀"原则,即先按第一个索引列排序,值相同时再按第二个索引列排序,以此类推。当查询条件的顺序与索引列顺序不一致时,mysql的查询优化器通常能够重新排序这些条件,但在某些复杂场景下可能无法最优化。
解决方案
在编写查询时,尽量保持条件顺序与索引列顺序一致:
-- 优化后的查询,更容易使用索引 select * from users where name = 'john' and age = 25 and city = 'new york';
另外,在设计复合索引时,应将选择性高(不重复值多)的列放在前面。
八、在where子句中使用is null或is not null
在某些情况下,对索引列使用is null或is not null条件可能导致索引失效。
问题示例
-- 创建索引 create index idx_deleted_at on users(deleted_at); -- 以下查询可能无法使用索引 select * from users where deleted_at is null;
原理解释
mysql对null值的处理比较特殊。在早期版本中,mysql对含有null值的列索引效果不佳,尤其是在使用is null或is not null条件时。不过,在mysql 5.6及以后的版本中,这种情况有所改善。
解决方案
1. 在设计表时,尽量避免使用null值,可以使用空字符串或默认值代替:
-- 创建表时使用not null约束和默认值 create table users ( id int primary key, name varchar(100) not null, deleted_at timestamp null default null, status tinyint not null default 1 );
2. 如果必须查询null值,检查执行计划确保索引被正确使用:
explain select * from users where deleted_at is null;
九、查询的数据占表中数据的比例较大
当查询条件返回的结果集占表总数据量的比例较大时,mysql优化器可能会选择不使用索引,而是直接全表扫描。
问题示例
-- 创建索引 create index idx_gender on users(gender); -- 假设性别比例接近1:1,以下查询可能不使用索引 select * from users where gender = 'male';
原理解释
使用索引查询涉及两个步骤:先通过索引找到满足条件的记录id,再通过这些id获取完整记录(回表操作)。当结果集较大时,这种"随机io"的成本可能高于顺序读取全表的成本,因此优化器会选择全表扫描。
解决方案
1. 增加更多的过滤条件,减小结果集:
-- 添加更多条件缩小结果集 select * from users where gender = 'male' and age between 25 and 35;
2. 使用覆盖索引避免回表:
-- 创建覆盖索引 create index idx_gender_age_name on users(gender, age, name); -- 查询仅需要索引中包含的列 select gender, age, name from users where gender = 'male';
十、索引字段的数据重复度过高
当索引列的基数(不同值的数量)很低时,例如状态字段只有几个不同的值,mysql可能会认为使用索引效率低而选择全表扫描。
问题示例
-- 创建索引 create index idx_status on orders(status); -- 假设status只有3个值:'pending', 'processing', 'completed' -- 以下查询可能不使用索引 select * from orders where status = 'completed';
原理解释
索引的选择性是指不同索引值与表中记录总数的比值,选择性越高(接近1),索引效率越高。对于低选择性的列,使用索引可能需要访问大量的索引页和数据页,效率不如全表扫描。
解决方案
1. 将低选择性的列放在复合索引的后面:
-- 创建复合索引,将高选择性的user_id放在前面 create index idx_user_status on orders(user_id, status); -- 查询同时使用user_id和status select * from orders where user_id = 10001 and status = 'completed';
2. 考虑使用索引下推(index condition pushdown,icp)特性(mysql 5.6及以上版本支持)。
十一、使用不等值范围查询
当对索引列进行范围查询(如>、<、between)时,会部分影响索引的使用效率,尤其是在复合索引中。
问题示例
-- 创建复合索引 create index idx_age_salary on employees(age, salary); -- 以下查询只能使用索引的age部分,salary部分无法使用 select * from employees where age > 30 and salary > 50000;
原理解释
在复合索引中,如果对前面的列使用了范围条件,那么后面的列就无法使用索引了。这是因为b+树索引在范围查询后,无法再保证后续列的有序性。
解决方案
1. 调整索引列顺序,将范围查询的列放在最后:
-- 调整索引顺序 create index idx_salary_age on employees(salary, age); -- 如果条件中salary是等值查询,age是范围查询 select * from employees where salary = 50000 and age > 30;
2. 对于复杂条件,考虑创建多个索引:
-- 为不同的查询模式创建不同的索引 create index idx_age on employees(age); create index idx_salary on employees(salary);
十二、order by或group by子句的使用不当
当order by或group by的列与where条件中使用的索引列不一致时,可能导致额外的排序操作,影响性能。
问题示例
-- 创建索引 create index idx_name on users(name); -- 以下查询无法使用索引排序,会产生filesort select * from users where name = 'john' order by create_time;
原理解释
b+树索引本身是有序的,如果排序或分组的列与索引列一致,mysql可以直接利用索引的有序性。但如果不一致,mysql需要在检索出结果后再进行排序(filesort),这是一个成本较高的操作。
解决方案
1. 创建包含排序/分组列的复合索引:
-- 创建包含排序列的复合索引 create index idx_name_create_time on users(name, create_time); -- 现在可以使用索引排序 select * from users where name = 'john' order by create_time;
2. 如果排序方向不一致,考虑使用降序索引(mysql 8.0+支持):
-- 创建包含混合排序方向的索引 create index idx_name_time_score on users(name asc, create_time desc, score asc); -- 可以高效使用索引 select * from users where name = 'john' order by create_time desc, score asc;
如何诊断索引失效问题
发现并解决索引失效问题,需要掌握一些实用的诊断工具和方法:
1. 使用explain分析查询计划
explain是诊断索引使用情况的主要工具:
explain select * from orders where customer_id = 1001 and status = 'completed';
重点关注以下字段:
- • type: 从好到差依次是:system > const > eq_ref > ref > range > index > all
- • key: 实际使用的索引,如果为null则表示未使用索引
- • rows: 预计扫描的行数,数值越小越好
- • extra: 额外信息,如"using filesort"表示需要额外排序
2. 使用慢查询日志发现问题sql
配置并启用mysql慢查询日志:
set global slow_query_log = 'on'; set global long_query_time = 1; -- 设置慢查询阈值为1秒
3. 使用mysql性能模式(performance schema)
mysql 5.6及以上版本提供了更强大的性能监控工具:
-- 查看查询性能统计 select * from performance_schema.events_statements_summary_by_digest order by sum_timer_wait desc limit 10;
4. 使用show profile分析查询执行情况
set profiling = 1; select * from users where email like '%@example.com'; show profiles; show profile for query 1;
总结
索引优化是一个持续的过程,需要结合具体的业务场景和数据特点。通过了解这些索引失效的场景和原理,你可以更有针对性地设计索引策略,显著提升数据库性能。
没有一劳永逸的索引方案,随着数据量的增长和业务的变化,索引策略也需要不断调整和优化。
持续监控、分析和优化是保持高性能数据库的关键。
到此这篇关于mysql中索引失效的12种场景及对应解决方案的文章就介绍到这了,更多相关mysql索引失效解决内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论