亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長提供免費收錄網(wǎng)站服務,提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

慢查詢?nèi)罩靖拍?/font>

     MySQL的慢查詢?nèi)罩臼荕ySQL提供的一種日志記錄,它用來記錄在MySQL中響應時間超過閥值的語句,具體指運行時間超過long_query_time值的SQL,則會被記錄到慢查詢?nèi)罩局小ong_query_time的默認值為10,意思是運行10S以上的語句。默認情況下,Mysql數(shù)據(jù)庫并不啟動慢查詢?nèi)罩荆枰覀兪謩觼碓O(shè)置這個參數(shù),當然,如果不是調(diào)優(yōu)需要的話,一般不建議啟動該參數(shù),因為開啟慢查詢?nèi)罩緯蚨嗷蛏賻硪欢ǖ男阅苡绊憽B樵內(nèi)罩局С謱⑷罩居涗泴懭胛募仓С謱⑷罩居涗泴懭霐?shù)據(jù)庫表。

 

官方文檔,關(guān)于慢查詢的日志介紹如下(部分資料,具體參考官方相關(guān)鏈接):

The slow query log consists of SQL statements that took more than long_query_time seconds to execute and required at least min_examined_row_limit rows to be examined. The minimum and default values of long_query_time are 0 and 10, respectively. The value can be specified to a resolution of microseconds. For logging to a file, times are written including the microseconds part. For logging to tables, only integer times are written; the microseconds part is ignored.

By default, administrative statements are not logged, nor are queries that do not use indexes for lookups. This behavior can be changed usinglog_slow_admin_statements and log_queries_not_using_indexes, as described later.

 

 

慢查詢?nèi)罩鞠嚓P(guān)參數(shù)

 

 

MySQL 慢查詢的相關(guān)參數(shù)解釋:

slow_query_log    :是否開啟慢查詢?nèi)罩荆?表示開啟,0表示關(guān)閉。

log-slow-queries  :舊版(5.6以下版本)MySQL數(shù)據(jù)庫慢查詢?nèi)罩敬鎯β窂健?梢圆辉O(shè)置該參數(shù),系統(tǒng)則會默認給一個缺省的文件host_name-slow.log

slow-query-log-file:新版(5.6及以上版本)MySQL數(shù)據(jù)庫慢查詢?nèi)罩敬鎯β窂健?梢圆辉O(shè)置該參數(shù),系統(tǒng)則會默認給一個缺省的文件host_name-slow.log

long_query_time :慢查詢閾值,當查詢時間多于設(shè)定的閾值時,記錄日志。

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ù)庫支持同時兩種日志存儲方式,配置的時候以逗號隔開即可,如:log_output='FILE,TABLE'。日志記錄到系統(tǒng)的專用日志表中,要比記錄到文件耗費更多的系統(tǒng)資源,因此對于需要啟用慢查詢?nèi)罩荆中枰軌颢@得更高的系統(tǒng)性能,那么建議優(yōu)先記錄到文件。

 

 

 

慢查詢?nèi)罩九渲?/font>

默認情況下slow_query_log的值為OFF,表示慢查詢?nèi)罩臼墙玫模梢酝ㄟ^設(shè)置slow_query_log的值來開啟,如下所示:

mysql> show variables  like '%slow_query_log%';
+---------------------+-----------------------------------------------+
| Variable_name       | Value                                         |
+---------------------+-----------------------------------------------+
| slow_query_log      | OFF                                           |
| slow_query_log_file | /home/WDPM/MysqlData/mysql/DB-Server-slow.log |
+---------------------+-----------------------------------------------+
2 rows in set (0.00 sec)

 
mysql> set global slow_query_log=1;
Query OK, 0 rows affected (0.09 sec)

 
mysql> show variables like '%slow_query_log%';
+---------------------+-----------------------------------------------+
| Variable_name       | Value                                         |
+---------------------+-----------------------------------------------+
| slow_query_log      | ON                                            |
| slow_query_log_file | /home/WDPM/MysqlData/mysql/DB-Server-slow.log |
+---------------------+-----------------------------------------------+
2 rows in set (0.00 sec)

 
mysql> 

 

使用set global slow_query_log=1開啟了慢查詢?nèi)罩局粚Ξ斍皵?shù)據(jù)庫生效,如果MySQL重啟后則會失效。如果要永久生效,就必須修改配置文件my.cnf(其它系統(tǒng)變量也是如此)。例如如下所示:

mysql> show variables like 'slow_query%';
+---------------------+-----------------------------------------------+
| Variable_name       | Value                                         |
+---------------------+-----------------------------------------------+
| slow_query_log      | OFF                                           |
| slow_query_log_file | /home/WDPM/MysqlData/mysql/DB-Server-slow.log |
+---------------------+-----------------------------------------------+
2 rows in set (0.01 sec)

 
mysql> 

clip_image001

 

修改my.cnf文件,增加或修改參數(shù)slow_query_log 和slow_query_log_file后,然后重啟MySQL服務器,如下所示

slow_query_log =1

slow_query_log_file=/tmp/mysql_slow.log

clip_image002

