MySQL-MySQL调优

MySQL调优

选择合适的存储引擎
  • 读操作多 :MyISAM
  • 写操作多 :InnoDB
创建索引

在select、where、order by常涉及到的字段建立索引

SQL语句的优化
  • where子句中不使用!=,否则放弃索引全表扫描
  • 尽量避免NULL值判断,否则放弃索引全表扫描
# 优化前
select number from t1 where number is null;
# 优化后
select number from t1 where number=0;
  • 尽量避免or连接条件,否则放弃索引全表扫描
# 优化前
select id from t1 where id=10 or id=20 or id=30;
# 优化后
select id from t1 where id=10
union all
select id from t1 where id=20
union all
select id from t1 where id=30;
  • 模糊查询尽量避免使用前置% ,否则全表扫描
  • 尽量避免使用innot in,否则全表扫描
# 优化前
select id from t1 where id in(1,2,3,4);
# 优化后
select id from t1 where id between 1 and 4;
  • 尽量避免使用select * ...;用具体字段代替*,不要返回用不到的任何字段

转载于:https://www.jianshu.com/p/a37224b7422c

猜你喜欢

转载自blog.csdn.net/weixin_33828101/article/details/91228929
今日推荐