注: log-slow-queries 設置把日志寫在那里,為空的時候,系統(tǒng)會給慢查詢日志賦予主機名,并被附加slow.log. /var/lib/mysql/slowquery.log為日志存放的文件的位置,一般這個目錄要有mysql的運行帳號的可寫權限,一般都將這個目錄設置為mysql的數據存放目錄 long_query_time=2中的2表示查詢超過兩秒才記錄. 如果設置了參數log-long-format,那么所有沒有使用索引的查詢也將被記錄。在文件my.cnf或my.ini中加入下面這一行可以記錄這些查詢 這是一個有用的日志。它對于性能的影響不大(假設所有查詢都很快),并且強調了那些最需要注意的查詢(丟失了索引或索引沒有得到最佳應用) # Time: 070927 8:08:52 # User@Host: root[root] @ [192.168.0.20] # Query_time: 372 Lock_time: 136 Rows_sent: 152 Rows_examined: 263630 select id, name from manager where id in (66,10135); 這是慢查詢日志中的一條,用了372秒,鎖了136秒,返回152行,一共查了263630行 如果日志內容很多,用眼睛一條一條去看會累死,mysql自帶了分析的工具,使用方法如下: 命令行下,進入mysql/bin目錄,輸入mysqldumpslow –help或--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 (t, at, l, al, r, ar etc), 'at' is default -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 scrīpt) -l don't subtract lock time from total time -s,是order的順序,說明寫的不夠詳細,俺用下來,包括看了代碼,主要有 c,t,l,r和ac,at,al,ar,分別是按照query次數,時間,lock的時間和返回的記錄數來排序,前面加了a的時倒敘 -t,是top n的意思,即為返回前面多少條的數據 -g,后邊可以寫一個正則匹配模式,大小寫不敏感的 mysqldumpslow -s c -t 20 host-slow.log mysqldumpslow -s r -t 20 host-slow.log