当前位置:网站首页>MySQL optimization: from ten seconds to three hundred milliseconds
MySQL optimization: from ten seconds to three hundred milliseconds
2022-07-31 04:52:00 【jiey0407】
Today's article is still a record of the problem solving process.When I tested the test code yesterday, I found that a function takes about ten seconds to execute.I went to see my own code logic. After reading the code completely, I felt that there are two reasons for the long time: one is the writing problem of mysql, and the other may be the problem of cyclically querying the database.The code Mengmeng will not be uploaded, which may involve the company's privacy issues
The following is the glued sql part. You can see that this sql is more than 7 seconds. The second picture is the result after using the explain executor. You can see that only two of the five tables are indexed.The table is not indexed, and you can see that the level of the type column is also ALL.
The resulting values, from good to bad, are: system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL.Generally speaking, it is necessary to ensure that the query reaches at least the range level, preferably ref, otherwise performance problems may occur.So this sql needs to be optimized.

Optimization method:
1. First, optimize by combining the two columns of type+key, that is, by adding an index to optimize.Typically I would add indexes to the fields used in the where and order by clauses.So I added four indexes (deviceId, name, del_flag, project_id) to the d table, and the c table also added an index to prdfession.
2, and then optimize according to the Extra column. You can see that "Using temporary" and "Using filesort" appear in the two columns above.
"Using temporary": It means that if the fields in order by or group by in sql come from other tables instead of the first table in the join order, a temporary table will be created.Solution: You can try to create a joint index.
"Using filesort": refers to the fact that the amount of field data in the order by query exceeds the default value of mysql, which causes the fields in the order by to not be indexed, and the file query is used, which is also the cause of the performance bottleneck.Solution: You can try to implement the sorting function in the code, and do not do sorting in sql.
After creating a common index and a joint index (project_id + name), look at the execution speed of sql.


You can see that the execution speed has been improved immediately. When the explain command is executed, it will also be found that the type column has reached the ref level, and the extra column has no "Using temporary" and "Using filesort", which affects performance.symbols of.
Now the optimization of sql is completed, if the speed is still slow, consider optimizing part of the code logic.
Let me introduce myself first. The editor graduated from Jiaotong University in 2013. I worked in a small company and went to big factories such as Huawei and OPPO. I joined Ali in 2018, until now.I know that most junior and intermediate java engineers want to upgrade their skills, they often need to explore their own growth or sign up to study, but for training institutions, the tuition fee is nearly 10,000 yuan, which is really stressful.Self-learning that is not systematic is very inefficient and lengthy, and it is easy to hit the ceiling and the technology stops.Therefore, I collected a "full set of learning materials for java development" for everyone. The original intention is also very simple. I hope to help friends who want to learn by themselves but don't know where to start, and at the same time reduce everyone's burden.Add the business card below to get a full set of learning materials
边栏推荐
- 行业落地呈现新进展 | 2022开放原子全球开源峰会OpenAtom OpenHarmony分论坛圆满召开
- HCIP Day 10_BGP Route Summary Experiment
- el-image标签绑定点击事件后没有有用
- MySQL database installation (detailed)
- Understanding of the presence of a large number of close_wait states
- ERROR 2003 (HY000) Can't connect to MySQL server on 'localhost3306' (10061)Solution
- VScode+ESP32快速安装ESP-IDF插件
- DVWA靶场环境搭建
- 【py脚本】批量二值化处理图像
- 【C语言进阶】文件操作(一)
猜你喜欢

DVWA之SQL注入

ENSP,划分VLAN、静态路由,三层交换机综合配置
![[R language] [3] apply, tapply, lapply, sapply, mapply and par function related parameters](/img/98/282d8d192f701ad33de48aeeb038b4.png)
[R language] [3] apply, tapply, lapply, sapply, mapply and par function related parameters

Win10 CUDA CUDNN installation configuration (torch paddlepaddle)

Unity打灵狐者

两个地址池r2负责管地址池r1负责管dhcp中继

Blockbuster | foundation for platinum, gold, silver gave nameboards donors

Unity资源管理系列:Unity 框架如何做好资源管理

C语言表白代码?

打造基于ILRuntime热更新的组件化开发
随机推荐
Doris学习笔记之监控
ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your
ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)
开源汇智创未来 | 2022开放原子全球开源峰会OpenAtom openEuler分论坛圆满召开
参考代码系列_1.各种语言的Hello World
DVWA靶场环境搭建
el-image tag doesn't work after binding click event
ERROR 2003 (HY000) Can't connect to MySQL server on 'localhost3306' (10061)
Explanation of
ENSP,划分VLAN、静态路由,三层交换机综合配置
PWN ROP
产学研用 共建开源人才生态 | 2022开放原子全球开源峰会教育分论坛圆满召开
Lua,ILRuntime, HybridCLR(wolong)/huatuo hot update comparative analysis
【云原生】DevOps(五):集成Harbor
MySQL to revise the root password
高斯分布及其极大似然估计
30 Years of Open Source Community | 2022 Open Atom Global Open Source Summit 30 Years of Open Source Community Special Event Held Successfully
Interview | Cheng Li, CTO of Alibaba: Cloud + open source together form a credible foundation for the digital world
XSS shooting range (3) prompt to win
Heavyweight | The Open Atomic School Source Line activity was officially launched