当前位置:数据库 > > 正文

mysqlsql语句的优化(MySQL优化之如何写出高质量sql语句)

时间:2021-10-04 01:42:51类别:数据库

mysqlsql语句的优化

MySQL优化之如何写出高质量sql语句

前言

关于数据库优化,网上有不少资料和方法,但是不少质量参差不齐,有些总结的不够到位,内容冗杂。这篇文章就来给大家详细介绍了26条优化建议,下面来一起看看吧

1. 查询sql尽量不要使用全查 select *,而是 select + 具体字段。

反例:

  • ?
  • 1
  • select * from student;
  • 正例:

  • ?
  • 1
  • select id,name, age from student;
  • 理由:

    2. 使用预编译语句进行数据库操作

    理由:

    3. 禁止使用不含字段列表的 insert 语句

    反例:

  • ?
  • 1
  • insert into values ('a', 'b', 'c');
  • 正例:

  • ?
  • 1
  • insert into t(a, b, c) values ('a','b','c');
  • 理由:

    4. 尽量避免在 where 子句中使用 or 来连接条件

    案例:新建一个user表,它有一个普通索引userid,表结构如下:

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • create table `user` ( 
  • `id` int(11) not null auto_increment, 
  • `user_id` int(11) not null
  • `age` int(11) not null
  • `name` varchar(30) not null
  • primary key (`id`), 
  • key `idx_userid` (`userid`)
  • ) engine=innodb default charset=utf8;
  • 查询userid为1 或者 年龄为 18 岁的用户

    反例:

  • ?
  • 1
  • select id, user_id, age, name from user where userid=1 or age =18
  • 正例:

  • ?
  • 1
  • 2
  • 3
  • 4
  • # 使用union all
  • select id, user_id, age, name from user where userid=1 union all select * from user where age = 18
  • # 或者分开两条sql写
  • select id, user_id, age, name from user where userid=1; select * from user where age = 18
  • 理由:

    5. 使用 where 条件查询,要限定要查询的数据,避免返回多余的行,同时避免数据类型的隐式转换

    假设 id 为 int 类型,查询 id = 1 的数据

    反例:

  • ?
  • 1
  • select id, name from student where id = '1';
  • 正例:

  • ?
  • 1
  • select id, name from student where id = 1;
  • 理由:

    6. 静止在 where 子句中对字段进行表达式操作或函数转换,这将导致系统放弃使用索引而进行全表扫描

    假设 user 表的 age 字段,加了索引,对其进行数据查询

    反例:

  • ?
  • 1
  • select name, age from user where age - 1 = 20;
  • 正例:

  • ?
  • 1
  • select name, age from user where age = 21;
  • 理由:

    7. 尽量避免在 where 子句中使用 != 或 <> 操作符,否则将引擎放弃使用索引而进行全表扫描。

    (mysql中适用)

    反例:

  • ?
  • 1
  • select age,name from user where age <> 18;
  • 正例:

  • ?
  • 1
  • 2
  • 3
  • # 可以考虑分开两条sql写
  • select age,name from user where age < 18;
  • select age,name from user where age > 18;
  • 理由:

    8. 对查询优化,应考虑在where及order by涉及的列上建立索引,尽量避免全表扫描。

    反例:

  • ?
  • 1
  • select name, age, address from user where address ='深圳' order by age ;
  • 正例:添加索引再查询

  • ?
  • 1
  • alter table user add index idx_address_age (address,age)
  • 9. where子句中考虑使用默认值代替 null

    反例:(这种会全查所有数据)

  • ?
  • 1
  • select user_id, name, age from user where age is not null;
  • 正例:

  • ?
  • 1
  • 2
  • 3
  • 4
  • # 表字段age设置0为默认值代替null
  • select user_id, name, age from user where age > 0;
  • 1
  • 2
  • 理由:

    10. 如果查询结果只有一条或者只需要一条记录(可能最大/小值),建议使用 limit 1

    假设现在有student学生表,要找出一个名字叫 tom 的人.

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • create table `student` ( 
  • `id` int(11) not null
  • `name` varchar(50) default null
  • `age` int(11) default null
  • `date` datetime default null
  • `sex` int(1) default null
  • primary key (`id`)
  • ) engine=innodb default charset=utf8mb4;
  • 反例:

  • ?
  • 1
  • select id,name from student where name='tom '
  • 正例

  • ?
  • 1
  • select id,name from employee where name='tom ' limit 1;
  • 理由:

    加上 limit 1 分页后,只要找到了对应的一条记录,就不会继续向下扫描了,效率将会大大提高。
    如果name是唯一索引的话,是不必要加上 limit 1 了,因为limit的存在主要就是为了防止全表扫描,从而提高性能,如果一个语句本身可以预知不用全表扫描,有没有limit ,性能的差别并不大。

    11. 优化 limit 分页语句

    我们日常做分页需求时,一般会用 limit 实现,但是当偏移量特别大的时候,查询效率就变得低下

    反例:

  • ?
  • 1
  • select id,name,age from student limit 10000,10
  • 正例:

  • ?
  • 1
  • 2
  • # 方案一 :返回上次查询的最大记录(偏移量)
  • select id,name from student where id > 10000 limit 10;
  • ?
  • 1
  • 2
  • # 方案二:order by + 索引
  • select id,name from student order by id  limit 10000,10;
  • ?
  • 1
  • # 方案三:在业务允许的情况下限制页数:
  • 理由:

    12. 尽量避免向客户端返回过多数据量,使用limit分页

    假设业务需求是,用户请求查看自己最近一年观看过的电影数据。

    反例:

  • ?
  • 1
  • 2
  • 3
  • 4
  • # 一次性查询所有数据回来
  • select * from livinginfo
  • where watchid =useid
  • and watchtime >= date_sub(now(),interval 1 y)
  • 正例:

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • # 分页查询
  • select * from livinginfo
  • where watchid =useid
  • and watchtime>= date_sub(now(),interval 1 y)
  • limit offset,pagesize
  •  
  • # 如果是前端分页,可以先查询前两百条记录,因为一般用户应该也不会往下翻太多页
  • select * from livinginfo
  • where watchid =useid
  • and watchtime>= date_sub(now(),interval 1 y)
  • limit 200 ;
  • 13. 优化 like 语句

    当用到模糊关键字查询使用 like 时,like很可能让索引失效。

    反例:

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • select * from student
  • where name like '%strive_day';
  • -- 或者使用 % 包裹
  • select * from student
  • where name like '%strive_day%';
  • 正例:

  • ?
  • 1
  • 2
  • select * from student
  • where name like 'strive_day%';
  • 理由:

    14. 尽量避免在索引列上使用mysql的内置函数

    案例:查询最近七天内登陆过的用户(假设 logintime 字段加了索引)

    反例:

  • ?
  • 1
  • 2
  • select * from system_user user
  • where date_add(user.logintime,interval 7 day) >= now();
  • 正例:

  • ?
  • 1
  • 2
  • select * from system_user user
  • where user.logintime >=date_add(now(),interval - 7 day);
  • 理由:

    15. 使用联合索引时,注意索引列的顺序,一般遵循 最左匹配原则

    假设有一个联合索引 (user_id, age),user_id 在前,age 在后。

    反例:

  • ?
  • 1
  • select user_id, name, age from user where age = 10;
  • 正例:

  • ?
  • 1
  • 2
  • 3
  • 4
  • # 符合最左匹配原则
  • select user_id, name, age from user where userid = 1 and age = 21;
  • # 符合最左匹配原则
  • select user_id, name, age from user where userid = 1;
  • 理由:

    16. 在适当时候,使用覆盖索引。

    覆盖索引能够使得你的sql语句不需要 回表,仅仅访问索引就能够得到所有需要的数据,大大提高了查询效率。

    反例:

  • ?
  • 1
  • 2
  • # like模糊查询,不走索引
  • select user_id, name, age from user where user_id like '%123%'
  • ?
  • 1
  • 2
  • # id为主键,那么为普通索引,即覆盖索引。
  • select user_id, name, age from user where userid like '%123%';
  • 17. 删除冗余和重复索引

    反例:

  • ?
  • 1
  • 2
  • key `idx_userid` (`userid`)
  • key `idx_userid_age` (`userid`,`age`)
  • 正例:

  • ?
  • 1
  • 2
  • 3
  •   key `idx_userid_age` (`userid`,`age`)
  • #  删除 userid 的索引(key `idx_userid_age` (`userid`,`age`))
  • #  因为组合索引(a,b)相当于创建了(a)和(a,b)索引。
  • 理由:

    18. inner join 、left join、right join,优先使用inner join,如果是left join,左边表结果尽量小

    inner join 内连接,在两张表进行连接查询时,只保留两张表中完全匹配的结果集

    left join 在两张表进行连接查询时,会返回左表所有的行,即使在右表中没有匹配的记录。

    right join 在两张表进行连接查询时,会返回右表所有的行,即使在左表中没有匹配的记录。

    都满足sql需求的前提下,优先使用inner join(内连接),如果要使用left join,左边表数据结果尽量小,如果有条件的尽量放到左边处理。

    反例:

  • ?
  • 1
  • select name, age from tab1 t1 left join tab2 t2  on t1.age = t2.age where t1.id = 2;
  • 正例:

  • ?
  • 1
  • select name, age from (select * from tab1 where id = 2) t1 left join tab2 t2 on t1.age = t2.age;
  • 理由:

    19. 如果插入数据过多,考虑 批量插入

    反例:

  • ?
  • 1
  • 2
  • for(user u :list)
  • { insert into user(name,age) values(name, age)}
  • 正例:

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • //一次500批量插入,分批进行
  • insert into user(name,age) values
  • <foreach collection="list" item="item" index="index" separator=",">
  •  (#{item.name},#{item.age})
  • </foreach>
  • 理由:

    20. 尽量少用 distinct 关键字

    distinct 关键字一般用来过滤重复记录,以返回不重复的记录。在查询一个字段或者很少字段的情况下使用时,给查询带来优化效果。但是在字段很多的时候使用,却会大大降低查询效率。

    反例:

  • ?
  • 1
  • 2
  • # 去重多个字段
  • select distinct * from  user;
  • 正例:

  • ?
  • 1
  • select distinct name from user;
  • 理由:

    21. 不要有超过5个以上的表连接

    理由:

    22. 数据量大的时候,如何优化更新语句。

    数据量大的时候,需要避免同时修改或删除过多数据,同时会造成cpu利用率过高,从而影响别人对数据库的访问。

    反例:

  • ?
  • 1
  • 2
  • 3
  • 4
  • # 一次删除10万或者100万+条数据
  • delete from user where id < 1000000;
  • # 或者采用单一循环操作,效率低,时间漫长
  • foruser user:list){delete from user;}
  • 正例:

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • # 分批进行删除,如每次500  
  • delete user where id < 500
  • delete user where id >= 500 and id < 1000;
  • ...
  • delete user where id >= 999500 and id < 1000000;
  • 理由:

    23. 合理使用 exist 和 in

    假设表a表示某企业的员工表,表b表示部门表,查询所有部门的所有员工sql

    反例::

  • ?
  • 1
  • select * from a where deptid in (select deptid from b);
  • 这样写等价于:

    先查询部门表b
    select deptid from b
    再由部门deptid,查询a的员工
    select * from a where a.deptid = b.deptid

    可以抽象成这样的一个循环语句:

  • ?
  • 标签:

    猜您喜欢