程序员

打造出色查询:如何优化SQL查询?

作者:admin 2021-05-11 我要评论

本文转载自公众号读芯术(ID:AI_Discovery)。 我们致力于打造能够较好运行并延续较长一段时间的query(查询)。本文将给出关于优化SQL语句的几点建议,希望能够帮...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)

本文转载自公众号“读芯术”(ID:AI_Discovery)。

我们致力于打造能够较好运行并延续较长一段时间的query(查询)。本文将给出关于优化SQL语句的几点建议,希望能够帮到你。

1. 尝试不去用select *来查询SQL,而是选择专用字段。

反例:

  1. select * from employee; 

正例:

  1. select id,name fromemployee; 

理由:

  • 通过只用必要字段进行查询,能够节省资源并减少网络开销。
  • 这样做可能不会使用覆盖索引,会导致一个查询返回到表中。

2. 如果已知只有一个查询结果,推荐使用limit 1

假设有一张员工表格,想在其中找到一名叫jay的员工。

  1. CREATE TABLE employee ( 
  2. id int(11) NOT NULL, 
  3. name varchar(255) DEFAULT NULL, 
  4. age int(11) DEFAULT NULL, 
  5. date datetime DEFAULT NULL, 
  6. sex int(1) DEFAULT NULL, 
  7. PRIMARY KEY (`id`) ); 

反例:

  1. select id,name from employeewhere name='jay'

正例:

  1. select id,name from employeewhere name='jay' limit 1; 

理由:添加limit 1后,查找到相应的记录时,便不会继续查找下去,效率会大大提高。

3. 尝试避免在 where 子句中使用or来连接条件

创建一个新的用户表格,其有一个常规索引userId,表格结构如下:

  1. CREATE TABLE `user` ( 
  2.   `id` int(11) NOT NULL AUTO_INCREMENT, 
  3.   `userId` int(11) NOT NULL, 
  4.   `age` int(11) NOT NULL, 
  5.   `name` varchar(255) NOT NULL, 
  6.   PRIMARY KEY (`id`), 
  7.   KEY `idx_userId` (`userId`) ) 

现在假设需要查询userid为1或为18岁的用户,使用如下的SQL就会很简单。

反例:

  1. select * from user where userid = 1 or age = 18

正例:

  1. //se union all 
  2. select * from user where userid=1 
  3. union all 
  4. select * from user where age = 18;//Or write two separate SQL 
  5. select * from user where userid=1

理由:or 的使用可能会使索引无效,因此需要进行全表扫描。

在or 无索引的情况下,假设已采用userId索引,但是当涉及到 age(年龄)查询条件时,必须执行全表扫描,其过程分为三步:全表扫描+索引扫描+合并。

打造出色查询:如何优化SQL查询?
图源:unsplash

4. 尽可能避免在where子句中使用!=或<>运算符,否则,引擎将放弃使用索引并执行全表扫描。

反例:

  1. select age,name from user where age<>18; 

正例:

  1. //You can consider separate two sql writeselect age,name from user where age <18
  2. select age,name from user where age>18; 

理由:使用!=和<>可能使索引无效。

5. 优化limit分页

通常用limits来实现日常分页,但当偏移量特别大时,查询效率便会降低。因为Mysql不会跳过偏移量,而是直接获取数据。

反例:

  1. select id,name,age from employeelimit 10000,10; 

正例:

  1. //Solution 1: Return the largest record (offset) of the last query 
  2. select id,name from employeewhere id>10000 limit 10;//Solution 2: order by + index 
  3. select id,name from employeeorder by id limit 10000,10; 

理由:

  • 如果使用了优化方案1,则会返回最末的查询记录(偏移量),因此可以跳过该偏移量,效率自然会大幅提高。
  • 选项二:使用+索引排序,也可以提高查询效率。

6. 优化like语句

在日常开发中,如果使用模糊关键字查询,我们很容易想到like,但like可能会使索引无效。

反例:

  1. select userId,name from user where userId like '%123'; 

正例:

  1. select userId,name from user where userId like '123%'; 

理由:https://medium.com/@pawanjain.432/hey-thanks-dovid-for-pointing-out-a-typo-in-13-1000a4103fe6

7. 使用where条件限制将要查询的数据来避免返回额外行

假设要查询一名用户是否为会员,老式执行代码会这样做。

反例:

  1. List<Long> userIds = sqlMap.queryList("select userId from userwhere isVip=1");boolean isVip = userIds.contains(userId); 

正例:

  1. Long userId = sqlMap.queryObject("select userId from user whereuserId='userId' and isVip='1' ")boolean isVip = userId!=null; 

理由:能够检查需要的数据,避免返回非必要数据,并能节省费用和计算机开销。

打造出色查询:如何优化SQL查询?
图源:unsplash

8. 考虑在where子句中使用默认值而不是null

