MySQL-慢查詢日誌

來源:互聯網
上載者:User

標籤:

慢查詢日誌功能預設不開啟,其記錄了執行時間超過參數long_query_time的值(預設是10),且訪問的行數超過了參數min_examined_row_limit的值得SQL語句。

mysql> show variables like ‘long_query_time‘\G*************************** 1. row ***************************Variable_name: long_query_time        Value: 10.0000001 row in set (0.00 sec)mysql>

擷取表的鎖的時間不算在sql執行時間內。語句執行結束後,才會寫到慢查詢日誌。

 

1.開啟慢查詢日誌功能(不同版本開啟方法不一樣)

5.1.6之前版本:

  --log-slow-queries[=file_name]
5.1.12版本:

  --slow_query_log[={0|1}] 
5.1.29版本

  --slow_query_log[={0|1}]

  --slow_query_log_file=file_name

mysql> show variables like ‘slow_query%‘;+---------------------+---------------------------------+| Variable_name       | Value                           |+---------------------+---------------------------------+| slow_query_log      | ON                              || slow_query_log_file | /var/mysql/data/dbtest-slow.log |+---------------------+---------------------------------+2 rows in set (0.00 sec)mysql> 

 

2.慢查詢日誌格式

慢查詢日誌可以以file格式輸出,也可以以資料庫表格式輸出

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> 

設定為table格式後,輸出內容在mysql.slow_log表中。

此外,可以使用mysqldumpslow工具分析慢查詢日誌

3.預設的慢查詢日誌名字為

  host_name-slow.log

4.與慢查詢有關的參數
如果啟動並執行語句沒有使用索引,開啟log_queries_not_using_indexes後,資料庫會將sql語句記錄到慢查詢記錄檔中。

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> 

MySQL 5.6.5新增了一個參數log_throttle_queries_not_using_indexes,表示每分鐘允許記錄到slow log的且未使用索引的sql語句次數,預設是0,表示沒有限制。

MySQL-慢查詢日誌

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.