慢查詢(xún)?nèi)罩鞠嚓P(guān)參數(shù)
MySQL 慢查詢(xún)的相關(guān)參數(shù)解釋?zhuān)簊low_query_log :是否開(kāi)啟慢查詢(xún)?nèi)罩荆?表示開(kāi)啟,0表示關(guān)閉。
一. 設(shè)置方法
使用慢查詢(xún)?nèi)罩纠锊东@
啟用之前需要先進(jìn)行一些設(shè)置
方法一:全局變量設(shè)置
設(shè)置慢查詢(xún)?nèi)罩镜娜罩疚募恢?br />
set global slow_query_log_file = "D:/slow_log/slow_log.log" ;
設(shè)置是否對(duì)未使用索引的SQL進(jìn)行記錄
set global log_queries_not_using_indexes = on;
設(shè)置只要SQL執(zhí)行時(shí)間超過(guò)n秒的就記錄
set global long_query_time = 0.001 ;
此處設(shè)置的0.001秒,便于測(cè)試,一般情況比這個(gè)大
啟用mysql慢查詢(xún)?nèi)罩?br />
set global slow_query_log = on;
方法二:配置文件設(shè)置
修改配置文件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è)置后的參數(shù)
show variables like 'slow_query%'; show variables like 'long_query__time';
二. 慢查詢(xún)?nèi)罩居涗浀膬?nèi)容
Time Id Command Argument # Time: 2019-01-08T04:12:09.269315Z # User@Host: h5_test[h5_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;
三. 如何分析慢查詢(xún)?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
由于慢查詢(xún)?nèi)罩局袝?huì)含有大量的重復(fù)的SQL,為了方便,可以通過(guò)mysql提供的命令行工具 mysqldumpslow 來(lái)分析日志
$ 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: h5_test[h5_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
與慢查詢(xún)?nèi)罩局杏涗浀臄?shù)據(jù)是相似的,只是多出了一行Count,這一行記錄的是這條SQL在記錄慢查詢(xún)?nèi)罩酒陂g的執(zhí)行次數(shù),如果一個(gè)SQL多次被執(zhí)行,用這個(gè)命令分析時(shí),只會(huì)出現(xiàn)一個(gè)SQL日志,Count里的數(shù)值代表執(zhí)行次數(shù),其他數(shù)字為了合并表示用N代替
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
標(biāo)簽:洛陽(yáng) 葫蘆島 安徽 嘉峪關(guān) 吐魯番 甘南 拉薩
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MySQL慢查詢(xún)?nèi)罩镜幕臼褂媒坛獭?,本文關(guān)鍵詞 MySQL,慢,查詢(xún),日志,的,基本,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。