当前位置:网站首页>MySQL optimization slow log query
MySQL optimization slow log query
2022-07-31 04:53:00 【jiey0407】
Article Directory
I. Concept of slow query log
For SQL and index optimization problems, we will use explain to analyze the SQL statement.But a real enterprise-level project has thousands of pieces of SQL, and it is impossible for us to analyze it one by one from scratch.Where can we get those long-running, performance-hungry SQL??
We can open the Slow Query Log:
Estimate a time limit (20ms, 100ms) according to the specific business and concurrency. After setting the business, open the slow query log after the stress test, and you will see the SQL that exceeds the execution time, and then use explain to analyzeThese time-consuming SQL statements
The steps are as follows:
- Turn on the slow query log switch
slow_query_log - Set a reasonable and business-acceptable slow query time limit
- Pressure testing to perform various services
- View the slow query log to find all time-consuming SQL statements
- Analyze these time-consuming SQL statements with explain for targeted optimization
MySQL can set a slow query log. When the SQL execution time exceeds the time we set, these SQL will be recorded in the slow query log. Then we can view the log and analyze the execution plan of these SQL with explain.To determine why the efficiency is low, is the index not used?Or is there something wrong with the index itself?Or the index is used, but because the amount of data in the table is too large, it takes a long time, then we can divide the table into multiple small tables and so on.
The parameters related to the slow query log are as follows:
(Many global switches defined by MySQL are stored in global variables, which can be viewed or set with show/set variablesvalue of a global variable)

The slow query log switch is disabled by default
The path of the slow query log: the default is under /var/lib/mysql/
The slow query log records the log containing all SQL statements whose execution time exceeds the value set by the parameter long_query_time (unit: second), which can be viewed with the command on MySQL, as follows:

This value can be modified:

Second, slow query log practice
1. Turn on the slow query log switch slow_query_log

When the slow query log switch is turned on, the error message indicates that slow_query_log is a global variable (there are also variables that only affect the current session, such as: long_query_time, profiling). After modification, it will affect all sessions, that is, it will affect all currently accessing the current session.Client of MySQL server.
Open the slow query log switch successfully!
2. Set a reasonable and business-acceptable slow query time upper limit long_query_time

View another session

It is found that it is still the default 10s, so long_query_time only affects the currentsession
3. Execute various business under pressure test

has exceeded our long_query_time=0.1s
4. View slow query log
Path: /var/lib/mysql/

5. Use explain to analyze these time-consuming SQL statements for targeted optimization

Do a search of the entire table, put the primary key index treeScan the whole thing.
We should add an index to password, and then remember that password is in string format, because if it involves type conversion, the index will not be used
Third, show profiles to view the specific running time of sql
MySQL generally only displays the time with two decimal places
Turn on the profiling switch to display more detailed time

No error is reported, indicating that the profiling variable only affects the current session

Let me introduce myself first. The editor graduated from Shanghai Jiaotong University in 2013. I worked in a small company and went to big factories such as Huawei and OPPO. I joined Alibaba 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
边栏推荐
- 110道 MySQL面试题及答案 (持续更新)
- Lua,ILRuntime, HybridCLR(wolong)/huatuo hot update comparative analysis
- Two address pools r2 are responsible for managing the address pool r1 is responsible for managing dhcp relays
- 【C语言】操作符详解
- 1. Get data - requests.get()
- Understanding of the presence of a large number of close_wait states
- MySQL optimization: from ten seconds to three hundred milliseconds
- [R language] [3] apply, tapply, lapply, sapply, mapply and par function related parameters
- A complete introduction to JSqlParse of Sql parsing and conversion
- prompt.ml/15中<svg>标签使用解释
猜你喜欢

The third is the code to achieve

三道leetcode上的oj题

HCIP第十天_BGP路由汇总实验

重磅 | 开放原子校源行活动正式启动

【云原生】DevOps(五):集成Harbor

From scratch, a mirror to the end, a pure system builds a grasscutter (Grasscutter)

ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your

Win10 CUDA CUDNN installation configuration (torch paddlepaddle)

Explanation of

Unity资源管理系列:Unity 框架如何做好资源管理
随机推荐
MySQL优化之慢日志查询
Three oj questions on leetcode
MySQL事务(transaction) (有这篇就足够了..)
SOLVED: After accidentally uninstalling pip (two ways to manually install pip)
Recursive implementation of the Tower of Hanoi problem
sql statement - how to query data in another table based on the data in one table
[C language] Detailed explanation of operators
Doris学习笔记之监控
开放原子开源基金会秘书长孙文龙 | 凝心聚力,共拓开源
MySQL数据库备份
BUG destroyer!!Practical debugging skills are super comprehensive
Basic knowledge of mysql (2)
行业落地呈现新进展 | 2022开放原子全球开源峰会OpenAtom OpenHarmony分论坛圆满召开
Lua,ILRuntime, HybridCLR(wolong)/huatuo hot update comparative analysis
MySQL database backup
产学研用 共建开源人才生态 | 2022开放原子全球开源峰会教育分论坛圆满召开
CentOS7 安装MySQL 图文详细教程
数字经济时代的开源数据库创新 | 2022开放原子全球开源峰会数据库分论坛圆满召开
1. Get data - requests.get()
110道 MySQL面试题及答案 (持续更新)