mysql服務器有一項功能,能夠檢測到哪條sql語句查詢得比較慢,就是慢查詢slowlog,如今介紹如何開啓。
在[mysqld]下面增長以下代碼:java
long_query_time = 1 log-slow-queries = /usr/local/mysql/data/slow.log log-queries-not-using-indexes
long_query_time = 1 #定義超過1秒的查詢計數到變量Slow_queries。
log-slow-queries = /usr/local/mysql/data/slow.log #定義慢查詢日誌路徑。
log-queries-not-using-indexes #未使用索引的查詢也被記錄到慢查詢日誌中(可選)。
mysql自帶了一個查看慢日誌的工具mysqldumpslow。
執行mysqldumpslow –h能夠查看幫助信息。
主要介紹兩個參數-s和-t
-s 這個是排序參數,可選的有:
al: 平均鎖定時間
ar: 平均返回記錄數
at: 平均查詢時間
c: 計數
l: 鎖定時間
r: 返回記錄
t: 查詢時間mysql
-t n 顯示頭n條記錄。
實例:
mysqldumpslow -s c -t 20 host-slow.log
mysqldumpslow -s r -t 20 host-slow.log
上述命令能夠看出訪問次數最多的20個sql語句和返回記錄集最多的20個sql。
mysqldumpslow -t 10 -s t -g 「left join」 host-slow.log
這個是按照時間返回前10條裏面含有左鏈接的sql語句。
用了這個工具就能夠查詢出來那些sql語句是性能的瓶頸,進行優化,好比加索引,該應用的實現方式等。git
附幫助:sql
[root@yzdb1 sqlpro]# mysqldumpslow -h Option h requires an argument ERROR: bad option 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