首頁  >  文章  >  資料庫  >  Mysql調優之profile的使用方法

Mysql調優之profile的使用方法

不言
不言轉載
2018-11-23 17:28:223566瀏覽

這篇文章帶給大家的內容是關於Mysql調優之profile的使用方法,有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。

  1. 在我們做mysql效能分析的時候,最常用的有三種方式:

    (1)慢查詢(分析出現問題的sql)
    (2)Explain (顯示了mysql如何使用索引來處理select語句以及連接表。可以幫助選擇更好的索引和寫出更優化的查詢語句)
    (3)Profile(查詢到SQL 會執行多少時間, 並看出CPU/Memory 使用量, 執行過程中Systemlock, Table lock 花多少時間等等。)

  2. 本章主要是對profile做簡單的概述,用來對某一條sql語句進行效能分析。

  3. Profiling是從 mysql5.0.3版本以後才開的。但在mysql5.7之後,profile資訊將逐漸被放棄,mysql推薦使用performance schema。

  4. profile此工具可用於查詢SQL執行狀態,System lock和Table lock 花多少時間等等,對定位一條語句的I/O消耗和CPU消耗非常重要。 (SQL 語句執行所消耗的最大兩部分資源就是IO和CPU)

profile工具使用

  1. 檢視自己的mysql版本:

    mysql> select version(); 
    +------------+
    | version()  |
    +------------+
    | 5.6.35-log |
    +------------+
  2. 檢視是否開啟profile功能(profiling=on代表開啟):

    mysql> show variables like '%profil%';
    +------------------------+-------+
    | Variable_name          | Value |
    +------------------------+-------+
    | have_profiling        | YES  |
    | profiling              | OFF    |
    | profiling_history_size | 15    |
    +------------------------+-------+
  3. 開啟profile:

    mysql> set profile=1;
  4. 開啟profile之後,執行要分析的sql語句:

    mysql> select t1.*,t2.action from pre_forum_thread as t1
    left join 
    (select a.* from pre_forum_threadmod as a,(select tid,max(dateline) as dateline from pre_forum_threadmod group by tid) as b
    where a.tid=b.tid and a.dateline=b.dateline) as t2
    on t1.tid=t2.tid
    where t1.displayorder>=0 and t1.fid in (47,49) and t1.tid > 100318 
    and (t1.authorid =7683017 or t2.action<>'DWN' or t2.action is null )
    order by t1.dateline desc limit 20;
  5. #查看產生的profile資訊:

    mysql> show profiles;
    +----------+------------+--------------------------------------------------------------------------------------------------------+
    | Query_ID | Duration  | Query                                                                                                  |
    +----------+------------+--------------------------------------------------------------------------------------------------------+
    |        1 | 1.37183777 | select t1.*,t2.action from pre_forum_thread as t1  |
    |        2 | 0.00078796 | show columns from `bbs`.`t2` |
    |        3 | 0.00150425 | show columns from `bbs`.`pre_forum_thread` |
    +----------+------------+--------------------------------------------------------------------------------------------------------+
  6. ##取得指定的query語句的開銷:

    mysql> show profile for query 2; 
    +----------------------+----------+
    | Status              | Duration |
    +----------------------+----------+
    | starting            | 0.000147 |
    | checking permissions | 0.000023 |
    | Opening tables      | 0.000047 |
    | init                | 0.000081 |
    | System lock          | 0.000031 |
    | optimizing          | 0.000034 |
    | statistics          | 0.001650 |
    | preparing            | 0.000046 |
    | executing            | 0.000018 |
    | Sending data        | 2.460588 |
    | end                  | 0.000041 |
    | query end            | 0.000019 |
    | closing tables      | 0.000022 |
    | freeing items        | 0.000055 |
    | cleaning up          | 0.000085 |
    +----------------------+----------+
  7. #關閉profile:

    mysql> set profiling=0;
  8. 相關特定的參數:

    type: 
       ALL                --显示所有的开销信息 
     | BLOCK IO          --显示块IO相关开销 
     | CONTEXT SWITCHES  --上下文切换相关开销 
     | CPU                --显示CPU相关开销信息 
     | IPC                --显示发送和接收相关开销信息 
     | MEMORY            --显示内存相关开销信息 
     | PAGE FAULTS        --显示页面错误相关开销信息 
     | SOURCE            --显示和Source_function,Source_file,Source_line相关的开销信息 
     | SWAPS              --显示交换次数相关开销的信息 
    
    例如,想要查看cpu和io开销可以执行命令:
    mysql> SHOW profile CPU,BLOCK IO  FOR query 2;
#總結

  1. 一般簡易的流程:

    (1)set profiling=1; //打开profile分析
    (2)run your sql1;
    (3)run your sql2;
    (4)show profiles;    //查看sql1,sql2的语句分析
    (5)SHOW profile CPU,BLOCK IO io FOR query 1; //查看CPU、IO消耗
    (6)set profiling=0; //关闭profile分析

以上是Mysql調優之profile的使用方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:segmentfault.com。如有侵權,請聯絡admin@php.cn刪除