information_schema.profiling學習


information_schema.profiling可以用來分析每一條SQL在它執行的各個階段的用時,注意這個表是session 級的,也就是說如果session1 開啟了它;session2沒有開啟

這個情況下session2 去查詢只會返回一個空表

 

1、set @@ssesion.profiling=1 可以開啟information_schema.profiling相關監測

 

2、information_schema.profiling 表的常用列

  1、query_id              :查詢id 用於標記不同的查詢

  2、seq                 :一個查詢內部執行的步驟

  3、state                :各個步驟的狀態

  4、duration              :各個步驟持續的時間

  5、cpu_user              :用戶空間的cpu 使用量

  6、cpu_system            :內核空間的cpu 使用量

  7、swaps               :swap 發生的次數

 

3、例子:

  1、開啟監測

set @@ssesion.profiling=1;

  2、執行查詢

select * from tempdb.t;

  3、關閉監測

set @@ssesion.profiling=0;

  4、查詢監測到的數據

 select
    -> query_id, -- 查詢id 它用於標識一個查詢
    -> seq, -- 顯示序號
    -> (select sum(duration) from information_schema.profiling as innert where innert.query_id = outert.query_id) as total_cost , -- 總用時in seconds
    -> state,-- 狀態
    -> duration, -- 持續時間
    -> cpu_user, -- 用戶空間的cpu 使用量
    -> cpu_system, -- 內核空間的cpu 使用量
    -> -- context_voluntary, -- 自願上下文切換
    -> -- context_involuntary, -- 非自願上下文切換
    -> block_ops_in, -- 塊調入次數
    -> block_ops_out, -- 塊調出次數
    -> swaps -- 發生swap 的次數
    -> from
    -> information_schema.profiling as outert
    -> 
    -> order by
    -> seq;
+----------+-----+------------+----------------------+----------+----------+------------+--------------+---------------+-------+
| query_id | seq | total_cost | state                | duration | cpu_user | cpu_system | block_ops_in | block_ops_out | swaps |
+----------+-----+------------+----------------------+----------+----------+------------+--------------+---------------+-------+
|        1 |   2 |   0.001984 | starting             | 0.000058 | 0.000000 |   0.000000 |            0 |             0 |     0 |
|        1 |   3 |   0.001984 | checking permissions | 0.000010 | 0.000000 |   0.000000 |            0 |             0 |     0 |
|        1 |   4 |   0.001984 | Opening tables       | 0.000033 | 0.000000 |   0.000000 |            0 |             0 |     0 |
|        1 |   5 |   0.001984 | init                 | 0.000018 | 0.000000 |   0.000000 |            0 |             0 |     0 |
|        1 |   6 |   0.001984 | System lock          | 0.000012 | 0.000000 |   0.000000 |            0 |             0 |     0 |
|        1 |   7 |   0.001984 | optimizing           | 0.000006 | 0.000000 |   0.000000 |            0 |             0 |     0 |
|        1 |   8 |   0.001984 | statistics           | 0.000014 | 0.000000 |   0.000000 |            0 |             0 |     0 |
|        1 |   9 |   0.001984 | preparing            | 0.000013 | 0.000000 |   0.000000 |            0 |             0 |     0 |
|        1 |  10 |   0.001984 | executing            | 0.000003 | 0.000000 |   0.000000 |            0 |             0 |     0 |
|        1 |  11 |   0.001984 | Sending data         | 0.000130 | 0.000000 |   0.000000 |            0 |             0 |     0 |
|        1 |  12 |   0.001984 | end                  | 0.000009 | 0.000000 |   0.000000 |            0 |             0 |     0 |
|        1 |  13 |   0.001984 | query end            | 0.000008 | 0.000000 |   0.000000 |            0 |             0 |     0 |
|        1 |  14 |   0.001984 | closing tables       | 0.001613 | 0.002000 |   0.000000 |            0 |             0 |     0 |
|        1 |  15 |   0.001984 | freeing items        | 0.000037 | 0.000000 |   0.000000 |            0 |             0 |     0 |
|        1 |  16 |   0.001984 | cleaning up          | 0.000020 | 0.000000 |   0.000000 |            0 |             0 |     0 |
+----------+-----+------------+----------------------+----------+----------+------------+--------------+---------------+-------+

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM