MYSQL慢查詢日誌)

來源:互聯網
上載者:User
mysql有一個功能就是可以log下來啟動並執行比較慢的sql語句,預設是沒有這個log的,為了開啟這個功能,要修改my.cnf或者在mysql啟動的時候加入一些參數。
如果在my.cnf裡面修改,需增加如下幾行
long_query_time = 10
log-slow-queries = /usr/local/mysql/log/slow.log

long_query_time 是指執行超過多久的sql會被log下來,這裡是10秒。
log-slow-queries 設定把日誌寫在那裡,為空白的時候,系統會給慢查詢日誌賦予主機名稱,並被附加slow.log

    如果設定了參數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

上述命令可以看出訪問次數最多的20個sql語句和返回記錄集最多的20個sql。
mysqldumpslow -t 10 -s t -g “left join” host-slow.log
這個是按照時間返回前10條裡面含有左串連的sql語句

注意:my.cnf要寫到

 

[mysqld]這一節下面。

 

原地址:http://blog.chinaunix.net/space.php?uid=618253&do=blog&cuid=1093402

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.