mysqldumpslow日志分析的案例-創(chuàng)新互聯(lián)

小編給大家分享一下mysqldumpslow日志分析的案例,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

成都創(chuàng)新互聯(lián)長期為上千多家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為紅河哈尼企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作紅河哈尼網(wǎng)站改版等技術(shù)服務(wù)。擁有十多年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。

一、mysqldumpslow

官方文檔:http://dev.mysql.com/doc/refman/5.1/en/mysqldumpslow.html

shell> mysqldumpslow [options] [log_file ...]

Table 4.12. mysqldumpslow Option Reference

FormatConfig FileDescriptionIntroductionDeprecatedRemoved-aDo not abstract all numbers to N and strings to S-n numAbstract numbers with at least the specified digits--debugdebugWrite debugging information-g patternOnly consider statements that match the pattern--helpDisplay help message and exit-h nameHost name of the server in the log file name-i nameName of the server instance-lDo not subtract lock time from total time-rReverse the sort order-s valueHow to sort output-t numDisplay only first num queries--verboseverboseVerbose mode

mysql有一個(gè)功能就是可以log下來運(yùn)行的比較慢的sql語句,默認(rèn)是沒有這個(gè)log的,為了開啟這個(gè)功能,要修改my.cnf或者在mysql啟動的時(shí)候加入一些參數(shù)。如果在my.cnf里面修改,需增加如下幾行

long_query_time = 1
log-slow-queries = /var/youpath/slow.log
log-queries-not-using-indexes

long_query_time 是指執(zhí)行超過多久的sql會被log下來,這里是1秒。
log-slow-queries 設(shè)置把日志寫在那里,可以為空,系統(tǒng)會給一個(gè)缺省的文件host_name-slow.log,我生成的log就在mysql的data目錄
log-queries-not-using-indexes 就是字面意思,log下來沒有使用索引的query。

把上述參數(shù)打開,運(yùn)行一段時(shí)間,就可以關(guān)掉了,省得影響生產(chǎn)環(huán)境。

接下來就是分析了,我這里的文件名字叫host-slow.log。
mysqldumpslow –help以下,俺主要用的是
-s ORDER what to sort by (t, at, l, al, r, ar etc), ‘a(chǎn)t’ is default
-t NUM just show the top n queries
-g PATTERN grep: only consider stmts that include this string

-s,是order的順序,說明寫的不夠詳細(xì),俺用下來,包括看了代碼,主要有
c,t,l,r和ac,at,al,ar,分別是按照query次數(shù),時(shí)間,lock的時(shí)間和返回的記錄數(shù)來排序,前面加了a的時(shí)倒敘
-t,是top n的意思,即為返回前面多少條的數(shù)據(jù)
-g,后邊可以寫一個(gè)正則匹配模式,大小寫不敏感的

mysqldumpslow -s c -t 20 host-slow.log
mysqldumpslow -s r -t 20 host-slow.log

上述命令可以看出訪問次數(shù)最多的20個(gè)sql語句和返回記錄集最多的20個(gè)sql。
mysqldumpslow -t 10 -s t -g “l(fā)eft join” host-slow.log
這個(gè)是按照時(shí)間返回前10條里面含有左連接的sql語句。

用了這個(gè)工具就可以查詢出來那些sql語句是性能的瓶頸,進(jìn)行優(yōu)化,比如加索引,該應(yīng)用的實(shí)現(xiàn)方式等。

二、mysqldumpslow

MySQL 自帶 slow log 的分析工具 mysqldumpslow ,但是沒有說明。本文通過分析該腳本,介紹了其用法。
slow log 是 MySQL 根據(jù) SQL 語句的執(zhí)行時(shí)間設(shè)定,寫入的一個(gè)文件,用于分析執(zhí)行較慢的語句。

只要在 my.cnf 文件中配置好:
log-slow-queries = [slow_query_log_filename]
即可記錄超過默認(rèn)的 10s 執(zhí)行時(shí)間的 SQL 語句。
如果要修改默認(rèn)設(shè)置,可以添加:
long_query_time = 5
設(shè)定為 5s 。

如果要記錄所有 SQL 語句,可以寫入:
log-long-format



# t=time, l=lock time, r=rows
# at, al, 以及 ar 是對應(yīng)的平均值

mysqldumpslow 可以接受的參數(shù)有:
'v+', # verbose
'd+', # debug
's=s', # 排序 (t, at, l, al, r, ar etc)
'r!', # 倒排序 (largest last instead of first)
't=i', # 顯示最高的 n 個(gè)查詢
'a!', # 不把所有的數(shù)字以 N ,字符串以 'S' 顯示
'n=i', # abstract numbers with at least n digits within names
'g=s', # grep: only consider stmts that include this string
'h=s', # hostname of db server for *-slow.log filename (can be wildcard)
'i=s', # name of server instance (if using mysql.server startup script)
'l!', # don't subtract lock time from total time

'verbose|v+',# verbose
'help+', # write usage info
'debug|d+', # debug
's=s', # what to sort by (t, at, l, al, r, ar etc)
'r!', # reverse the sort order (largest last instead of first)
't=i', # just show the top n queries
'a!', # don't abstract all numbers to N and strings to 'S'
'n=i', # abstract numbers with at least n digits within names
'g=s', # grep: only consider stmts that include this string
'h=s', # hostname of db server for *-slow.log filename (can be wildcard)
'i=s', # name of server instance (if using mysql.server startup script)
'l!', # don't subtract lock time from total time

以時(shí)間倒序顯示前10個(gè)慢查詢?nèi)罩荆簃ysqldumpslow -s at -t 10 /var/db/mysql/db-slow.log

看完了這篇文章,相信你對“mysqldumpslow日志分析的案例”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道,感謝各位的閱讀!

分享文章:mysqldumpslow日志分析的案例-創(chuàng)新互聯(lián)
本文URL:http://bm7419.com/article30/cecgso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄標(biāo)簽優(yōu)化、網(wǎng)站建設(shè)、建站公司、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)