当前位置:网站首页>MySQL optimization tutorial: slow query log practice

MySQL optimization tutorial: slow query log practice

2022-06-09 19:13:00 1024 Q

One 、 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??

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 :

Two 、 Slow query log practice

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


原网站

版权声明
本文为[1024 Q]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206091903081413.html