mysql的sql效能分析器

來源:互聯網
上載者:User

標籤:


MySQL 的SQL效能分析器主要用途是顯示SQL執行的整個過程中各項資源的使用方式。分析器可以更好的展示
出不良SQL的效能問題所在。

mysql sql profile的使用方法

1.開啟mysql sql profile

檢查mysql sql profile是否啟用
mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
|           0 | 
+-------------+
1 row in set (0.01 sec)

預設情況下 profiling 的值為0表示 MySQL SQL Profiler處於OFF狀態,如果開啟SQL效能分析器後, profiling 的值將為1.

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

mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
|           1 | 
+-------------+
1 row in set (0.01 sec)


上面可以看到profiling已經變為1了,但是這個是session層級的,系統是不支援的。如下測試

退出mysql
mysql> quit
Bye
[[email protected] ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.0.45-log Source distribution

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the buffer.

查看profiling的值
mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
|           0 | 
+-------------+
1 row in set (0.01 sec)

發現已經變為預設值0了,那如果設定系統級會如何呢?

mysql> set global profiling=1;
ERROR 1228 (HY000): Variable ‘profiling‘ is a SESSION variable and can‘t be used with SET GLOBAL
mysql> 

看到這裡報錯了。所以mysql sql profile是session層級的。

2. 舉個例如,看如何使用

mysql> create table t5 as select * from t1;
ERROR 1046 (3D000): No database selected
mysql> use backup;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> create table t5 as select * from t1;
Query OK, 2 rows affected (0.06 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select count(*) from t5;
+----------+
| count(*) |
+----------+
|        2 | 
+----------+
1 row in set (0.00 sec)

mysql> select count(*) from t5;
+----------+
| count(*) |
+----------+
|        2 | 
+----------+
1 row in set (0.00 sec)

mysql> show profiles;
+----------+------------+-------------------------------------+
| Query_ID | Duration   | Query                               |
+----------+------------+-------------------------------------+
|        1 | 0.00382400 | select @@profiling                  | 
|        2 | 0.00268500 | create table t5 as select * from t1 | 
|        3 | 0.00017200 | SELECT DATABASE()                   | 
|        4 | 0.01985400 | show databases                      | 
|        5 | 0.00018900 | show tables                         | 
|        6 | 0.06225200 | create table t5 as select * from t1 | 
|        7 | 0.00368800 | select count(*) from t5             | 
|        8 | 0.00322200 | select count(*) from t5             | 
+----------+------------+-------------------------------------+
8 rows in set (0.01 sec)

mysql> 
mysql> show profile for query 7;
+--------------------+----------+
| Status             | Duration |
+--------------------+----------+
| (initialization)   | 0.000414 | 
| Opening tables     | 0.000599 | 
| System lock        | 0.000254 | 
| Table lock         | 0.000175 | 
| init               | 0.000052 | 
| optimizing         | 0.00001  | 
| executing          | 0.002107 | 
| end                | 0.000042 | 
| query end          | 0.000005 | 
| freeing items      | 0.000014 | 
| closing tables     | 0.000011 | 
| logging slow query | 0.000005 | 
+--------------------+----------+
12 rows in set (0.03 sec)

mysql> show profile for query 8;
+--------------------+----------+
| Status             | Duration |
+--------------------+----------+
| (initialization)   | 0.000064 | 
| Opening tables     | 0.000018 | 
| System lock        | 0.00001  | 
| Table lock         | 0.000013 | 
| init               | 0.00002  | 
| optimizing         | 0.00001  | 
| executing          | 0.002589 | 
| end                | 0.000459 | 
| query end          | 0.000007 | 
| freeing items      | 0.000015 | 
| closing tables     | 0.000012 | 
| logging slow query | 0.000005 | 
+--------------------+----------+
12 rows in set (0.00 sec)

mysql> select sum(format(duration,6)) as duration from information_schema.profiling where query_id=7;
+----------+
| duration |
+----------+
| 0.003688 | 
+----------+
1 row in set (0.02 sec)

mysql> select sum(format(duration,6)) as duration from information_schema.profiling where query_id=8;
+----------+
| duration |
+----------+
| 0.003222 | 
+----------+
1 row in set (0.00 sec)

mysql> 

從如上的資訊可以看出這兩個sql的profile統計資訊裡,前4項差別比較大,這是兩個sql主要區別,第二次查詢有很多

緩衝了了。SQL 效能分析器可以協助我們對一些比較難以確定效能問題的 SQL 進行診斷,找出問題根源。

 

 

------end-----

mysql的sql效能分析器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.