處理 mysql slow sql

來源:互聯網
上載者:User

 
在slow_query_log 值為ON的情況下(預設為OFF 預設為10s,同時精確到微秒)預設(log_output值為FIFL時)就會把這種慢查詢記錄到:slow_query_log_file值所指定的檔案中。
mysql> select @@global.log_output;
+---------------------+
| @@global.log_output |
+---------------------+
| FILE                |
+---------------------+
1 row in set (0.00 sec)

注意在MySQL5.1就開始支援把慢查詢的日誌記錄放到mysq.slow_log中,但需要設定log_output變數值為TABLE:

mysql> set @@global.log_output='TABLE';
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    6
Current database: *** NONE ***

Query OK, 0 rows affected (0.00 sec)
開啟慢SQL之前:
mysql> show variables like '%slow%';
+---------------------+------------------------------------+
| Variable_name       | Value                              |
+---------------------+------------------------------------+
| log_slow_queries    | OFF                                |
| slow_launch_time    | 2                                  |
| slow_query_log      | OFF                                |
| slow_query_log_file | /var/mysql/data/localhost-slow.log |
+---------------------+------------------------------------+
4 rows in set (0.00 sec)
開啟慢SQL
mysql> set @@global.slow_query_log=ON;
Query OK, 0 rows affected (0.00 sec)

mysql> system cat /var/mysql/data/localhost-slow.log            
/usr/local/mysql/bin/mysqld, Version: 5.5.15-log (Source distribution). started with:
Tcp port: 3306  Unix socket: /tmp/mysql.sock
Time                 Id Command    Argument
/usr/local/mysql/bin/mysqld, Version: 5.5.15-log (Source distribution). started with:
Tcp port: 3306  Unix socket: /tmp/mysql.sock
Time                 Id Command    Argument
/usr/local/mysql/bin/mysqld, Version: 5.5.15-log (Source distribution). started with:
Tcp port: 3306  Unix socket: /tmp/mysql.sock
Time                 Id Command    Argument
/usr/local/mysql/bin/mysqld, Version: 5.5.15-log (Source distribution). started with:
Tcp port: 3306  Unix socket: /tmp/mysql.sock
Time                 Id Command    Argument
mysql>
進入  /var/mysql/data/這個目錄然後查看
 mysqldumpslow localhost-slow.log
 [root@localhost data]# mysqldumpslow localhost-slow.log

Reading mysql slow query log from localhost-slow.log
Count: 1  Time=0.00s (0s)  Lock=0.00s (0s)  Rows=0.0 (0), 0users@0hosts
 

因為為了效能考慮,一般general log不會開啟。slow log可以定位一些有效能問題的sql,而general log會記錄所有的SQL。
 mysql5.0版本,如果要開啟slow log、general log,需要重啟,從MySQL5.1.6版開始,general query log和slow query log開始支援寫到檔案或者資料庫表兩種方式,並且日誌的開啟,輸出方式的修改,都可以在Global層級動態修改。
 
mysql> select version();
+------------+
| version()  |
+------------+
| 5.5.15-log |
+------------+
1 row in set (0.00 sec)
 
設定日誌輸出方式為檔案(如果設定log_output=table的話,則日誌結果會記錄到名為gengera_log的表中,這表的預設引擎都是CSV):
 
mysql> set global log_output=file;
Query OK, 0 rows affected (0.00 sec)
 
設定general log的記錄檔路徑:
 
mysql> set global general_log_file='/tmp/general.log';
Query OK, 0 rows affected (0.00 sec)
 
 開啟general log:
 
mysql> set global general_log=on;
Query OK, 0 rows affected (0.00 sec)
 
過一段時間後,關閉general log:
 
mysql> set global general_log=off;
Query OK, 0 rows affected (0.01 sec)
 
查看tmp/general.log的資訊,可以大致看到哪些sql查詢/更新/刪除/插入比較頻繁了。比如有些表不是經常變化的,查詢量又很大,就完全可以cache;對主備延遲要求不高的表,讀可以放到備庫;等等

相關文章

聯繫我們

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