慢查詢日志相關參數
MySQL 慢查詢的相關參數解釋:slow_query_log :是否開啟慢查詢日志,1表示開啟,0表示關閉。
-
slow_query_log :是否開啟慢查詢日志,1表示開啟,0表示關閉。
-
log-slow-queries :舊版(5.6以下版本)MySQL數據庫慢查詢日志存儲路徑。可以不設置該參數,系統則會默認給一個缺省的文件host_name-slow.log
-
slow-query-log-file:新版(5.6及以上版本)MySQL數據庫慢查詢日志存儲路徑。可以不設置該參數,系統則會默認給一個缺省的文件host_name-slow.log
-
long_query_time :慢查詢閾值,當查詢時間多于設定的閾值時,記錄日志。
-
log_queries_not_using_indexes:未使用索引的查詢也被記錄到慢查詢日志中(可選項)。
-
log_output:日志存儲方式。log_output='FILE'表示將日志存入文件,默認值是'FILE'。log_output='TABLE'表示將日志存入數據庫,這樣日志信息就會被寫入到mysql.slow_log表中。MySQL數據<br>庫支持同時兩種日志存儲方式,配置的時候以逗號隔開即可,如:log_output='FILE,TABLE'。日志記錄到系統的專用日志表中,要比記錄到文件耗費更多的系統資源,因此對于需要啟用慢查詢日志,又需<br>要能夠獲得更高的系統性能,那么建議優先記錄到文件。
一. 設置方法
使用慢查詢日志里捕獲
啟用之前需要先進行一些設置
方法一:全局變量設置
設置慢查詢日志的日志文件位置
1 | set global slow_query_log_file = "D:/slow_log/slow_log.log" ; |
設置是否對未使用索引的SQL進行記錄
1 | set global log_queries_not_using_indexes = on ; |
設置只要SQL執行時間超過n秒的就記錄
1 | set global long_query_time = 0.001 ; |
此處設置的0.001秒,便于測試,一般情況比這個大
啟用mysql慢查詢日志
1 | set global slow_query_log = on ; |
方法二:配置文件設置
修改配置文件my.cnf,在[mysqld]下的下方加入
3 | log_queries_not_using_indexes = ON ; |
4 | slow_query_log_file = /usr/ local /mysql/data/slow.log |
查看設置后的參數
1 | show variables like 'slow_query%' ; |
2 | show variables like 'long_query__time' ; |
二. 慢查詢日志記錄的內容
01 | Time Id Command Argument |
02 | # Time : 2019-01-08T04:12:09.269315Z |
03 | # User @Host: h5_test[h5_test] @ localhost [::1] Id: 12 |
04 | # Query_time: 0.000831 Lock_time: 0.000198 Rows_sent: 1 Rows_examined: 3 |
06 | SET timestamp =1546920729; |
07 | SELECT t.customer_id,t.title,t.content |
09 | SELECT customer_id FROM product_comment WHERE product_id =199726 AND audit_status = 1 LIMIT 0,15 |
10 | )a JOIN product_comment t |
11 | ON a.customer_id = t.comment_id; |
-
Time:執行查詢的日期時間
-
User@Host:執行查詢的用戶和客戶端IP
-
Id:是執行查詢的線程Id
-
Query_time:SQL執行所消耗的時間
-
Lock_time:執行查詢對記錄鎖定的時間
-
Rows_sent:查詢返回的行數
-
Rows_examined:為了返回查詢的數據所讀取的行數
三. 如何分析慢查詢日志
01 | Usage: mysqldumpslow [ OPTS... ] [ LOGS... ] |
03 | Parse and summarize the MySQL slow query log. Options are |
11 | -s ORDER what to sort by (al, at , ar, c, l, r, t), 'at' is default |
14 | at : average query time |
19 | -r reverse the sort order (largest last instead of first ) |
20 | -t NUM just show the top n queries |
21 | -a don 't abstract all numbers to N and strings to ' S ' |
22 | -n NUM abstract numbers with at least n digits within names |
23 | -g PATTERN grep: only consider stmts that include this string |
24 | -h HOSTNAME hostname of db server for *-slow.log filename (can be wildcard), |
25 | default is ' * ', i.e. match all |
26 | -i NAME name of server instance (if using mysql.server startup script) |
27 | -l don' t subtract lock time from total time |
由于慢查詢日志中會含有大量的重復的SQL,為了方便,可以通過mysql提供的命令行工具 mysqldumpslow 來分析日志
01 | $ mysqldumpslow.pl slow_log.log |
03 | Reading mysql slow query log from slow_log.log |
04 | Count : 1 Time =0.00s (0s) Lock=0.00s (0s) Rows =0.0 (0), 0users@0hosts |
05 | C:\Program Files\MySQL\MySQL Server N.N\bin\mysqld.exe, Version: N.N.N-log (MySQL Community Server (GPL)). started with : |
06 | TCP Port: N, Named Pipe: MySQL |
07 | # Time : N-N-08T04:N:N.269315Z |
08 | # User @Host: h5_test[h5_test] @ localhost [::N] Id: N |
09 | # Query_time: N.N Lock_time: N.N Rows_sent: N Rows_examined: N |
12 | SELECT t.customer_id,t.title,t.content |
14 | SELECT customer_id FROM product_comment WHERE product_id =N AND audit_status = N LIMIT N,N |
15 | )a JOIN product_comment t |
16 | ON a.customer_id = t.comment_id |
與慢查詢日志中記錄的數據是相似的,只是多出了一行Count,這一行記錄的是這條SQL在記錄慢查詢日志期間的執行次數,如果一個SQL多次被執行,用這個命令分析時,只會出現一個SQL日志,Count里的數值代表執行次數,其他數字為了合并表示用N代替
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。