当前位置:网站首页>MySQL优化:从十几秒优化到三百毫秒
MySQL优化:从十几秒优化到三百毫秒
2022-08-02 07:24:00 【embelfe_segge】
今天这篇文章还是记录问题的解决过程的。昨天测试测试代码的时候,发现一个功能需要大概十几秒的时间才能执行完。我就去看自己的代码逻辑,完整看了代码之后,凭感觉导致时间长的原因有两部分:一个是mysql的书写问题,另一个可能是循环查询数据库的问题。代码梦梦就不上传了,可能会涉及到公司的隐私问题
下面是粘出来的sql部分,可以看到这个sql都7秒多了,第二张图片是使用explain执行器之后的结果,可以看到五张表只有两张表走了索引,其他的表没有走索引,可以看到type这一列的级别也是ALL。
结果值从好到坏依次是:system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL。一般来说,得保证查询至少达到range级别,最好能达到ref,否则就可能会出现性能问题。所以这个sql要优化了。

优化方式:
1,先结合type+key这两列来优化,也就是通过增加索引来优化。一般我会给在where 和order by 子句中用到字段添加索引。所以我就在 d 表上添加了四个索引(deviceId、name、del_flag、project_id),c 表给prdfession也添加了索引。
2,之后再根据Extra这一列进行优化,可以看到上图这两列出现了“Using temporary”、“Using filesort”这两种情况。
“Using temporary”:指的是sql中如果order by或者group by中的字段都来自于其它表而非连接顺序中的第一个表,就会创建一个临时表了。解决方案:可以尝试创建联合索引试试。
“Using filesort”:指的是order by 查询的字段数据量超出了mysql的默认值,导致了order by中的字段不走索引,使用了文件查询,也是导致性能瓶颈的原因。解决方案:可以尝试把排序功能放到代码中实现,sql中不做排序。
当创建了普通索引、联合索引(project_id + name)之后,再看一下sql的执行速度。


可以看到执行速度立马就提了上去,explain命令执行的时候,也会发现type一列全达到了ref级别,extra一列去没有了“Using temporary”、“Using filesort”这两个影响性能的标志。
现在sql的优化就完成了,如果速度还慢的话,就考虑优化代码逻辑的部分辽。
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- pnpm + workspace + changesets 构建你的 monorepo 工程
- 59: Chapter 5: Develop admin management services: 12: MongoDB usage scenarios; (non-core data, non-core data with a relatively large amount of data, small private files such as face photos;)
- 爬虫 视频爬取工具you-get
- RIP综合实验
- MPLS和BGP的综合实验
- MySQL batch update
- Control 'ContentPlaceHolder1_ddlDepartment' of type 'DropDownList' must be placed inside a form tag with runat=server.
- 数据中台:始于阿里,兴于DaaS
- MySQL - index explanation
- FormData upload binary file, object, object array
猜你喜欢

Buried development process

LeetCode 2360. The longest cycle in a graph

Splunk Filed extraction field interception

MySQL-数据库事务详解

mysql操作入门(四)-----数据排序(升序、降序、多字段排序)

Aided by training and learning by battle | The new version of the Offensive and Defense World Platform is officially launched!

FormData upload binary file, object, object array

概率论与数理统计

A Preliminary Study on the Basic Principles of Formal Methods

59: Chapter 5: Develop admin management services: 12: MongoDB usage scenarios; (non-core data, non-core data with a relatively large amount of data, small private files such as face photos;)
随机推荐
MySQL - Detailed Explanation of Database Transactions
CollectionUtil:一个函数式风格的集合工具
OC - NSSet (set)
Ask a question, my Flinkcdc has run through, I can monitor the binlog of msql, and I can also send kafk
概率论与数理统计
读入、输出优化
From cloud computing to function computing
View port number occupancy
HCIP 第八天
【Network】IP, subnet mask
LeetCode 2360. The longest cycle in a graph
Inverter insulation detection detection function and software implementation
HCIP第二天
A full review of mainstream timed task solutions
spark架构
停止精神内耗 每日分享
flutter 自己写一个组件
About the SQL concat () function problem, how to splice
Metasploit (MSF) Basic Super Detailed Edition
Understand the Chisel language. 30. Chisel advanced communication state machine (2) - FSMD: Take Popcount as an example