【MySQL调优】
1.选择合适的存储引擎
查询多—> myisam
写操作多—> innodb
2.SQL语句的调优(尽量避免全表扫描,加快数据的检索速度)
- 在select、where、order by 常涉及到的字段上建立索引
- where子句中不适用!=,否则导致全表扫描
- 尽量避免空值(NULL)判断,否则导致全表扫描
示例:
优化前:select id from t1 where number is null;
优化后:可以在number设置默认值为0,确保number无NULL,查询如下
selecrt id from t1 where number = 0; - 尽量避免用 or 来连接条件,否则全表扫描
优化前:select id from t1 where id=10 or id=20;
优化后:select id from t1 where id=10
union all
select id from t1 where id=20;
[union all]
…; - 模糊查询尽量避免前置%,否则全表扫描
select id from t1 where name like “c%”; - 尽量避免in 和 not in的使用,否则导致全表扫描
select id from t1 where number in(1,2,3);
对于连续的值,尽量用between and - 尽量避免使用select * from t1;要用具体的字段名代替*,不用返回用不到的任何字段