当前位置:网站首页>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
边栏推荐
- [R language] [3] apply, tapply, lapply, sapply, mapply and par function related parameters
- Hand in hand to realize the picture preview plug-in (3)
- Recursive implementation of the Tower of Hanoi problem
- 30 Years of Open Source Community | 2022 Open Atom Global Open Source Summit 30 Years of Open Source Community Special Event Held Successfully
- 简易网络文件拷贝的C实现
- Go language study notes - dealing with timeout problems - Context usage | Go language from scratch
- VScode+ESP32 quickly install ESP-IDF plugin
- Puzzle Game Level Design: Reverse Method--Explaining Puzzle Game Level Design
- The third is the code to achieve
- .NET-9.乱七八糟的理论笔记(概念,思想)
猜你喜欢

Reinforcement learning: from entry to pit to shit

The MySQL database installed configuration nanny level tutorial for 8.0.29 (for example) have hands

ERP生产作业控制 金蝶

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

HCIP第十天_BGP路由汇总实验

MySQL database backup

微信小程序使用云函数更新和添加云数据库嵌套数组元素

On Governance and Innovation | 2022 OpenAtom Global Open Source Summit OpenAnolis sub-forum was successfully held

马斯克对话“虚拟版”马斯克,脑机交互技术离我们有多远

产学研用 共建开源人才生态 | 2022开放原子全球开源峰会教育分论坛圆满召开
随机推荐
XSS靶场(三)prompt to win
XSS shooting range (3) prompt to win
Minesweeper game - C language
Sql解析转换之JSqlParse完整介绍
Can't load /home/Iot/.rnd into RNG
sql语句-如何以一个表中的数据为条件据查询另一个表中的数据
Unity框架设计系列:Unity 如何设计网络框架
高斯分布及其极大似然估计
npm、nrm两种方式查看源和切换镜像
MySQL database must add, delete, search and modify operations (CRUD)
开放原子开源基金会秘书长孙文龙 | 凝心聚力,共拓开源
Visual studio shortcuts that improve efficiency, summary (updated from time to time)
sql语句之多表查询
Fusion Cloud Native, Empowering New Milestones | 2022 Open Atom Global Open Source Summit Cloud Native Sub-Forum Successfully Held
关于出现大量close_wait状态的理解
已解决:不小心卸载pip后(手动安装pip的两种方式)
打造基于ILRuntime热更新的组件化开发
STM32HAL库修改Hal_Delay为us级延时
Unity打灵狐者
HCIP Day 10_BGP Route Summary Experiment