当前位置:网站首页>MySQL optimization tutorial: slow query log practice
MySQL optimization tutorial: slow query log practice
2022-06-09 19:13:00 【1024 Q】
about SQL And index optimization , We will use explain To analyze SQL sentence . But there are thousands of real enterprise projects SQL, We can't start from scratch, one by one explain To analyze . Where can we get those long running , Performance consuming SQL??
We can open it Slow query log :
Estimate a time limit according to the specific business and concurrency (20ms、100ms), Start the business after setting , After the pressure test, open the slow query log , You will see that the execution time is exceeded SQL, And then use explain Analyze these time-consuming SQL sentence
Steps are as follows :
Turn on the slow query log switch slow_query_log
Reasonable setting 、 The upper limit of slow query time acceptable to the business
Pressure measurement performs various businesses
View slow query log , Find out all the time-consuming SQL sentence
use explain Analyze these time-consuming SQL sentence , So as to optimize
MySQL You can set slow query log , When SQL The execution time exceeds the time we set , So these SQL It will be recorded in the slow query log , Then we check the log , use explain Analyze these SQL Implementation plan of , To determine why it's inefficient , The index is not used ? There is still a problem with the creation of the index itself ? Or the index is used , However, due to the large amount of data in the table , It takes a long time , At this time, we can divide the table into several small tables .
The parameters related to slow query log are as follows :
(MySQL Many global switches defined , Are stored in global variables , It can be used show/set variables View or set the value of global variable )

The slow query log switch is off by default
Slow query log path : Default in /var/lib/mysql/ Next
The slow query log contains all the execution time exceeding the parameter long_query_time( Company : second ) Of the set value SQL Statement log , stay MySQL Use the command to view , as follows :

This value can be modified :

1. Turn on the slow query log switch slow_query_log

When turning on the slow query log switch , Error indication slow_query_log It's a global The variable of ( Others only affect the current session The variable of , Such as :long_query_time 、profiling), The modification will affect all session, That is, it affects all users who are accessing the current MySQL server The client of .
Turn on the slow query log switch successfully !
2. Reasonable setting 、 The upper limit of slow query time acceptable to the business long_query_time

View another session

Discovery is still default 10s, so long_query_time It only affects the present session
3. Pressure measurement performs various businesses

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

5. use explain Analyze these time-consuming SQL sentence , So as to optimize

Did a search of the whole table , Sweep the whole primary key index tree .
We should give password Add index , And remember password It's a string format , Because if type conversion is involved, the index cannot be used
3、 ... and 、show profiles see sql Specific running time
MySQL Generally, only the time of two decimal places is displayed 
open profiling switch , Show more detailed time

No report error , explain profiling Variables only affect the current session

边栏推荐
- SQL exercise 4: string processing function
- Gee | seeking improved version of NDVI
- US Secretary of Commerce: consider adding more Chinese enterprises to the "blacklist" and will not relax sanctions in the near future
- [pb03f environment setup] Bluetooth 5.2 Anxin Ke pb-03f-kit development board secondary development environment setup
- Microgrid digital twin 𞓜 intelligent era, deploy the integrated control platform of source network load and storage
- 投资5.5亿元!默克宣布在中国张家港建先进半导体一体化基地
- 明细表1字符串拼接合并插入到明细表2SQL输出过程记录
- Schedule 1 string splicing and inserting into schedule 2sql output process record
- How about opening an account for flush stock? Is it safe to open an account?
- 前美联储高级经济学家胡捷:从USDD升级看未来金融趋势
猜你喜欢

前美联储高级经济学家胡捷:从USDD升级看未来金融趋势

Why is cloud native data Lake worth paying attention to?

My younger brother Dan Teng, yincuisiting, is a robot human typewriter~~~~~~

散列表 分布式散列表(DHT)哈希表

mfc连接数据库显示未发现数据源名称并且未指定默认驱动程序
windows下mysql 8.0.27 安装配置图文教程

GNN如何泛化?这篇135页pdf博士论文《神经图推理与学习的泛化评估与改进》

24个月暴涨180万名开发者,Rust 迎来高光时刻

二叉树的按层遍历

Hu Jie, former senior economist of the Federal Reserve: future financial trends from the upgrading of usdd
随机推荐
CL210OpenStack操作的故障排除--诊断OpenStack问题
紫光展锐手机芯片被曝有严重安全漏洞,全球约10%安卓手机或受影响
企业内部Wiki,你建立了么?
英特尔、三星、高通等将组团收购Arm?
The white paper on the panorama of the digital economy, bank wealth management, was released
STM32内存知识
10 common high-frequency business scenarios that trigger IO bottlenecks
Godson huweiwu: overcome the slave mentality and make your own CPU instruction system! Loongarch will keep pace with x86/arm
155_ Model_ Safety stock of power Bi & power pivot purchase, sales and inventory
What are the products with high expected income of personal endowment insurance?
德州仪器发函:下半年供需失衡将缓解!模拟芯片涨势终结?
[database data recovery] data recovery case of SQLSERVER database file missing for unknown reasons under Windows Server Environment
DBeaver中如何调整SQL编辑器的字体大小
华为宣布成立三大军团、两大系统部,累计已组建20个军团
Traversal by layer of binary tree
三星彻底关闭LCD产线,300名员工转移至芯片封装部门
Introduction to Multivariate Statistics
一文彻底理解并发编程中非常重要的票据锁——StampedLock
深入浅出对话系统——概述
用指纹做Windows双因素身份认证,既安全又方便