反例:

  1. select * from user where age is not null; 

正例:

  1. select * from user where age>0; //Set 0 as default 

理由:如果用默认值取代null值,则通常可以建立索引,与此同时,表达式将相对清晰。

9. 如果插入数据过多,可以考虑批量插入

反例:

  1. for(User 
  2.     u :list){ 
  3. INSERT into user(name,age)values(#name#,#age#) 

正例:

  1. //One batch of 500 inserts, carried out in batchesinsert intouser(name,age) values 
  2. <foreach collection="list" item="item"index="index" separator=","> 
  3.     (#{item.name},#{item.age}) 
  4. </foreach> 

理由:批量插入性能良好且省时。

打个比方,在有电梯的情况下,你需要将1万块砖移送到建筑物的顶部。电梯一次可以放置适当数量的砖块(最多500块),你可以选择一次运送一块砖,也可以一次运送500块。哪种方案更好?

10. 谨慎使用distinct关键词

Distinct关键词通常用于过滤重复记录以返回唯一记录。当其被用于查询一个或几个字段时,Distinct关键词将为查询带来优化效果。然而,在字段过多的情况下,Distinct关键词将大大降低查询效率。

反例:

  1. SELECT DISTINCT * from user; 

正例:

  1. select DISTINCT name from user; 

理由:带有“distinct”语句的CPU时间和占用时间高于没有“ distinct”的语句。

如果在查询多字段时使用distinct,数据库引擎将比较数据,并滤除重复的数据。然而,该比较和滤除过程将消耗系统资源和CPU时间。

打造出色查询:如何优化SQL查询?
图源:unsplash

11. 删除多余和重复的索引

反例:

  1. KEY `idx_userId` (`userId`) 
  2. KEY `idx_userId_age` (`userId`,`age`) 

正例:

  1. //Delete the userId index, because the combined index (A, B) is equivalentto creating the (A) and (A, B) indexesKEY `idx_userId_age` (`userId`,`age`) 

理由:若保留重复的索引,那么优化程序在优化查询时也需要对其进行一一考量,这会影响性能。

12. 如果数据量很大,优化 modify或delete语句

避免同时修改或删除过多数据,因其将导致CPU利用率过高,从而影响他人对数据库的访问。

反例:

  1. //Delete 100,000 or 1 million+ at a time? 
  2. delete from user where id <100000;//Or use single cycle operation, lowefficiency and long time 
  3. for(User user:list){ 
  4.    delete from user;} 

正例:

  1. //Delete in batches, such as 500 each timedelete user where id<500
  2. delete product where id>=500 and id<1000; 

理由:一次删除过多数据,可能会导致lock wait timeout exceed error(锁定等待超时错误),因此建议分批操作。

13. 使用explain分析SQL方案

在日常开发中编写SQL时,尝试养成习惯:使用explain来分析自己编写的SQL,尤其是索引。

  1. explain select * from user where userid = 10086 or age =18

14. 尝试用union all代替union

如果搜索结果里没有重复的记录,我推荐用union all代替union。

反例:

  1. select * from user where userid=1 
  2. union 
  3. select * from user where age = 10 

正例:

  1. select * from user where userid=1 
  2. union all 
  3. select * from user where age = 10 

理由:

  • 如果使用union,则无论有没有重复的搜索结果,它都会尝试对其进行合并、排序,然后输出最终结果。
  • 若已知搜索结果中没有重复记录,用union all代替union将提高效率。

15. 尽可能使用数字字段。如果字段仅包含数字信息,尝试不将其设置为字符类型。

反例:

  1. `king_id` varchar(20) NOT NULL; 

正例:

  1. `king_id` int(11) NOT NULL; 

理由:与数字字段相比,字符类型将降低查询和连接的性能,并会增加存储开销。

16. 尽可能用varchar或nvarchar代替char或nchar

反例:

  1. `deptName` char(100) DEFAULT NULL 

正例:

  1. `deptName` varchar(100) DEFAULT NULL 

理由:

  • 首先,由于可变长度字段的存储空间很小,该方法可以节省存储空间。
  • 其次,对于查询而言,在相对较小的字段中搜索会更有效率。

优化和加速SQL查询是门技术活,常常思考和尝试,你会打开新世界的大门。

【责任编辑:赵宁宁 TEL:(010)68476606】
本文转载自网络,原文链接:https://www.toutiao.com/i6848447745120797187/

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • “鸿蒙设备开发”选“址” --&gt;

    “鸿蒙设备开发”选“址” --&gt;

  • Nextcloud是如何成为终极开源生产力套

    Nextcloud是如何成为终极开源生产力套

  • 手把手教你用Pycharm连接远程Python环

    手把手教你用Pycharm连接远程Python环

  • Windows 10X镜像生成工具发布:任意PC

    Windows 10X镜像生成工具发布:任意PC