MySQL性能调优

1、选择合适的存储引擎

  1、经常用来读的表使用myisam存储引擎

   2、其余的表都用innodb存储引擎   

2、SQL语句调优(尽量避免全表扫描)

  1、在select where order by常涉及到的字段上建立索引
  2、where子句中不使用 !=,否则将放弃使用索引进行全表扫描
  3、尽量避免用NULL值判断,否则会全表扫描

示例:
select id from t1 where number is null;
优化:
在number字段设置默认值0

  4、尽量避免 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;

5、模糊查询尽量避免使用前置 %,导致全表扫描

select id from t1 where name like "a%";

6、尽量避免 in 和 not in,导致全表扫描

select id from t1 where id in(1,2,3);
select id from t1 where id between 1 and 3;

7、尽量避免使用 select * ...,要用具体的字段列表代替 *,不要返回用不到的任何字段

8、使用存储过程

因为存储过程通常以编译过的形式存储,所以DBMS处理命令少提高了性能。

posted @ 2018-08-01 11:22  饕客  阅读(134)  评论(1编辑  收藏  举报