mysql> show variables like 'slow_query%';
+---------------------+---------------------+
| Variable_name       | Value               |
+---------------------+---------------------+
| slow_query_log      | ON                  |
| slow_query_log_file | /tmp/mysql_slow.log |
+---------------------+---------------------+
2 rows in set (0.00 sec)

 
mysql> 

關(guān)于慢查詢的參數(shù)slow_query_log_file ,它指定慢查詢?nèi)罩疚募拇娣怕窂剑到y(tǒng)默認會給一個缺省的文件host_name-slow.log(如果沒有指定參數(shù)slow_query_log_file的話)

mysql> show variables like 'slow_query_log_file';
+---------------------+-----------------------------------------------+
| Variable_name       | Value                                         |
+---------------------+-----------------------------------------------+
| slow_query_log_file | /home/WDPM/MysqlData/mysql/DB-Server-slow.log |
+---------------------+-----------------------------------------------+
1 row in set (0.00 sec)

 

那么開啟了慢查詢?nèi)罩竞螅裁礃拥腟QL才會記錄到慢查詢?nèi)罩纠锩婺兀?這個是由參數(shù)long_query_time控制,默認情況下long_query_time的值為10秒,可以使用命令修改,也可以在my.cnf參數(shù)里面修改。關(guān)于運行時間正好等于long_query_time的情況,并不會被記錄下來。也就是說,在mysql源碼里是判斷大于long_query_time,而非大于等于。從MySQL 5.1開始,long_query_time開始以微秒記錄SQL語句運行時間,之前僅用秒為單位記錄。如果記錄到表里面,只會記錄整數(shù)部分,不會記錄微秒部分。

mysql> show variables like 'long_query_time%';
+-----------------+-----------+
| Variable_name   | Value     |
+-----------------+-----------+
| long_query_time | 10.000000 |
+-----------------+-----------+
1 row in set (0.00 sec)

 
mysql> set global long_query_time=4;
Query OK, 0 rows affected (0.00 sec)

 
mysql> show variables like 'long_query_time';
+-----------------+-----------+
| Variable_name   | Value     |
+-----------------+-----------+
| long_query_time | 10.000000 |
+-----------------+-----------+
1 row in set (0.00 sec)

 

如上所示,我修改了變量long_query_time,但是查詢變量long_query_time的值還是10,難道沒有修改到呢?注意:使用命令 set global long_query_time=4修改后,需要重新連接或新開一個會話才能看到修改值。你用show variables like 'long_query_time'查看是當前會話的變量值,你也可以不用重新連接會話,而是用show global variables like 'long_query_time'; 如下所示:

clip_image003

在MySQL里面執(zhí)行下面SQL語句,然后我們?nèi)z查對應的慢查詢?nèi)罩荆蜁l(fā)現(xiàn)類似下面這樣的信息。

mysql> select sleep(3);
+----------+
| sleep(3) |
+----------+
|        0 |
+----------+
1 row in set (3.00 sec)

 
[root@DB-Server ~]# more /tmp/mysql_slow.log
/usr/sbin/mysqld, Version: 5.6.20-enterprise-commercial-advanced-log (MySQL Enterprise Server - Advanced Edition (Commercial)). started with:
Tcp port: 0  Unix socket: (null)
Time                 Id Command    Argument
/usr/sbin/mysqld, Version: 5.6.20-enterprise-commercial-advanced-log (MySQL Enterprise Server - Advanced Edition (Commercial)). started with:
Tcp port: 0  Unix socket: (null)
Time                 Id Command    Argument
# Time: 160616 17:24:35
# User@Host: root[root] @ localhost []  Id:     5
# Query_time: 3.002615  Lock_time: 0.000000 Rows_sent: 1  Rows_examined: 0
SET timestamp=1466069075;
select sleep(3);

image

 

log_output 參數(shù)是指定日志的存儲方式。log_output='FILE'表示將日志存入文件,默認值是'FILE'。log_output='TABLE'表示將日志存入數(shù)據(jù)庫,這樣日志信息就會被寫入到mysql.slow_log表中。MySQL數(shù)據(jù)庫支持同時兩種日志存儲方式,配置的時候以逗號隔開即可,如:log_output='FILE,TABLE'。日志記錄到系統(tǒng)的專用日志表中,要比記錄到文件耗費更多的系統(tǒng)資源,因此對于需要啟用慢查詢?nèi)罩荆中枰軌颢@得更高的系統(tǒng)性能,那么建議優(yōu)先記錄到文件。

