当前位置:网站首页>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
边栏推荐
- Lua,ILRuntime, HybridCLR(wolong)/huatuo热更新对比分析
- el-image标签绑定点击事件后没有有用
- mysql使用on duplicate key update批量更新数据
- WeChat applet uses cloud functions to update and add cloud database nested array elements
- 行业落地呈现新进展 | 2022开放原子全球开源峰会OpenAtom OpenHarmony分论坛圆满召开
- 已解决(最新版selenium框架元素定位报错)NameError: name ‘By‘ is not defined
- Error EPERM operation not permitted, mkdir 'Dsoftwarenodejsnode_cache_cacach Two solutions
- Mysql应用安装后找不到my.ini文件
- 【线性神经网络】softmax回归
- ENSP, VLAN division, static routing, comprehensive configuration of Layer 3 switches
猜你喜欢

input输入框展示两位小数之precision

mysql使用on duplicate key update批量更新数据

Fusion Cloud Native, Empowering New Milestones | 2022 Open Atom Global Open Source Summit Cloud Native Sub-Forum Successfully Held

SOLVED: After accidentally uninstalling pip (two ways to manually install pip)

ERROR 2003 (HY000) Can't connect to MySQL server on 'localhost3306' (10061)Solution

Blockbuster | foundation for platinum, gold, silver gave nameboards donors

三子棋的代码实现

Unity资源管理系列:Unity 框架如何做好资源管理

ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)解决办法

ERROR 1819 (HY000) Your password does not satisfy the current policy requirements
随机推荐
Unity URP渲染管线摄像机核心机制剖析
PCL calculates the point cloud coordinate maximum and its index
两个地址池r2负责管地址池r1负责管dhcp中继
MySQL database addition, deletion, modification and query (detailed explanation of basic operation commands)
Two address pools r2 are responsible for managing the address pool r1 is responsible for managing dhcp relays
MySQL数据库增删改查(基础操作命令详解)
ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your
The 15th day of the special assault version of the sword offer
【R语言】【3】apply,tapply,lapply,sapply,mapply与par函数相关参数
el-image tag doesn't work after binding click event
ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your
三子棋的代码实现
MySQL修改root账号密码
MySQL常见面试题汇总(建议收藏!!!)
【wpf】wpf中的那些模板之深度解析
30 Years of Open Source Community | 2022 Open Atom Global Open Source Summit 30 Years of Open Source Community Special Event Held Successfully
MySQL database installation (detailed)
VScode+ESP32 quickly install ESP-IDF plugin
MySQL基础操作
手把手实现图片预览插件(三)