MySQL optimization to enable slow log check / how to analyze problematic SQL through slow log

table of Contents

What is the slow log check switch

Check whether the slow log switch is enabled

View slow log variable information

Turn on the slow log variable switch

Custom slow log storage time

Turn on the slow log switch

View the slow log variable information again

test

How to analyze problematic SQL through slow logs


 

What is the slow log check switch

 

The slow log check will store the performance information of the successfully executed SQL statements outside the custom time into the slow log according to the user-defined time, so that the user administrator can optimize the slow log 

 

Check whether the slow log switch is enabled

 

show variables like 'slow_query_log';

You can see that it is OFF by default 


 

View slow log variable information

 

show variables like '%log%';

 Remember this switch is off by default

This is the default storage path for slow log files

 

Turn on the slow log variable switch

 

set global log_queries_not_using_indexes=on;

 

Custom slow log storage time

 

set global long_query_time=1;

 

Turn on the slow log switch

 

set global slow_query_log=on;

 

View the slow log variable information again

 

show variables like '%log%'

You can see that both the slow log variable and the slow log are turned on

 

test

 

Execute a query in mysql

Then copy a session, in another session, do not enter mysql, enter the command, query the slow log file

more /var/lib/mysql/master-slow.log

As you can see, there will be a detailed record of the performance parameters of the SQL you execute 

At this time, you will be wondering why I set up only SQL statements that exceed 1S to be saved in the log, and now all the executed SQL statements are stored in the log?

Note: The slow log will record all SQL statements by default. When you set the time for automatic deposit, you must restart the mysql service, otherwise it will not take effect! ! !

systemctl restart mysqld.service

After restarting the service, test again, execute a SQL, and query the log on another port. You will find that if it takes less than 1S, it cannot be recorded in the slow load log.

 

 

How to analyze problematic SQL through slow logs

 

1. Look at the SQL that has many queries and each query takes a long time

2. Look at the SQL with large IO, and see the Rows examine item. The more scan lines, the larger the IO

3. Look at the SQL of the missed index, by analyzing Rows examine and Rows Send, look at the index hit rate

 

 

 

Published 568 original articles · Like 180 · Visits 180,000+

Guess you like

Origin blog.csdn.net/Delicious_Life/article/details/105585701