linux下開啟mysql慢查詢,分析查詢語句

來源:互聯網
上載者:User

標籤:

一,為什麼要開啟這個查詢呢?

資料庫是很容易產生瓶頸的地方,現在Nosql大家討論這麼熱,估計都被資料庫搞鬱悶了。mysql中最影響速度的就是那些查詢非常慢的語句,這些慢的語句,可能是寫的不夠合理或者是大資料下多表的聯集查詢等等,所以我們要找出這些語句,分析原因,加以最佳化。這也是發這篇博文的原因

二,開啟mysql的慢查詢

方法1,用命令開啟慢查詢

查看複製列印?
  1. mysql> show variables like "%long%";         //查看一下預設為慢查詢的時間10秒  
  2. +-----------------+-----------+  
  3. | Variable_name   | Value     |  
  4. +-----------------+-----------+  
  5. | long_query_time | 10.000000 |  
  6. +-----------------+-----------+  
  7. 1 row in set (0.00 sec)  
  8.   
  9. mysql> set global long_query_time=2;          //設定成2秒,加上global,下次進mysql已然生效  
  10. Query OK, 0 rows affected (0.00 sec)  
  11.   
  12. mysql> show variables like "%slow%";          //查看一下慢查詢是不是已經開啟  
  13. +---------------------+---------------------------------+  
  14. | Variable_name       | Value                           |  
  15. +---------------------+---------------------------------+  
  16. | log_slow_queries    | OFF                             |  
  17. | slow_launch_time    | 2                               |  
  18. | slow_query_log      | OFF                             |  
  19. | slow_query_log_file | /usr/local/mysql/mysql-slow.log |  
  20. +---------------------+---------------------------------+  
  21. 4 rows in set (0.00 sec)  
  22.   
  23. mysql> set slow_query_log=‘ON‘;                        //加上global,不然會報錯的。  
  24. ERROR 1229 (HY000): Variable ‘slow_query_log‘ is a GLOBAL variable and should be set with SET GLOBAL  
  25. mysql> set global slow_query_log=‘ON‘;            //啟用慢查詢  
  26. Query OK, 0 rows affected (0.28 sec)  
  27.   
  28. mysql> show variables like "%slow%";              //查看是否已經開啟  
  29. +---------------------+---------------------------------+  
  30. | Variable_name       | Value                           |  
  31. +---------------------+---------------------------------+  
  32. | log_slow_queries    | ON                              |  
  33. | slow_launch_time    | 2                               |  
  34. | slow_query_log      | ON                              |  
  35. | slow_query_log_file | /usr/local/mysql/mysql-slow.log |  
  36. +---------------------+---------------------------------+  
  37. 4 rows in set (0.00 sec)  

方法2,修改mysql的設定檔my.cnf

在[mysqld]裡面加上以下內容

  1. long_query_time = 2  
  2. log-slow-queries = /usr/local/mysql/mysql-slow.log  

重起一下
/usr/local/mysql/libexec/mysqld restart

三,分析工具

分析工具幹什麼事的呢,其實就是把mysql-slow.log裡面記錄下來的資料,分析一下顯示出來。其實自己寫一個shell指令碼也是可以把要的資訊取出來的。我們來看一下mysql-slow.log裡面到底是什麼東西

查看複製列印?
  1. [[email protected] mysql]# cat mysql-slow.log     //查看命令  
  2. /usr/local/mysql/libexec/mysqld, Version: 5.1.26-rc-log (Source distribution). started with:  
  3. Tcp port: 3306  Unix socket: /tmp/mysql.sock  
  4. Time                 Id Command    Argument  
  5. # Time: 100814 13:28:30  
  6. # [email protected]: root[root] @ localhost []  
  7. # Query_time: 10.096500  Lock_time: 0.045791 Rows_sent: 1  Rows_examined: 2374192  
  8. SET timestamp=1281763710;  
  9. 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);  
  10. # Time: 100814 13:37:02  
  11. # [email protected]: root[root] @ localhost []  
  12. # Query_time: 10.394134  Lock_time: 0.000091 Rows_sent: 1  Rows_examined: 2374192  
  13. SET timestamp=1281764222;  
  14. 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);  
  15. # Time: 100814 13:37:16  
  16. # [email protected]: root[root] @ localhost []  
  17. # Query_time: 4.608920  Lock_time: 0.000078 Rows_sent: 1  Rows_examined: 1260544  
  18. SET timestamp=1281764236;  
  19. 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語句的執行情況,包括執行時間,鎖定時間等,所以要不要分析工具看個人情況,分析工具很多,在這兒只說一下mysql內建的慢查詢分析工具mysqldumpslow的使用方法。

查看複製列印?
  1. [[email protected] bin]# mysqldumpslow -h  
  2. Option h requires an argument  
  3. ERROR: bad option  
  4.   
  5. Usage: mysqldumpslow [ OPTS... ] [ LOGS... ]  
  6.   
  7. Parse and summarize the MySQL slow query log. Options are  
  8.   
  9.  --verbose    verbose  
  10.  --debug      debug  
  11.  --help       write this text to standard output  
  12.   
  13.  -v           verbose  
  14.  -d           debug          //查錯  
  15.  -s ORDER     what to sort by (t, at, l, al, r, ar etc), ‘at‘ is default     //排序方式query次數,時間,lock的時間和返回的記錄數來排序  
  16.  -r           reverse the sort order (largest last instead of first)       //倒排序  
  17.  -t NUM       just show the top n queries                                       //顯示前N多個  
  18.  -a           don‘t abstract all numbers to N and strings to ‘S‘ 
  19.  -n NUM       abstract numbers with at least n digits within names   //抽象的數字,至 少有n位內的名稱 
  20.  -g PATTERN   grep: only consider stmts that include this string      //配置模式 
  21.  -h HOSTNAME  hostname of db server for *-slow.log filename (can be wildcard),     //mysql所以機器名或者IP 
  22.  default is ‘*‘, i.e. match all 
  23.  -i NAME      name of server instance (if using mysql.server startup script) 
  24.  -l           don‘t subtract lock time from total time           //總時間中不減去鎖定時間  

例子:

[[email protected] bin]# ./mysqldumpslow -s r -t 20 /usr/local/mysql/mysql-slow.log

[[email protected] bin]# ./mysqldumpslow -s r -t 20 -g ‘count‘ /usr/local/mysql/mysql-slow.log

linux下開啟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.