当前位置:网站首页>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 .
边栏推荐
猜你喜欢

Never forget the original intention, and be lazy if you can: C # operate word files

Force buckle ------ replace blank space

1.9 - Cache

不忘初心,能偷懒就偷懒:C#操作Word文件

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

KEIL - 下载调试出现“TRACE HW not present”

Introduction to neural networks

1.6 - CPU composition
![[fuzzy neural network] mobile robot path planning based on Fuzzy Neural Network](/img/39/c9bf235828b6d50e7931876fa4a918.png)
[fuzzy neural network] mobile robot path planning based on Fuzzy Neural Network

ROS service communication programming
随机推荐
InnoDB engine in MySQL
[JSON tutorial] Chapter 1 learning notes
RT thread Kernel Implementation (III): implementation of idle threads and blocking delay
The 40g high-efficiency cloud disk purchased by Alibaba cloud is only 20g attached
第一行代码(第三版)学习笔记
Vscode returns the previous cursor (previous browse position)
Redis cache
Hao looking for a job
RT thread migration to s5p4418 (I): scheduler
SOC project AHB_ SD_ Host controller design
Judge whether H5 is in wechat environment or enterprise wechat environment at both ends
What if I forget my account number after opening an account? Is it safe to open an account online?
CPU到底是怎么识别代码的?
0 basic job transfer software test, how to achieve a monthly salary of 9.5k+
RT thread Kernel Implementation (I): threads and scheduling
Cmake post makefile:32: * * * missing separator Stop.
关注这场直播,了解能源行业双碳目标实现路径
1.3 - Code System
银河麒麟初体验
leetcode:98. Validate binary search tree