Mysql慢查詢解釋
MySQL的慢查詢?nèi)罩臼荕ySQL提供的一種日志記錄,它用來(lái)記錄在MySQL中響應(yīng)時(shí)間超過(guò)閥值的語(yǔ)句,具體指運(yùn)行時(shí)間超過(guò)long_query_time值的SQL,則會(huì)被記錄到慢查詢?nèi)罩局?。long_query_time的默認(rèn)值為10,意思是運(yùn)行10S以上的語(yǔ)句。默認(rèn)情況下,Mysql數(shù)據(jù)庫(kù)并不啟動(dòng)慢查詢?nèi)罩?,需要我們手?dòng)來(lái)設(shè)置這個(gè)參數(shù),當(dāng)然,如果不是調(diào)優(yōu)需要的話,一般不建議啟動(dòng)該參數(shù),因?yàn)殚_(kāi)啟慢查詢?nèi)罩緯?huì)或多或少帶來(lái)一定的性能影響。慢查詢?nèi)罩局С謱⑷罩居涗泴?xiě)入文件,也支持將日志記錄寫(xiě)入數(shù)據(jù)庫(kù)表;慢查詢?nèi)罩居糜谟涗浺恍┻^(guò)慢的查詢語(yǔ)句,可以幫助管理員分析問(wèn)題所在,該日志默認(rèn)是沒(méi)有開(kāi)啟的,需要在配置文件里添加一系列參數(shù)來(lái)手動(dòng)啟動(dòng)
開(kāi)啟Mysql慢查詢的理由
數(shù)據(jù)庫(kù)是很容易產(chǎn)生瓶頸的地方,現(xiàn)在Nosql大家討論這么熱,估計(jì)都被數(shù)據(jù)庫(kù)搞郁悶了。MySQL中最影響速度的就是那些查詢非常慢的語(yǔ)句,這些慢的語(yǔ)句,可能是寫(xiě)的不夠合理或者是大數(shù)據(jù)下多表的聯(lián)合查詢等等,所以我們要找出這些語(yǔ)句,分析原因,加以優(yōu)化。這也是發(fā)這篇博文的原因
開(kāi)啟mysql慢查詢方法
1)方法一、登陸mysql數(shù)據(jù)庫(kù)終端里開(kāi)啟
mysql> show variables like "%long%"; SPAN style="COLOR: #ff00ff"> //查看一下默認(rèn)為慢查詢的時(shí)間10秒 /SPAN>
+-----------------+-----------+
| Variable_name | Value |
+-----------------+-----------+
| long_query_time | 10.000000 |
+-----------------+-----------+
1 row in set (0.00 sec)
mysql> set global long_query_time=1; SPAN style="COLOR: #ff00ff">//設(shè)置成1秒,加上global,下次進(jìn)mysql已然生效/SPAN>
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like "%slow%"; SPAN style="COLOR: #ff00ff"> //查看一下慢查詢是不是已經(jīng)開(kāi)啟 /SPAN>
+---------------------+---------------------------------+
| Variable_name | Value |
+---------------------+---------------------------------+
| log_slow_queries | OFF |
| slow_launch_time | 2 |
| slow_query_log | OFF |
| slow_query_log_file | /usr/local/mysql/mysql-slow.log |
+---------------------+---------------------------------+
4 rows in set (0.00 sec)
mysql> set slow_query_log='ON'; SPAN style="COLOR: #ff00ff"> //加上global,不然會(huì)報(bào)錯(cuò)的/SPAN>
ERROR 1229 (HY000): Variable 'slow_query_log' is a GLOBAL variable and should be set with SET GLOBAL
mysql> set global slow_query_log='ON'; SPAN style="COLOR: #ff00ff">//這個(gè)參數(shù)設(shè)置為ON,表示啟用慢查詢,可以捕獲執(zhí)行時(shí)間超過(guò)一定數(shù)值的SQL語(yǔ)句。/SPAN>
Query OK, 0 rows affected (0.28 sec)
mysql> show variables like "%slow%"; SPAN style="COLOR: #ff00ff">//查看是否已經(jīng)開(kāi)啟 /SPAN>
+---------------------+---------------------------------+
| Variable_name | Value |
+---------------------+---------------------------------+
| log_slow_queries | ON |
| slow_launch_time | 2 |
| slow_query_log | ON |
| slow_query_log_file | /usr/local/mysql/mysql-slow.log |
+---------------------+---------------------------------+
4 rows in set (0.00 sec)
方法2、修改mysql的配置文件my.cnf
[root@www ~]# vim /etc/mysql.cnf
在[mysqld]區(qū)域配置里添加下面內(nèi)容
......
slow_query_log = 1 //開(kāi)啟慢查詢?nèi)罩?將1改為ON也可以
long_query_time = 1 //mysql慢查詢時(shí)間,指定達(dá)到多少秒才算慢查詢;這里推薦設(shè)置超過(guò)1秒的執(zhí)行語(yǔ)句就會(huì)被記錄到慢查詢?nèi)罩局?/span>
slow_query_log_file= /var/lib/mysql/mysql-slow.log //慢查詢?nèi)罩韭窂?。這里slow_query_log_file修改為log-slow-queries 也可以
//主要是上面三行內(nèi)容,還可以添加下面幾行細(xì)節(jié)性配置
long-queries-not-using-indexes //記錄沒(méi)有使用索引的查詢語(yǔ)句
min_examined_row_limit = 1000 //記錄查找了多達(dá)1000次而引起的慢查詢
log-slow-admin-statements //記錄ALTER TABLE等語(yǔ)句引發(fā)的慢查詢
log-slow-slave-statements //記錄從服務(wù)器產(chǎn)生的慢查詢
分析工具
分析工具其實(shí)就是把mysql-slow.log里面記錄下來(lái)的數(shù)據(jù),分析一下顯示出來(lái)(其實(shí)自己寫(xiě)一個(gè)shell腳本也是可以把要的信息取出來(lái)的)。
[root@www ~]# cat mysql-slow.log //查看命令
/usr/local/mysql/libexec/mysqld, Version: 5.1.26-rc-log (Source distribution). started with:
Tcp port: 3306 Unix socket: /tmp/mysql.sock
Time Id Command Argument
# Time: 100814 13:28:30
# User@Host: root[root] @ localhost []
# Query_time: 10.096500 Lock_time: 0.045791 Rows_sent: 1 Rows_examined: 2374192
SET timestamp=1281763710;
select count(distinct ad_code) as x from ad_visit_history where ad_code in (select ad_code from ad_list where media_id=15);
# Time: 100814 13:37:02
# User@Host: root[root] @ localhost []
# Query_time: 10.394134 Lock_time: 0.000091 Rows_sent: 1 Rows_examined: 2374192
SET timestamp=1281764222;
select count(distinct ad_code) as x from ad_visit_history where ad_code in (select ad_code from ad_list where media_id=15);
# Time: 100814 13:37:16
# User@Host: root[root] @ localhost []
# Query_time: 4.608920 Lock_time: 0.000078 Rows_sent: 1 Rows_examined: 1260544
SET timestamp=1281764236;
select count(*) as cou from ad_visit_history where ad_code in (select ad_code from ad_list where id=41) order by id desc;
看到了,就是記錄一下sql語(yǔ)句的執(zhí)行情況,包括執(zhí)行時(shí)間,鎖定時(shí)間等,所以要不要分析工具看個(gè)人情況,分析工具很多,在這兒只說(shuō)一下mysql自帶的慢查詢分析工具mysqldumpslow的使用方法。
[root@www ~]# mysqldumpslow -h
Option h requires an argument
ERROR: bad option
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 //查錯(cuò)
-s ORDER what to sort by (t, at, l, al, r, ar etc), 'at' is default //排序方式query次數(shù),時(shí)間,lock的時(shí)間和返回的記錄數(shù)來(lái)排序
-r reverse the sort order (largest last instead of first) //倒排序
-t NUM just show the top n queries //顯示前N多個(gè)
-a don't abstract all numbers to N and strings to 'S'
-n NUM abstract numbers with at least n digits within names //抽象的數(shù)字,至 少有n位內(nèi)的名稱
-g PATTERN grep: only consider stmts that include this string //配置模式
-h HOSTNAME hostname of db server for *-slow.log filename (can be wildcard), //mysql所以機(jī)器名或者IP
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 //總時(shí)間中不減去鎖定時(shí)間
實(shí)例:
[root@BlackGhost bin]# ./mysqldumpslow -s r -t 20 /var/lib/mysql/mysql-slow.log
[root@BlackGhost bin]# ./mysqldumpslow -s r -t 20 -g 'count' /var/lib/mysql/mysql-slow.log
以上這篇Mysql慢查詢操作梳理總結(jié)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:- 詳解mysql數(shù)據(jù)庫(kù)如何開(kāi)啟慢查詢?nèi)罩?/li>
- MySQL慢查詢之開(kāi)啟慢查詢
- MySQL啟用慢查詢?nèi)罩居涗浄椒?/li>
- mysqldumpslow用法示例(慢查詢)
- MySQL如何清空慢查詢文件
- 一次MySQL慢查詢導(dǎo)致的故障
- mysql5.7.10開(kāi)啟慢查詢?cè)斀?/li>