当前位置:网站首页>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
边栏推荐
- 马斯克对话“虚拟版”马斯克,脑机交互技术离我们有多远
- ERROR 1819 (HY000) Your password does not satisfy the current policy requirements
- MySQL to revise the root password
- 扫雷小游戏——C语言
- MySQL database installation (detailed)
- SOLVED: After accidentally uninstalling pip (two ways to manually install pip)
- 微信小程序使用云函数更新和添加云数据库嵌套数组元素
- ERROR 1819 (HY000) Your password does not satisfy the current policy requirements
- mysql基础知识(二)
- 两个地址池r2负责管地址池r1负责管dhcp中继
猜你喜欢

三子棋的代码实现
![[debug highlights] Expected input batch_size (1) to match target batch_size (0)](/img/b3/ff6ccc3cd307befad3bd07a9f4a956.png)
[debug highlights] Expected input batch_size (1) to match target batch_size (0)

ES 源码 API调用链路源码分析

MATLAB/Simulink & & STM32CubeMX tool chain completes model-based design development (MBD) (three)

开放原子开源基金会秘书长孙文龙 | 凝心聚力,共拓开源

递归实现汉诺塔问题

MySQL数据库备份

Two address pools r2 are responsible for managing the address pool r1 is responsible for managing dhcp relays

The third is the code to achieve

益智类游戏关卡设计:逆推法--巧解益智类游戏关卡设计
随机推荐
ENSP, VLAN division, static routing, comprehensive configuration of Layer 3 switches
MySQL to revise the root password
On Governance and Innovation | 2022 OpenAtom Global Open Source Summit OpenAnolis sub-forum was successfully held
Win10 CUDA CUDNN installation configuration (torch paddlepaddle)
BUG消灭者!!实用调试技巧超全整理
.NET-6.WinForm2.NanUI学习和总结
MySQL开窗函数
Open Source Smart Future | 2022 OpenAtom Global Open Source Summit OpenAtom openEuler sub-forum was successfully held
MATLAB/Simulink&&STM32CubeMX工具链完成基于模型的设计开发(MBD)(三)
el-image标签绑定点击事件后没有有用
mysql使用on duplicate key update批量更新数据
重磅 | 开放原子校源行活动正式启动
.NET-9.乱七八糟的理论笔记(概念,思想)
MySQL忘记密码怎么办
ABC D - Distinct Trio(k元组的个数
MySQL基础操作
ERROR 2003 (HY000) Can't connect to MySQL server on 'localhost3306' (10061)
Explanation of
开放原子开源基金会秘书长孙文龙 | 凝心聚力,共拓开源
ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)