当前位置:网站首页>MySQL optimized slow log query
MySQL optimized slow log query
2022-06-12 09:41:00 【Rookie ~~】
Catalog
One 、 Slow query log (slow_query_log) Concept

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??
When we analyze the business involved in the project sql Efficiency time , You should open the slow query log , According to specific business 、 Specific concurrency , To estimate a time limit , The entire query cannot exceed the upper limit , Start the business after setting , During the pressure test, we will find out what sql Our query exceeded our forecast time , So these sql Will be recorded in the slow query log , And then use explain Analyze these time-consuming statements , Judge why it is inefficient . You can know whether the index is useful , Or there is no index to be added ( Primary key 、unique Or secondary index ), 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 , Let's see if there is grouping and sorting , If there is where There is also grouping and sorting , Consider adding a union index .
Slow query log related parameters :show variables like '%slow_query%';(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 , as follows :
Now change it to more than 1 Of a second SQL Will be recorded in the slow query log ! It can be set to 0.01 second , Express 10 millisecond .
Two 、 Slow query log practice
1. Turn on the slow query log switch


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
Slow query log path :/var/lib/mysql/


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

It turned out to be a whole table search , Sweep the whole primary key index tree . And then again paswd As a filter , The index should be set , Even if you set the index, you still can't use the index , And then I found out that because passwd String format involves type conversion , So there's no index , Correct sql It should be the following statement .
To sum up the problem at the beginning 

Next, we can give some examples , For example, when I do a business, I use where Filter condition plus order by, And then use explain After analysis using filedort, Involving external sorting , Why does it involve external sorting ? Because our data and indexes are stored on the disk , If you don't have a proper index , We have to do it. order by You can only sort externally , This is time-consuming , Then I tried to use where Filter conditions and order by The sorted fields establish a union index ...
Then it will be combined with the index of various problems mentioned in the previous blog , For example, type forced conversion 、 The filter conditions used MySQL No index is used for any of the functions , Take into account the optimization and so on . Reference answer :MySQL Index FAQs
3、 ... and 、show profiles see sql Specific running time
MySQL Generally, only two decimal places are displayed 
open profiling switch , More detailed time can be displayed 
No report error , explain profiling Variables only affect the current session
边栏推荐
- Summary of APP test interview questions, which must be taken in the interview
- 《真北》读书笔记
- In 2026, the capacity of China's software defined storage market will be close to US $4.51 billion
- 六月集训(第12天) —— 链表
- III Regular expression to finite state automata: NFA to DFA
- 002: what are the characteristics of the data lake
- Storage R & D Engineer Recruitment
- Automated test learning path, come and learn
- SAP HANA 错误消息 SYS_XSA authentication failed SQLSTATE - 28000
- Database common interview questions are ready for you
猜你喜欢

How CEPH improves storage performance and storage stability

软件定义存储概览(一篇就够)

Principle analysis of mongodb storage engine wiredtiger
Selection of interview questions for software testing

Auto.js学习笔记4:autojs打包后,大部分华为等大牌子手机无法安装?利用模拟器远程在autoPro里签名打包可以解决该问题。

Hotspot Metaspace

SAP HANA 错误消息 SYS_XSA authentication failed SQLSTATE - 28000

Ceph如何改善存储性能以及提升存储稳定性

Is it necessary to separate databases and tables for MySQL single table data of 5million?

After going to the bathroom, I figured out the ES search server
随机推荐
C#入门系列(十二) -- 字符串
Autojs微信研究:微信不同的版本或模拟器上的微信里的控件ip是不同的。
哈希表的线性探测法代码实现
Microservice gateway
【云原生】具体指什么呢---此文和大伙儿分享答案
端午節安康--諸佬在我心裏越來越粽要了
使用C语言代码实现工厂端LCD RGB的测试程序
CEPH performance optimization and enhancement
[cloud native] establishment of Eureka service registration
Cas d'essai et spécification de description des bogues référence
002:数据湖有哪些特征
What are the functions of resistance? (super full)
价值投资.
The Dragon Boat Festival is in good health -- people are becoming more and more important in my heart
ThreadLocal
硬盘 SMART 检测参数详解
MySQL优化之慢日志查询
Abstract classes and interfaces
Example interview -- dongyuhang: harvest love in the club
Basic SQL syntax i