1. 程式人生 > >mysql效能分析show profile/show profiles

mysql效能分析show profile/show profiles

MySQL效能分析show profiles

show profile 和 show profiles 語句可以展示當前會話(退出session後,profiling重置為0) 中執行語句的資源使用情況。

Profiling 功能由MySQL會話變數 : profiling控制,預設是OFF.關閉狀態。select @@profiling; 或者show variables like '%profi%';

mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
|           0
| +-------------+

Profiling是針對程序(process)而非執行緒(threads),因此執行在伺服器上的其他服務程序可能會影響分析結果。Profiling 資訊收集依賴於呼叫 系統方法 getrusage().因此Windows系統不適用。

開啟Profiling功能:

mysql> set profiling = 1;
Query OK, 0 rows affected, 1 warning (0.00 sec)

退出會話後,Profiling功能自動關閉。

語句使用:

show profiles :列表,顯示最近傳送到伺服器上執行的語句的資源使用情況.顯示的記錄數由變數:profiling_history_size

 控制,預設15條。

mysql> show profiles;
+----------+------------+-------------------------------+
| Query_ID | Duration   | Query                         |
+----------+------------+-------------------------------+
|        1 | 0.00371475 | show variables like '%profi%' |
|        2 | 0.00005700 | show prifiles                 |
| 3 | 0.00011775 | SELECT DATABASE() | | 4 | 0.00034875 | select * from student | +----------+------------+-------------------------------+

show profile: 展示最近一條語句執行的詳細資源佔用資訊,預設顯示 Status和Duration兩列

mysql> show profile;
+----------------------+----------+
| Status               | Duration |
+----------------------+----------+
| starting             | 0.000054 |
| checking permissions | 0.000007 |
| Opening tables       | 0.000116 |
| init                 | 0.000019 |
| System lock          | 0.000009 |
| optimizing           | 0.000004 |
| statistics           | 0.000011 |
| preparing            | 0.000010 |
| executing            | 0.000002 |
| Sending data         | 0.000061 |
| end                  | 0.000005 |
| query end            | 0.000006 |
| closing tables       | 0.000006 |
| freeing items        | 0.000031 |
| cleaning up          | 0.000010 |
+----------------------+----------+

show profile 還可根據 show profiles 列表中的 Query_ID ,選擇顯示某條記錄的效能分析資訊。

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
}

例項:

Examples:
mysql> SELECT @@profiling;
+-------------+
| @@profiling |
+-------------+
|           0 |
+-------------+
1 row in set (0.00 sec)

mysql> SET profiling = 1;
Query OK, 0 rows affected (0.00 sec)

mysql> DROP TABLE IF EXISTS t1;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> CREATE TABLE T1 (id INT);
Query OK, 0 rows affected (0.01 sec)

mysql> SHOW PROFILES;
+----------+----------+--------------------------+
| Query_ID | Duration | Query                    |
+----------+----------+--------------------------+
|        0 | 0.000088 | SET PROFILING = 1        |
|        1 | 0.000136 | DROP TABLE IF EXISTS t1  |
|        2 | 0.011947 | CREATE TABLE t1 (id INT) |
+----------+----------+--------------------------+
3 rows in set (0.00 sec)

mysql> SHOW PROFILE;
+----------------------+----------+
| Status               | Duration |
+----------------------+----------+
| checking permissions | 0.000040 |
| creating table       | 0.000056 |
| After create         | 0.011363 |
| query end            | 0.000375 |
| freeing items        | 0.000089 |
| logging slow query   | 0.000019 |
| cleaning up          | 0.000005 |
+----------------------+----------+
7 rows in set (0.00 sec)

mysql> SHOW PROFILE FOR QUERY 1;
+--------------------+----------+
| Status             | Duration |
+--------------------+----------+
| query end          | 0.000107 |
| freeing items      | 0.000008 |
| logging slow query | 0.000015 |
| cleaning up        | 0.000006 |
+--------------------+----------+
4 rows in set (0.00 sec)

mysql> SHOW PROFILE CPU FOR QUERY 2;
+----------------------+----------+----------+------------+
| Status               | Duration | CPU_user | CPU_system |
+----------------------+----------+----------+------------+
| checking permissions | 0.000040 | 0.000038 |   0.000002 |
| creating table       | 0.000056 | 0.000028 |   0.000028 |
| After create         | 0.011363 | 0.000217 |   0.001571 |
| query end            | 0.000375 | 0.000013 |   0.000028 |
| freeing items        | 0.000089 | 0.000010 |   0.000014 |
| logging slow query   | 0.000019 | 0.000009 |   0.000010 |
| cleaning up          | 0.000005 | 0.000003 |   0.000002 |
+----------------------+----------+----------+------------+
7 rows in set (0.00 sec)