mysql的索引失效

一、成功的索引优化

1.表数据如下:
image

image

2.查询语句如下:

explain select id, age, level from employee where dpId = 1 and age = 30 order by level
image

Extra: Using where; Using filesort

出现了Using filesort需要进行优化。方法很简单,为查询,分组排序的字段建索引即可。

3.建索引优化:

create index idx_employee_dla on employee(dpId, age, level)

image

再次查询结果如下,type为ref,使用到了索引key,Extra为Using Where; Using index,优化成功:

image

二、跳过中间索引,使索引失效

image

索引的顺序为 id, dpId,age,level,查询时跳过age索引

image
Extra出现Using filesort,索引失效,需要优化。



猜你喜欢

转载自blog.51cto.com/12874079/2149404