MySQL性能分析工具profiling

MySQL性能分析工具profiling

MySQL數據庫中,可以通過配置profiling參數來啓用SQL性能分析,找出當前SQL瓶頸,進而優化、調整sql語句。

profiling參數開啓後,後續執行的SQL語句都將記錄其資源開銷,諸如IO,IPC,CPU,Memory等等。

用法:

mysql> help profile

Name:'SHOW PROFILE'

Description:

Syntax:

SHOW PROFILE [type [, type] ... ]

   [FOR QUERY n]

   [LIMIT row_count [OFFSET offset]]

type:

   ALL

  |BLOCK IO

  |CONTEXT SWITCHES

  |CPU

  |IPC

  |MEMORY

  |PAGE FAULTS

  |SOURCE

  |SWAPS

Profiling變量默認值爲0off),可以通過設置profiling0ON開啓

mysql>select @@profiling;

+-------------+

|@@profiling |

+-------------+

|          0 |

+-------------+

1 row inset (0.03 sec)

mysql>show variables like '%profil%';

+------------------------+-------+

|Variable_name         | Value |

+------------------------+-------+

|profiling             | OFF  | 

|profiling_history_size | 15   |

+------------------------+-------+

2 rows inset (0.00 sec)

profiling:設置是否開啓SQL性能剖析

profiling_history_size:設置是SQL性能剖析緩存的記錄數

mysql>set profiling=1;

Query OK,0 rows affected (0.03 sec)

mysql>show variables like '%profil%';

+------------------------+-------+

|Variable_name         | Value |

+------------------------+-------+

|profiling             | ON   |

|profiling_history_size | 15   |

+------------------------+-------+

2 rows inset (0.00 sec)

mysql>show variables like 'profiling_history_size';

+------------------------+-------+

|Variable_name         | Value |

+------------------------+-------+

|profiling_history_size | 15   |

+------------------------+-------+

1 row inset (0.00 sec)

show profiles:返回一個列表,顯示最近發送到服務器上執行的語句的資源使用情況.

顯示的記錄數由變量“profiling_history_size”控制,默認15條,最大值爲100,可以手動設置該參數值。

mysql>set profiling_history_size = 30;

Query OK,0 rows affected (0.00 sec)

mysql>show variables like '%profil%';

+------------------------+-------+

|Variable_name         | Value |

+------------------------+-------+

|profiling             | ON   |

|profiling_history_size | 30   |

+------------------------+-------+

2 rows inset (0.00 sec)

 

選項註釋:

type:

   ALL :顯示所有信息

  |BLOCK IO :塊設備IO輸入輸出次數

  |CONTEXT SWITCHES:上下文切換相關開銷

  |CPU:用戶和系統的CPU使用情況

  |IPC:顯示發送和接收消息的相關消耗

  |MEMORY:內存消耗情況(該版本is not currently implemented)

  |PAGE FAULTS:顯示主要和次要頁面故障相關的開銷

  |SOURCE:顯示和Source_function,Source_file,Source_line相關的開銷信息

  |SWAPS:顯示交換次數相關的開銷

Profilingis enabled per session. When a session ends, itsprofiling

information is lost. 

注意:profiling被應用在每一個會話中,當前會話關閉後,profiling統計的信息將丟失。

用法示例:

mysql>use mydb

mysql>drop table if exists t4;

Query OK,0 rows affected, 1 warning (0.00 sec)

mysql>create table cid(id int);

Query OK,0 rows affected (0.05 sec)

mysql>show profiles;

+----------+------------+----------------------------------------------+

| Query_ID| Duration   |Query                                       |

+----------+------------+----------------------------------------------+

|       1 | 0.00061900 | show variables like '%profil%'              |

|       2 | 0.00061800 | show variables like 'profiling_history_size'|

|       3 | 0.00010600 | set profiling_history_size = 30             |

|       4 | 0.00078200 | show variables like '%profil%'              |

|       5 | 0.00018800 | SELECT DATABASE()                           |

|       6 | 0.00050400 | show databases                              |

|       7 | 0.00032200 | show tables                                 |

|       8 | 0.00027300 | drop table if exists t4                     |

|       9 | 0.04367500 | create table cid(id int)                    |

+----------+------------+----------------------------------------------+

9 rows inset (0.00 sec)

mysql>show profile; -->默認會打開上一個語句的開銷信息

+----------------------+----------+

|Status              | Duration |

+----------------------+----------+

|starting            | 0.000074 |

| checkingpermissions | 0.000083 |