mysql> show variables like '%log_output%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output    | FILE  |
+---------------+-------+
1 row in set (0.00 sec)

 
mysql> set global log_output='TABLE';
Query OK, 0 rows affected (0.00 sec)

 
mysql> show variables like '%log_output%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output    | TABLE |
+---------------+-------+
1 row in set (0.00 sec)

 
mysql> select sleep(5) ;
+----------+
| sleep(5) |
+----------+
|        0 |
+----------+
1 row in set (5.00 sec)

 
mysql> 

 
mysql> select * from mysql.slow_log;
+---------------------+---------------------------+------------+-----------+-----------+---------------+----+----------------+-----------+-----------+-----------------+-----------+
| start_time          | user_host                 | query_time | lock_time | rows_sent | rows_examined | db | last_insert_id | insert_id | server_id | sql_text        | thread_id |
+---------------------+---------------------------+------------+-----------+-----------+---------------+----+----------------+-----------+-----------+-----------------+-----------+
| 2016-06-16 17:37:53 | root[root] @ localhost [] | 00:00:03   | 00:00:00  |         1 |             0 |    |              0 |         0 |         1 | select sleep(3) |         5 |
| 2016-06-16 21:45:23 | root[root] @ localhost [] | 00:00:05   | 00:00:00  |         1 |             0 |    |              0 |         0 |         1 | select sleep(5) |         2 |
+---------------------+---------------------------+------------+-----------+-----------+---------------+----+----------------+-----------+-----------+-----------------+-----------+
2 rows in set (0.00 sec)

 
mysql> 

 

系統(tǒng)變量log-queries-not-using-indexes:未使用索引的查詢也被記錄到慢查詢?nèi)罩局校蛇x項)。如果調(diào)優(yōu)的話,建議開啟這個選項。另外,開啟了這個參數(shù),其實使用full index scan的sql也會被記錄到慢查詢?nèi)罩尽?/font>

This option does not necessarily mean that no index is used. For example, a query that uses a full index scan uses an index but would be logged because the index would not limit the number of rows.

mysql> show variables like 'log_queries_not_using_indexes';
+-------------------------------+-------+
| Variable_name                 | Value |
+-------------------------------+-------+
| log_queries_not_using_indexes | OFF   |
+-------------------------------+-------+
1 row in set (0.00 sec)

 
mysql> set global log_queries_not_using_indexes=1;
Query OK, 0 rows affected (0.00 sec)

 
mysql> show variables like 'log_queries_not_using_indexes';
+-------------------------------+-------+
| Variable_name                 | Value |
+-------------------------------+-------+
| log_queries_not_using_indexes | ON    |
+-------------------------------+-------+
1 row in set (0.00 sec)

 
mysql> 

系統(tǒng)變量log_slow_admin_statements表示是否將慢管理語句例如ANALYZE TABLE和ALTER TABLE等記入慢查詢?nèi)罩?/font>

mysql> show variables like 'log_slow_admin_statements';
+---------------------------+-------+
| Variable_name             | Value |
+---------------------------+-------+
| log_slow_admin_statements | OFF   |
+---------------------------+-------+
1 row in set (0.00 sec)

 
mysql> 

 

系統(tǒng)變量log_slow_slave_statements 表示

By default, a replication slave does not write replicated queries to the slow query log. To change this, use thelog_slow_slave_statements system variable.

When the slow query log is enabled, this variable enables logging for queries that have taken more than long_query_time seconds to execute on the slave. This variable was added in MySQL 5.7.1. Setting this variable has no immediate effect. The state of the variable Applies on all subsequent START SLAVE statements.

 

參數(shù)--log-short-format

The server writes less information to the slow query log if you use the --log-short-format option.

  • Command-Line Format --log-short-format
    Permitted Values Type boolean
    Default FALSE

     

另外,如果你想查詢有多少條慢查詢記錄,可以使用系統(tǒng)變量。

mysql> show global status like '%Slow_queries%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Slow_queries  | 2104  |
+---------------+-------+
1 row in set (0.00 sec)

 
mysql> 

 

 

 

日志分析工具mysqldumpslow

在生產(chǎn)環(huán)境中,如果要手工分析日志,查找、分析SQL,顯然是個體力活,MySQL提供了日志分析工具mysqldumpslow

查看mysqldumpslow的幫助信息:

[root@DB-Server ~]# mysqldumpslow --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 (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

-s, 是表示按照何種方式排序,

c: 訪問計數(shù)

l: 鎖定時間

r: 返回記錄

t: 查詢時間

al:平均鎖定時間

ar:平均返回記錄數(shù)

at:平均查詢時間

-t, 是top n的意思,即為返回前面多少條的數(shù)據(jù);

-g, 后邊可以寫一個正則匹配模式,大小寫不敏感的;

比如

得到返回記錄集最多的10個SQL。

mysqldumpslow -s r -t 10 /database/mysql/mysql06_slow.log

得到訪問次數(shù)最多的10個SQL

mysqldumpslow -s c -t 10 /database/mysql/mysql06_slow.log

得到按照時間排序的前10條里面含有左連接的查詢語句。

mysqldumpslow -s t -t 10 -g “left join” /database/mysql/mysql06_slow.log

另外建議在使用這些命令時結(jié)合 | 和more 使用 ,否則有可能出現(xiàn)刷屏的情況。

mysqldumpslow -s r -t 20 /mysqldata/mysql/mysql06-slow.log | more

分享到:
標簽:查詢 MySQL
用戶無頭像

網(wǎng)友整理

注冊時間:

網(wǎng)站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨大挑戰(zhàn)2018-06-03

數(shù)獨一種數(shù)學游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數(shù)有氧達人2018-06-03

記錄運動步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定