MySQL:動態開啟慢查詢日誌(Slow Query Log)

來源:互聯網
上載者:User

標籤:bar   view   查看   ted   開啟   osc   soc   bash   meta   

前言

在開發中,高效能的程式 也包括 高效能的查詢,所以最佳化SQL也是程式員必要技能之一。要最佳化就必須要有慢日誌記錄才可以知道哪些查詢慢,然後反向去修改

慢日誌設定方式
  • 寫入檔案

  • 寫入資料庫

實踐操作方式一:寫入檔案

編輯my.conf 中修改 log_slow_queries 的日誌地址

$ cd /etc/mysql$ cat my.cnf |grep slow log_slow_queries= /data/logs/mysql/mysql-slow.log$ sudo /etc/init.d/mysql restart


簡單驗證

$ mysql -uroot -pmysql> show variables like ‘%slow_query_log%‘;+---------------------+---------------------------------+| Variable_name       | Value                           |+---------------------+---------------------------------+| slow_query_log      | ON                              || slow_query_log_file | /data/logs/mysql/mysql-slow.log |+---------------------+---------------------------------+2 rows in set (0.00 sec)$ tail -f /data/logs/mysql/mysql-slow.log# Time: 161110 23:20:22# [email protected]: root[root] @ localhost []# Query_time: 3.007048  Lock_time: 0.000000 Rows_sent: 1  Rows_examined: 0SET timestamp=1478791222;select sleep(3);
方式二:寫入資料庫

先查看目前日誌輸出方式

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


設定輸出方式為FILE,TABLE

mysql> set global log_output=‘FILE,TABLE‘;Query OK, 0 rows affected (0.00 sec)mysql> show variables like ‘%log_output%‘;+---------------+------------+| Variable_name | Value      |+---------------+------------+| log_output    | FILE,TABLE |+---------------+------------+1 row in set (0.00 sec)mysql> select count(*) from mysql.slow_log;+----------+| count(*) |+----------+|        2 |+----------+1 row in set (0.00 sec)



備忘: log_output 參數設定記錄檔的輸出,可選值為 TABLE, FILE ,NONE;  "TABLE" 意思為設定日誌分別記錄到 mysql 庫的 general_log 和 slow_log 表中; "FILE" 意思為記錄日誌到作業系統的檔案中, "NONE" 意思為取消日誌記錄。

參考資料
  1. http://dev.mysql.com/doc/refman/5.6/en/slow-query-log.html    


原文地址:MySQL:動態開啟慢查詢日誌(Slow Query Log)
標籤:mysql   slow_log   log   log_output   query   

智能推薦
  • apparmor 引起自訂mysql 日誌問題
  • In MySQL, a zero number equals any string
  • MetaWeblog 同時管理51cto,csdn,sina,163,oschina,cnblogs等部落格
  • PAC 自動代理
  • 免費FQ利器:shadowsocks

MySQL:動態開啟慢查詢日誌(Slow Query Log)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.