文件在:E:\學習文檔子目錄壓縮\數據庫\mysql\mysql優化\螞蟻\螞蟻1\3mysql優化之SQL語句調優 或 我的網盤\我的筆記\學習文檔子目錄壓縮\數據庫\mysql\mysql優化\螞蟻\螞蟻1\3mysql優化之SQL語句調優
SQL優化技巧
- 使用group by 分組查詢是,默認分組後,還會排序,可能會降低速度,
在group by 後面增加 order by null 就可以防止排序.
explain select * from emp group by deptno order by null;
- 有些情況下,可以使用連接來替代子查詢。因爲使用join,MySQL不需要在內存中創建臨時表。
select * from dept, emp where dept.deptno=emp.deptno; [簡單處理方式]
select * from dept left join emp on dept.deptno=emp.deptno; [左外連接,更ok!]
- 對查詢進行優化,要儘量避免全表掃描,首先應考慮在 where 及 order by 涉及的列上建立索引
應儘量避免在 where 子句中對字段進行 null 值判斷,否則將導致引擎放棄使用索引而進行全表掃描,如: