分析SQL執行帶來的開銷是優化SQL的重要手段。在MySQL數據庫中,可以通過配置profiling參數來啟用SQL剖析。該參數可以在全局和session級別來設置。對於全局級別則作用於整個MySQL實例,而session級別緊影響當前session。該參數開啟后,后續執行的SQL語句都將記錄其資源開銷,諸如IO,上下文切換,CPU,Memory等等。根據這些開銷進一步分析當前SQL瓶頸從而進行優化與調整。本文描述了如何使用MySQL profile,不涉及具體的樣例分析。
如何查看執行SQL的耗時的步驟:開啟profile、發送sql、查看profile的資源開銷結果、關閉profile。
1 mysql> select version(); 2 profile默認是不打開的 3 mysql> show profiles; 4 Empty set (0.02 sec) 5 驗證修改后的結果 6 7 mysql> show variables like "%pro%"; 8 可以看到profiling 默認是OFF的。 9 開啟profile,然后測試 10 11 開啟profile 12 mysql> set profiling=1; 13 獲取profile的幫助 14 help profile; 15 16 root@localhost[sakila]> help profile; 17 Name: 'SHOW PROFILE' 18 Description: 19 Syntax: 20 SHOW PROFILE [type [, type] ... ] 21 [FOR QUERY n] 22 [LIMIT row_count [OFFSET offset]] 23 24 type: 25 ALL --顯示所有的開銷信息 26 | BLOCK IO --顯示塊IO相關開銷 27 | CONTEXT SWITCHES --上下文切換相關開銷 28 | CPU --顯示CPU相關開銷信息 29 | IPC --顯示發送和接收相關開銷信息 30 | MEMORY --顯示內存相關開銷信息 31 | PAGE FAULTS --顯示頁面錯誤相關開銷信息 32 | SOURCE --顯示和Source_function,Source_file,Source_line相關的開銷信息 33 | SWAPS --顯示交換次數相關開銷的信息
執行業務SQL,並用profile分析示例:
--發布SQL查詢
1 root@localhost[sakila]> select count(*) from customer; 2 +----------+ 3 | count(*) | 4 +----------+ 5 | 599 | 6 +----------+ 7 8 --查看當前session所有已產生的profile 9 root@localhost[sakila]> show profiles; 10 +----------+------------+--------------------------------+ 11 | Query_ID | Duration | Query | 12 +----------+------------+--------------------------------+ 13 | 1 | 0.00253600 | show variables like '%profil%' | 14 | 2 | 0.00138150 | select count(*) from customer | 15 +----------+------------+--------------------------------+ 16 2 rows in set, 1 warning (0.01 sec) 17 18 --我們看到有2個warning,之前一個,現在一個 19 root@localhost[sakila]> show warnings; --下面的結果表明SHOW PROFILES將來會被Performance Schema替換掉 20 +---------+------+--------------------------------------------------------------------------------------------------------------+ 21 | Level | Code | Message | 22 +---------+------+--------------------------------------------------------------------------------------------------------------+ 23 | Warning | 1287 | 'SHOW PROFILES' is deprecated and will be removed in a future release. Please use Performance Schema instead | 24 +---------+------+--------------------------------------------------------------------------------------------------------------+
獲取SQL語句的開銷信息
--可以直接使用show profile來查看上一條SQL語句的開銷信息
--注,show profile之類的語句不會被profiling,即自身不會產生Profiling
--我們下面的這個show profile查看的是show warnings產生的相應開銷
1 root@localhost[sakila]> show profile; 2 +----------------+----------+ 3 | Status | Duration | 4 +----------------+----------+ 5 | starting | 0.000141 | 6 | query end | 0.000058 | 7 | closing tables| 0.000014 | 8 | freeing items | 0.001802 | 9 | cleaning up | 0.000272 | 10 +----------------+----------+ 11 12 --如下面的查詢show warnings被添加到profiles 13 root@localhost[sakila]> show profiles; 14 +----------+------------+--------------------------------+ 15 | Query_ID | Duration | Query | 16 +----------+------------+--------------------------------+ 17 | 1 | 0.00253600 | show variables like '%profil%' | 18 | 2 | 0.00138150 | select count(*) from customer | 19 | 3 | 0.00228600 | show warnings | 20 +----------+------------+--------------------------------+ 21 22 --獲取指定查詢的開銷(第二條查詢的開銷明細) 23 root@localhost[sakila]> show profile for query 2; 24 +----------------------+----------+ 25 | Status | Duration | 26 +----------------------+----------+ 27 | starting | 0.000148 | 28 | checking permissions | 0.000014 | 29 | Opening tables | 0.000047 | 30 | init | 0.000023 | 31 | System lock | 0.000035 | 32 | optimizing | 0.000012 | 33 | statistics | 0.000019 | 34 | preparing | 0.000014 | 35 | executing | 0.000006 | 36 | Sending data | 0.000990 | 37 | end | 0.000010 | 38 | query end | 0.000011 | 39 | closing tables | 0.000010 | 40 | freeing items | 0.000016 | 41 | cleaning up | 0.000029 | 42 +----------------------+----------+ 43 44 --查看特定部分的開銷,如下為CPU部分的開銷 45 root@localhost[sakila]> show profile cpu for query 2 ; 46 +----------------------+----------+----------+------------+ 47 | Status | Duration | CPU_user | CPU_system | 48 +----------------------+----------+----------+------------+ 49 | starting | 0.000148 | 0.000000 | 0.000000 | 50 | checking permissions | 0.000014 | 0.000000 | 0.000000 | 51 | Opening tables | 0.000047 | 0.000000 | 0.000000 | 52 | init | 0.000023 | 0.000000 | 0.000000 | 53 | System lock | 0.000035 | 0.000000 | 0.000000 | 54 | optimizing | 0.000012 | 0.000000 | 0.000000 | 55 | statistics | 0.000019 | 0.000000 | 0.000000 | 56 | preparing | 0.000014 | 0.000000 | 0.000000 | 57 | executing | 0.000006 | 0.000000 | 0.000000 | 58 | Sending data | 0.000990 | 0.001000 | 0.000000 | 59 | end | 0.000010 | 0.000000 | 0.000000 | 60 | query end | 0.000011 | 0.000000 | 0.000000 | 61 | closing tables | 0.000010 | 0.000000 | 0.000000 | 62 | freeing items | 0.000016 | 0.000000 | 0.000000 | 63 | cleaning up | 0.000029 | 0.000000 | 0.000000 | 64 +----------------------+----------+----------+------------+ 65 66 --如下為MEMORY部分的開銷 67 root@localhost[sakila]> show profile memory for query 2 ; 68 +----------------------+----------+ 69 | Status | Duration | 70 +----------------------+----------+ 71 | starting | 0.000148 | 72 | checking permissions | 0.000014 | 73 | Opening tables | 0.000047 | 74 | init | 0.000023 | 75 | System lock | 0.000035 | 76 | optimizing | 0.000012 | 77 | statistics | 0.000019 | 78 | preparing | 0.000014 | 79 | executing | 0.000006 | 80 | Sending data | 0.000990 | 81 | end | 0.000010 | 82 | query end | 0.000011 | 83 | closing tables | 0.000010 | 84 | freeing items | 0.000016 | 85 | cleaning up | 0.000029 | 86 +----------------------+----------+ 87 88 --同時查看不同資源開銷 89 root@localhost[sakila]> show profile block io,cpu for query 2; 90 +----------------------+----------+----------+------------+--------------+---------------+ 91 | Status | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out | 92 +----------------------+----------+----------+------------+--------------+---------------+ 93 | starting | 0.000148 | 0.000000 | 0.000000 | 0 | 0 | 94 | checking permissions | 0.000014 | 0.000000 | 0.000000 | 0 | 0 | 95 | Opening tables | 0.000047 | 0.000000 | 0.000000 | 0 | 0 | 96 | init | 0.000023 | 0.000000 | 0.000000 | 0 | 0 | 97 | System lock | 0.000035 | 0.000000 | 0.000000 | 0 | 0 | 98 | optimizing | 0.000012 | 0.000000 | 0.000000 | 0 | 0 | 99 | statistics | 0.000019 | 0.000000 | 0.000000 | 0 | 0 | 100 | preparing | 0.000014 | 0.000000 | 0.000000 | 0 | 0 | 101 | executing | 0.000006 | 0.000000 | 0.000000 | 0 | 0 | 102 | Sending data | 0.000990 | 0.001000 | 0.000000 | 0 | 0 | 103 | end | 0.000010 | 0.000000 | 0.000000 | 0 | 0 | 104 | query end | 0.000011 | 0.000000 | 0.000000 | 0 | 0 | 105 | closing tables | 0.000010 | 0.000000 | 0.000000 | 0 | 0 | 106 | freeing items | 0.000016 | 0.000000 | 0.000000 | 0 | 0 | 107 | cleaning up | 0.000029 | 0.000000 | 0.000000 | 0 | 0 | 108 +----------------------+----------+----------+------------+--------------+---------------+
--下面的SQL語句用於查詢query_id為2的SQL開銷,且按最大耗用時間倒序排列
1 root@localhost[sakila]> set @query_id=2; 2 3 root@localhost[sakila]> SELECT STATE, SUM(DURATION) AS Total_R, 4 ROUND( 5 100 * SUM(DURATION) / 6 (SELECT SUM(DURATION) 7 FROM INFORMATION_SCHEMA.PROFILING 8 WHERE QUERY_ID = @query_id 9 ), 2) AS Pct_R, 10 COUNT(*) AS Calls, 11 SUM(DURATION) / COUNT(*) AS "R/Call" 12 FROM INFORMATION_SCHEMA.PROFILING 13 WHERE QUERY_ID = @query_id 14 GROUP BY STATE 15 ORDER BY Total_R DESC; 16 +----------------------+----------+-------+-------+--------------+ 17 | STATE | Total_R | Pct_R | Calls | R/Call | 18 +----------------------+----------+-------+-------+--------------+ 19 | Sending data | 0.000990 | 71.53 | 1 | 0.0009900000 |--最大耗用時間部分為發送數據 20 | starting | 0.000148 | 10.69 | 1 | 0.0001480000 | 21 | Opening tables | 0.000047 | 3.40 | 1 | 0.0000470000 | 22 | System lock | 0.000035 | 2.53 | 1 | 0.0000350000 | 23 | cleaning up | 0.000029 | 2.10 | 1 | 0.0000290000 | 24 | init | 0.000023 | 1.66 | 1 | 0.0000230000 | 25 | statistics | 0.000019 | 1.37 | 1 | 0.0000190000 | 26 | freeing items | 0.000016 | 1.16 | 1 | 0.0000160000 | 27 | preparing | 0.000014 | 1.01 | 1 | 0.0000140000 | 28 | checking permissions | 0.000014 | 1.01 | 1 | 0.0000140000 | 29 | optimizing | 0.000012 | 0.87 | 1 | 0.0000120000 | 30 | query end | 0.000011 | 0.79 | 1 | 0.0000110000 | 31 | end | 0.000010 | 0.72 | 1 | 0.0000100000 | 32 | closing tables | 0.000010 | 0.72 | 1 | 0.0000100000 | 33 | executing | 0.000006 | 0.43 | 1 | 0.0000060000 | 34 +----------------------+----------+-------+-------+--------------+
--開啟profiling后,我們可以通過show profile等方式查看,其實質是這些開銷信息被記錄到information_schema.profiling表
--如下面的查詢,部分信息省略
1 profiling 2 root@localhost[information_schema]> select * from profiling limit 3,3\G; 3 *************************** 1. row *************************** 4 QUERY_ID: 1 5 SEQ: 5 6 STATE: init 7 DURATION: 0.000020 8 CPU_USER: 0.000000 9 CPU_SYSTEM: 0.000000 10 CONTEXT_VOLUNTARY: 0 11 CONTEXT_INVOLUNTARY: 0 12 BLOCK_OPS_IN: 0 13 BLOCK_OPS_OUT: 0 14 MESSAGES_SENT: 0 15 MESSAGES_RECEIVED: 0 16 PAGE_FAULTS_MAJOR: 0 17 PAGE_FAULTS_MINOR: 0 18 SWAPS: 0 19 SOURCE_FUNCTION: mysql_prepare_select 20 SOURCE_FILE: sql_select.cc 21 SOURCE_LINE: 1050
--停止profile,可以設置profiling參數,或者在session退出之后,profiling會被自動關閉
1 root@localhost[sakila]> set profiling=off; 2 Query OK, 0 rows affected, 1 warning (0.00 sec)
————————————————
聚焦技術與人文,分享干貨,共同成長更多內容請關注“數據與人”