| creatingtable      | 0.043427 |

| Aftercreate        | 0.000014 |

| queryend           | 0.000003 |

| freeingitems       | 0.000068 |

| loggingslow query   |0.000004 |

| cleaningup         | 0.000002 |

+----------------------+----------+

8 rows inset (0.02 sec)

mysql>show profile for query 9;    -->獲取指定查詢的開銷 

+----------------------+----------+

|Status              | Duration |

+----------------------+----------+

|starting            | 0.000074 |

| checkingpermissions | 0.000083 |

| creatingtable      | 0.043427 |

| Aftercreate        | 0.000014 |

| queryend           | 0.000003 |

| freeingitems       | 0.000068 |

| loggingslow query   |0.000004 |

| cleaningup         | 0.000002 |

+----------------------+----------+

8 rows inset (0.00 sec)

-->查看特定部分的開銷,如CPU,SWAPS,BLOCK IO的開銷

mysql>show profile CPU,SWAPS,BLOCK IO for query 9;

+----------------------+----------+----------+------------+--------------+---------------+-------+

|Status              |Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |Swaps |

+----------------------+----------+----------+------------+--------------+---------------+-------+

|starting            | 0.000074 | 0.000000 |  0.000000 |           0|            0 |    0 |

| checkingpermissions | 0.000083 | 0.000000 |  0.000000 |           0 |            0 |    0 |

| creatingtable      | 0.043427 | 0.009998 |  0.017997 |           0 |          256 |    0 |

| Aftercreate        | 0.000014 | 0.000000 |  0.000000 |           0 |            0 |    0 |

| queryend           | 0.000003 | 0.000000 |  0.000000 |           0 |            0 |    0 |

| freeingitems       | 0.000068 | 0.000000 |  0.000000 |           0 |            0 |    0 |

| loggingslow query   |0.000004 | 0.000000 |  0.000000 |           0 |            0 |    0 |

| cleaningup         | 0.000002 | 0.000000 |  0.000000 |           0 |            0 |    0 |

+----------------------+----------+----------+------------+--------------+---------------+-------+

8 rows inset (0.00 sec)

mysql>show profile ALL for query 9;

+----------------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+---------------+-------------------+-------------------+-------------------+-------+------------------+--------------+-------------+

|Status              | Duration | CPU_user | CPU_system | Context_voluntary |Context_involuntary | Block_ops_in | Block_ops_out | Messages_sent| Messages_received | Page_faults_major | Page_faults_minor | Swaps| Source_function  |Source_file | Source_line |

+----------------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+---------------+-------------------+-------------------+-------------------+-------+------------------+--------------+-------------+

|starting            | 0.000074 | 0.000000 |  0.000000 |                0 |                  0 |           0 |            0 |            0 |                0 |                0 |                3 |    0 | NULL            |NULL        |       NULL |

| checkingpermissions | 0.000083 | 0.000000 |  0.000000 |                0 |                  0 |           0 |            0 |            0 |                0 |                0 |                4 |    0 | unknown function | sql_parse.cc |       5369 |

| creatingtable      | 0.043427 | 0.009998 |  0.017997 |               17 |                  0 |           0 |          256 |            0 |                0 |                0 |               26 |    0 |unknown function | sql_table.cc |       3975 |

| Aftercreate        | 0.000014 | 0.000000 |  0.000000 |                0 |                  0 |           0 |            0 |            0 |                0 |                0 |                0|    0 | unknown function | sql_table.cc |       4062 |

| queryend           | 0.000003 | 0.000000 |  0.000000 |                0 |                  0 |           0 |            0 |            0 |                0 |                0 |                0|    0 | unknown function | sql_parse.cc |       5133 |

| freeingitems       | 0.000068 | 0.000000 |  0.000000 |                0 |                  0 |           0 |            0 |            0 |                0 |                0|                0 |    0 | unknown function | sql_parse.cc |       6157 |

| loggingslow query   |0.000004 | 0.000000 |  0.000000 |                0 |                  0 |           0 |            0 |            0 |                0 |                0|                0 |    0 | unknown function | sql_parse.cc |       1745 |

| cleaningup         | 0.000002 | 0.000000 |  0.000000 |                0 |                  0 |           0 |            0 |            0 |                0 |                0 |                0 |    0 | unknown function | sql_parse.cc |       1713 |

+----------------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+---------------+-------------------+-------------------+-------------------+-------+------------------+--------------+-------------+

8 rows inset (0.00 sec)

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章