当前位置:网站首页>MySQL Optimization: from more than ten seconds to 300 milliseconds
MySQL Optimization: from more than ten seconds to 300 milliseconds
2022-06-30 06:52:00 【Dream~~】
Today's article still records the problem-solving process . When I tested the code yesterday , It takes about ten seconds to find a function . I just look at my own code logic , After a complete look at the code , According to the feeling, there are two reasons for the long time : One is mysql Writing problems , Another possible problem is the circular query of the database . The code dream will not be uploaded , It may involve the privacy of the company
The bottom is glued out sql part , You can see this sql all 7 More than seconds , The second picture is using explain Results after actuator , You can see that only two of the five tables are indexed , Other tables do not have indexes , You can see type The level of this column is also ALL.
The result value is from good to bad :system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL. Generally speaking , Make sure that the query is at least range Level , It's better to achieve ref, Otherwise, there may be performance problems . So this sql To be optimized .

How to optimize :
1, First combine type+key These two columns are used to optimize , That is, by increasing the index to optimize . I usually give it to where and order by Clause to add an index to a field . So I was there d Four indexes have been added to the table (deviceId、name、del_flag、project_id),c Table to prdfession Also added indexes .
2, Then according to Extra This column is optimized , You can see that the two columns in the above figure appear “Using temporary”、“Using filesort” In both cases .
“Using temporary”: refer to sql If order by perhaps group by The fields in are 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 federated index .
“Using filesort”: refer to order by The amount of field data queried exceeds mysql The default value of , Led to order by The fields in do not go through the index , File query is used , It is also the cause of performance bottlenecks . Solution : You can try to put the sorting function into the code ,sql Do not sort in .
When a normal index is created 、 Joint index (project_id + name) after , Look again. sql Execution speed of .


It can be seen that the execution speed is immediately increased ,explain When the command is executed , Also will find type A whole train has reached ref Level ,extra There is no train left “Using temporary”、“Using filesort” These two signs that affect performance .
Now? sql The optimization is complete , If the speed is still slow , Consider optimizing the logic of your code .
边栏推荐
- Record one time of Tencent Test Development Engineer's automation interface test practice experience
- 记录一次腾讯测试开发工程师自动化接口测试实践经验
- MySQL中的InnoDB引擎
- The solution of memcpy memory overlap
- GO安装以及配置(1)
- RT thread Kernel Implementation (IV): multi priority
- [Hot100]10. 正则表达式匹配
- Bat usage details 2
- CPU到底是怎么识别代码的?
- Introduction to neural networks
猜你喜欢

SOC_ SD_ CLK

六,购物⻋与订单

基础刷题(一)

SOC_SD_CLK

Pay attention to this live broadcast and learn about the path to achieve the dual carbon goal of the energy industry

Rising posture series: fancy debugging information

【Mask-RCNN】基于Mask-RCNN的目标检测和识别

Basic questions (I)

ROS system problem: rosdep init

First experience of Galaxy Kirin
随机推荐
RT thread application
Authority management system
ROS-URDF
RT thread Kernel Implementation (VI): time slice
1.4 - 定点数与浮点数
RT thread Kernel Implementation (III): implementation of idle threads and blocking delay
Analysis of startup process of gazebo multi computer simulation
Gazebo model modification
Cmake post makefile:32: * * * missing separator Stop.
Joseph problem C language
Judge whether H5 is in wechat environment or enterprise wechat environment at both ends
ROS system problem: rosdep init
Record one time of Tencent Test Development Engineer's automation interface test practice experience
Ffmplay is not generated during the compilation and installation of ffmpeg source code
Vscode returns the previous cursor (previous browse position)
The most complete sentence in history
KEIL - 下载调试出现“TRACE HW not present”
1.3 - 码制
关注这场直播,了解能源行业双碳目标实现路径
Several C language implementations