這期內(nèi)容當中小編將會給大家?guī)碛嘘P(guān)如何在MySQL中使用慢查詢?nèi)罩荆恼聝?nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
慢查詢?nèi)罩鞠嚓P(guān)參數(shù)
MySQL 慢查詢的相關(guān)參數(shù)解釋:slow_query_log :是否開啟慢查詢?nèi)罩荆?表示開啟,0表示關(guān)閉。
slow_query_log :是否開啟慢查詢?nèi)罩荆?表示開啟,0表示關(guān)閉。
log-slow-queries :舊版(5.6以下版本)MySQL數(shù)據(jù)庫慢查詢?nèi)罩敬鎯β窂???梢圆辉O置該參數(shù),系統(tǒng)則會默認給一個缺省的文件host_name-slow.log
slow-query-log-file:新版(5.6及以上版本)MySQL數(shù)據(jù)庫慢查詢?nèi)罩敬鎯β窂???梢圆辉O置該參數(shù),系統(tǒng)則會默認給一個缺省的文件host_name-slow.log
long_query_time :慢查詢閾值,當查詢時間多于設定的閾值時,記錄日志。
log_queries_not_using_indexes:未使用索引的查詢也被記錄到慢查詢?nèi)罩局校蛇x項)。
log_output:日志存儲方式。log_output='FILE'表示將日志存入文件,默認值是'FILE'。log_output='TABLE'表示將日志存入數(shù)據(jù)庫,這樣日志信息就會被寫入到mysql.slow_log表中。MySQL數(shù)據(jù)<br>庫支持同時兩種日志存儲方式,配置的時候以逗號隔開即可,如:log_output='FILE,TABLE'。日志記錄到系統(tǒng)的專用日志表中,要比記錄到文件耗費更多的系統(tǒng)資源,因此對于需要啟用慢查詢?nèi)罩?,又?lt;br>要能夠獲得更高的系統(tǒng)性能,那么建議優(yōu)先記錄到文件。
一. 設置方法
使用慢查詢?nèi)罩纠锊东@
啟用之前需要先進行一些設置
方法一:全局變量設置
設置慢查詢?nèi)罩镜娜罩疚募恢?/p>
set global slow_query_log_file = "D:/slow_log/slow_log.log" ;
設置是否對未使用索引的SQL進行記錄
set global log_queries_not_using_indexes = on;
設置只要SQL執(zhí)行時間超過n秒的就記錄
set global long_query_time = 0.001 ;
此處設置的0.001秒,便于測試,一般情況比這個大
啟用mysql慢查詢?nèi)罩?/p>
set global slow_query_log = on;
方法二:配置文件設置
修改配置文件my.cnf,在[mysqld]下的下方加入
[mysqld] slow_query_log = ON log_queries_not_using_indexes = ON; slow_query_log_file = /usr/local/mysql/data/slow.log long_query_time = 1
查看設置后的參數(shù)
show variables like 'slow_query%'; show variables like 'long_query__time';
二. 慢查詢?nèi)罩居涗浀膬?nèi)容
Time Id Command Argument # Time: 2019-01-08T04:12:09.269315Z # User@Host: h6_test[h6_test] @ localhost [::1] Id: 12 # Query_time: 0.000831 Lock_time: 0.000198 Rows_sent: 1 Rows_examined: 3 use mc_productdb; SET timestamp=1546920729; SELECT t.customer_id,t.title,t.content FROM ( SELECT customer_id FROM product_comment WHERE product_id =199726 AND audit_status = 1 LIMIT 0,15 )a JOIN product_comment t ON a.customer_id = t.comment_id;
Time:執(zhí)行查詢的日期時間
User@Host:執(zhí)行查詢的用戶和客戶端IP
Id:是執(zhí)行查詢的線程Id
Query_time:SQL執(zhí)行所消耗的時間
Lock_time:執(zhí)行查詢對記錄鎖定的時間
Rows_sent:查詢返回的行數(shù)
Rows_examined:為了返回查詢的數(shù)據(jù)所讀取的行數(shù)
三. 如何分析慢查詢?nèi)罩?/strong>
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
由于慢查詢?nèi)罩局袝写罅康闹貜偷腟QL,為了方便,可以通過mysql提供的命令行工具 mysqldumpslow 來分析日志
$ mysqldumpslow.pl slow_log.log Reading mysql slow query log from slow_log.log Count: 1 Time=0.00s (0s) Lock=0.00s (0s) Rows=0.0 (0), 0users@0hosts C:\Program Files\MySQL\MySQL Server N.N\bin\mysqld.exe, Version: N.N.N-log (MySQL Community Server (GPL)). started with: TCP Port: N, Named Pipe: MySQL # Time: N-N-08T04:N:N.269315Z # User@Host: h6_test[h6_test] @ localhost [::N] Id: N # Query_time: N.N Lock_time: N.N Rows_sent: N Rows_examined: N use mc_productdb; SET timestamp=N; SELECT t.customer_id,t.title,t.content FROM ( SELECT customer_id FROM product_comment WHERE product_id =N AND audit_status = N LIMIT N,N )a JOIN product_comment t ON a.customer_id = t.comment_id
與慢查詢?nèi)罩局杏涗浀臄?shù)據(jù)是相似的,只是多出了一行Count,這一行記錄的是這條SQL在記錄慢查詢?nèi)罩酒陂g的執(zhí)行次數(shù),如果一個SQL多次被執(zhí)行,用這個命令分析時,只會出現(xiàn)一個SQL日志,Count里的數(shù)值代表執(zhí)行次數(shù),其他數(shù)字為了合并表示用N代替
上述就是小編為大家分享的如何在MySQL中使用慢查詢?nèi)罩玖?,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設計公司行業(yè)資訊頻道。
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
新聞名稱:如何在MySQL中使用慢查詢?nèi)罩?創(chuàng)新互聯(lián)
文章鏈接:http://aaarwkj.com/article28/cdhjjp.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、營銷型網(wǎng)站建設、微信公眾號、靜態(tài)網(wǎng)站、網(wǎng)站內(nèi)鏈、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容