mysql的索引失效
阿新 • • 發佈:2018-07-24
width 優化 c89 使用 files tle index ddd sha 一、成功的索引優化
1.表數據如下:
2.查詢語句如下:
explain select id, age, level from employee where dpId = 1 and age = 30 order by level
Extra: Using where; Using filesort
出現了Using filesort需要進行優化。方法很簡單,為查詢,分組或排序的字段建索引即可。
3.建索引優化:
create index idx_employee_dla on employee(dpId, age, level)
再次查詢結果如下,type為ref,使用到了索引key,Extra為Using Where; Using index,優化成功:
二、跳過中間索引,使索引失效
索引的順序為 id, dpId,age,level,查詢時跳過age索引或使用>,<,<>,!=條件查詢age索引
Extra出現Using filesort,索引失效,需要優化。
三、查詢字段加入非索引字段,使索引失效
userName為非索引字段,當查詢userName字段時,Extra為:Using index condition
四、使用>, <, <>, != 使索引失效
mysql的索引失效