Mysql slow query log
一、概念部分:
顧名思義,慢查詢日誌中記錄的是執行時間較長的query,也就是我們常說的slow query,通過設--log-slow-queries[=file_name]來打開該功能並設置記錄位置和文件名,默認文件名為hostname-slow.log,默認目錄也是數據目錄。
慢查詢日誌采用的是簡單的文本格式,可以通過各種文本編輯器查看其中的內容。其中記錄了語句執行的時刻,執行所消耗的時間,執行用戶,連接主機等相關信息。MySQL還提供了專門用來分析滿查詢日誌的工具程序mysqlslowdump,用來幫助數據庫管理人員解決可能存在的性能問題。
二、slow query log相關變量
2.1、命令行參數:
--log-slow-queries
指定日誌文件存放位置,可以為空,系統會給一個缺省的文件host_name-slow.log
2.2、系統變量
log_slow_queries
指定日誌文件存放位置,可以為空,系統會給一個缺省的文件host_name-slow.log
slow_query_log
slow quere log的開關,當值為1的時候說明開啟慢查詢。
slow_query_log_file
指定日誌文件存放位置,可以為空,系統會給一個缺省的文件host_name-slow.log
long_query_time
記錄超過的時間,默認為10s
log_queries_not_using_indexes
log下來沒有使用索引的query,可以根據情況決定是否開啟
三、實驗部分:
----使用log_slow_queries參數打開慢查詢,由於該參數已經過時,因此在err日誌中將出現提示信息
----修改my.cnf文件,添加log_slow_queries參數
[[email protected] ~]# vi /opt/mysql5.5/my.cnf
[[email protected] ~]# cat /opt/mysql5.5/my.cnf |grep ‘^log_slow‘
log_slow_queries = /tmp/mysqlslow.log
----清空err日誌內容:
[[email protected]
四、Mysqldumpslow
如果日誌內容很多,用眼睛一條一條看會累死,mysql自帶了分析的工具,使用方法如下:
[[email protected] data]# mysqldumpslow --help
Usage: mysqldumpslow [ OPTS... ] [ LOGS... ]
Parse and summarize the MySQL slow query log. Options are
--verbose verbose
--debug debug
--help write this text to standard output
-v verbose
-d debug
-s ORDER what to sort by (al, at, ar, c, l, r, t), ‘at‘ is default
al: average lock time
ar: average rows sent
at: average query time
c: count
l: lock time
r: rows sent
t: query time
-r reverse the sort order (largest last instead of first)
-t NUM just show the top n queries
-a don‘t abstract all numbers to N and strings to ‘S‘
-n NUM abstract numbers with at least n digits within names
-g PATTERN grep: only consider stmts that include this string
-h HOSTNAME hostname of db server for *-slow.log filename (can be wildcard),
default is ‘*‘, i.e. match all
-i NAME name of server instance (if using mysql.server startup script)
-l don‘t subtract lock time from total time
文章引自----www.cnblogs.com/Richardzhu/p/3230221.html
Mysql slow query log