当前位置:网站首页>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
边栏推荐
- Industry landing presents new progress | 2022 OpenAtom Global Open Source Summit OpenAtom OpenHarmony sub-forum was successfully held
- The input input box displays the precision of two decimal places
- Win10 CUDA CUDNN installation configuration (torch paddlepaddle)
- 简易网络文件拷贝的C实现
- MySQL database addition, deletion, modification and query (detailed explanation of basic operation commands)
- Win10 CUDA CUDNN 安装配置(torch paddlepaddle)
- Solved (the latest version of selenium framework element positioning error) NameError: name 'By' is not defined
- 从零开始,一镜到底,纯净系统搭建除草机(Grasscutter)
- 产学研用 共建开源人才生态 | 2022开放原子全球开源峰会教育分论坛圆满召开
- sql语句之多表查询
猜你喜欢
Mysql应用安装后找不到my.ini文件
Musk talks to the "virtual version" of Musk, how far is the brain-computer interaction technology from us
PWN ROP
Hand in hand to realize the picture preview plug-in (3)
MySQL忘记密码怎么办
矩池云快速安装torch-sparse、torch-geometric等包
MATLAB/Simulink&&STM32CubeMX工具链完成基于模型的设计开发(MBD)(三)
The input input box displays the precision of two decimal places
开源汇智创未来 | 2022开放原子全球开源峰会OpenAtom openEuler分论坛圆满召开
Puzzle Game Level Design: Reverse Method--Explaining Puzzle Game Level Design
随机推荐
mysql数据库安装(详细)
Unity资源管理系列:Unity 框架如何做好资源管理
C Implementation of Simple Network File Copy
简易网络文件拷贝的C实现
重磅 | 开放原子校源行活动正式启动
【云原生】DevOps(五):集成Harbor
聚变云原生,赋能新里程 | 2022开放原子全球开源峰会云原生分论坛圆满召开
DVWA安装教程(懂你的不懂·详细)
.NET-6.WinForm2.NanUI学习和总结
Reinforcement learning: from entry to pit to shit
Win10 CUDA CUDNN installation configuration (torch paddlepaddle)
Go语学习笔记 - 处理超时问题 - Context使用 | 从零开始Go语言
BUG destroyer!!Practical debugging skills are super comprehensive
sql语句-如何以一个表中的数据为条件据查询另一个表中的数据
【debug锦集】Expected input batch_size (1) to match target batch_size (0)
ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)解决办法
论治理与创新 | 2022开放原子全球开源峰会OpenAnolis分论坛圆满召开
ABC D - Distinct Trio(k元组的个数
Unity教程:URP渲染管线实战教程系列【1】
专访 | 阿里巴巴首席技术官程立:云+开源共同形成数字世界的可信基础