当前位置:网站首页>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 variables
value 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
边栏推荐
- Minesweeper game (written in c language)
- 微信小程序使用云函数更新和添加云数据库嵌套数组元素
- [C language] Detailed explanation of operators
- MySQL优化:从十几秒优化到三百毫秒
- Win10 CUDA CUDNN installation configuration (torch paddlepaddle)
- Visual studio shortcuts that improve efficiency, summary (updated from time to time)
- MySQL常见面试题汇总(建议收藏!!!)
- HCIP Day 10_BGP Route Summary Experiment
- 高斯分布及其极大似然估计
- ENSP, VLAN division, static routing, comprehensive configuration of Layer 3 switches
猜你喜欢
npm、nrm两种方式查看源和切换镜像
Unity框架设计系列:Unity 如何设计网络框架
【debug锦集】Expected input batch_size (1) to match target batch_size (0)
从零开始,一镜到底,纯净系统搭建除草机(Grasscutter)
CentOS7 install MySQL graphic detailed tutorial
SQL row-column conversion
Doris学习笔记之监控
Vue项目通过node连接MySQL数据库并实现增删改查操作
Minesweeper game - C language
Minesweeper game (written in c language)
随机推荐
PWN ROP
Blockbuster | foundation for platinum, gold, silver gave nameboards donors
A complete introduction to JSqlParse of Sql parsing and conversion
参考代码系列_1.各种语言的Hello World
【py脚本】批量二值化处理图像
ERP Production Operation Control Kingdee
The idea project obviously has dependencies, but the file is not displayed, Cannot resolve symbol 'XXX'
CentOS7 —— yum安装mysql
open failed: EACCES (Permission denied)
MySQL优化:从十几秒优化到三百毫秒
重磅 | 基金会为白金、黄金、白银捐赠人授牌
PCL 计算点云坐标最值及其索引
Solved (the latest version of selenium framework element positioning error) NameError: name 'By' is not defined
SOLVED: After accidentally uninstalling pip (two ways to manually install pip)
Win10 CUDA CUDNN installation configuration (torch paddlepaddle)
Musk talks to the "virtual version" of Musk, how far is the brain-computer interaction technology from us
input输入框展示两位小数之precision
MySQL database must add, delete, search and modify operations (CRUD)
高斯分布及其极大似然估计
Doris学习笔记之监控