您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)如何理解MySQL Profile在MySQL5.7的簡(jiǎn)單測(cè)試,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。
MySQL Profile對(duì)于分析執(zhí)行計(jì)劃的開(kāi)銷來(lái)說(shuō),還是有一定的幫助,至少在分析一些性能問(wèn)題的時(shí)候有很多的參考依據(jù)。
我在5.6, 5.7版本中進(jìn)行了測(cè)試,沒(méi)發(fā)現(xiàn)差別,還是以5.7為例進(jìn)行演示吧。
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.10 |
+-----------+
1 row in set (0.00 sec)
傳統(tǒng)的使用Profile都是使用show profile這樣的命令方式,這個(gè)功能默認(rèn)是關(guān)閉的。
mysql> show profiles;
Empty set, 1 warning (0.00 sec)
這個(gè)地方可以看到有一個(gè)警告,我們看看是什么警告。
mysql> show warnings;
+---------+------+--------------------------------------------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+--------------------------------------------------------------------------------------------------------------+
| Warning | 1287 | 'SHOW PROFILES' is deprecated and will be removed in a future release. Please use Performance Schema instead |
+---------+------+--------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
原來(lái)這種方式已經(jīng)過(guò)期了,新的功能是在performance_schema中開(kāi)放。當(dāng)然在5.6, 5.7版本中測(cè)試還是可用,我們先簡(jiǎn)單了解一下,再來(lái)看performance_schema怎么用。
Profile相關(guān)的幾個(gè)參數(shù)如下:
mysql> show variables like '%profil%';
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| have_profiling | YES |
| profiling | OFF |
| profiling_history_size | 15 |
+------------------------+-------+
3 rows in set (0.00 sec)
可以看到Profileing為OFF,當(dāng)前默認(rèn)值為0,代表的是一個(gè)意思。
mysql> SELECT @@profiling;
+-------------+
| @@profiling |
+-------------+
| 0 |
+-------------+
1 row in set, 1 warning (0.00 sec)
have_profiling 用于控制是否開(kāi)啟或者禁用Profiling
profiling_history_size是保留Profiling的數(shù)目
當(dāng)然本質(zhì)上,Profile的內(nèi)容還是來(lái)自于information_schema.profiling
mysql> select * from information_schema.profiling\G
Empty set, 1 warning (0.00 sec)
這個(gè)地方還是有一個(gè)警告,還是過(guò)期的提示。
mysql> show warnings;
+---------+------+-----------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+-----------------------------------------------------------------------------------------------------------------------------+
| Warning | 1287 | 'INFORMATION_SCHEMA.PROFILING' is deprecated and will be removed in a future release. Please use Performance Schema instead |
+---------+------+-----------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
我們開(kāi)啟profiling
mysql> set profiling=1;
Query OK, 0 rows affected, 1 warning (0.00 sec)
查看所有的profiles
mysql> show profiles;
+----------+------------+---------------+
| Query_ID | Duration | Query |
+----------+------------+---------------+
| 1 | 0.00018200 | show warnings |
+----------+------------+---------------+
1 row in set, 1 warning (0.00 sec)
我們順便運(yùn)行一條SQL
mysql> select count(*)from information_schema.columns;
+----------+
| count(*) |
+----------+
| 3077 |
+----------+
1 row in set (0.07 sec)
然后再次查看,就會(huì)看到query_ID會(huì)得到剛剛運(yùn)行的語(yǔ)句。
mysql> show profiles;
+----------+------------+------------------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+------------------------------------------------+
| 1 | 0.00018200 | show warnings |
| 2 | 0.06627200 | select count(*)from information_schema.columns |
+----------+------------+------------------------------------------------+
2 rows in set, 1 warning (0.00 sec)
可以使用如下的方式來(lái)查看profile的信息,比如涉及CPU的明細(xì)信息。
mysql> show profile cpu for query 2;
+----------------------+----------+----------+------------+
| Status | Duration | CPU_user | CPU_system |
+----------------------+----------+----------+------------+
| checking permissions | 0.000004 | 0.000000 | 0.000000 |
| checking permissions | 0.000053 | 0.000999 | 0.000000 |
| checking permissions | 0.000014 | 0.000000 | 0.000000 |
| checking permissions | 0.000006 | 0.000000 | 0.000000 |
。。。。。
| closing tables | 0.000005 | 0.000000 | 0.000000 |
| freeing items | 0.000052 | 0.000000 | 0.000000 |
| cleaning up | 0.000023 | 0.000000 | 0.000000 |
+----------------------+----------+----------+------------+
100 rows in set, 1 warning (0.00 sec)
除此之外,還有哪些選項(xiàng)呢,可以自由選用。
上面的內(nèi)容其實(shí)介于使用和過(guò)期之間,那么我們來(lái)看看新版本中推薦的performace_schema是怎么回事。
先切換到performance_schema下,這是MySQL新增的性能優(yōu)化引擎,在5.6以前是關(guān)閉的,5。6,5.7中是默認(rèn)開(kāi)啟的,5.7切換的時(shí)候還會(huì)有一句提示。
mysql> use performance_schema
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
使用profile涉及幾個(gè)表,setup_actors,setup_instruments,setup_consumers
說(shuō)白了都是配置,都是套路。
默認(rèn)表setup_actors的內(nèi)容如下:
mysql> SELECT * FROM setup_actors;
+------+------+------+---------+---------+
| HOST | USER | ROLE | ENABLED | HISTORY |
+------+------+------+---------+---------+
| % | % | % | YES | YES |
+------+------+------+---------+---------+
1 row in set (0.00 sec)
按照官方的建議,默認(rèn)是啟用,可以根據(jù)需求禁用。
UPDATE performance_schema.setup_actors SET ENABLED = 'NO', HISTORY = 'NO'
WHERE HOST = '%' AND USER = '%';
禁用后的內(nèi)容如下:
mysql> select * from setup_actors;
+------+------+------+---------+---------+
| HOST | USER | ROLE | ENABLED | HISTORY |
+------+------+------+---------+---------+
| % | % | % | NO | NO |
+------+------+------+---------+---------+
1 row in set (0.00 sec)
然后加入指定的用戶
INSERT INTO performance_schema.setup_actors (HOST,USER,ROLE,ENABLED,HISTORY)
VALUES('localhost','root','%','YES','YES');
加入成功后的數(shù)據(jù)內(nèi)容如下:
mysql> select * from setup_actors;
+-----------+------+------+---------+---------+
| HOST | USER | ROLE | ENABLED | HISTORY |
+-----------+------+------+---------+---------+
| % | % | % | NO | NO |
| localhost | root | % | YES | YES |
+-----------+------+------+---------+---------+
2 rows in set (0.00 sec)
好了,setup_actors的配置就這樣,另外兩個(gè)表的內(nèi)容修改也是大同小異。
表 setup_consumers 描述各種事件,setup_instruments 描述這個(gè)數(shù)據(jù)庫(kù)下的表名以及是否開(kāi)啟監(jiān)控
我統(tǒng)計(jì)了一下,兩個(gè)表的默認(rèn)數(shù)據(jù)還不少。
setup_instruments 1006 rows
setup_consumers 15 rows
我們按照官方的建議來(lái)修改,可以看到修改的不是一行,而是相關(guān)的很多行。
mysql> UPDATE performance_schema.setup_instruments SET ENABLED = 'YES', TIMED = 'YES'
-> WHERE NAME LIKE '%statement/%';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 192 Changed: 0 Warnings: 0
mysql> UPDATE performance_schema.setup_instruments SET ENABLED = 'YES', TIMED = 'YES'
-> WHERE NAME LIKE '%stage/%';
Query OK, 119 rows affected (0.00 sec)
Rows matched: 128 Changed: 119 Warnings: 0
mysql> UPDATE performance_schema.setup_consumers SET ENABLED = 'YES'
-> WHERE NAME LIKE '%events_statements_%';
Query OK, 1 row affected (0.01 sec)
Rows matched: 3 Changed: 1 Warnings: 0
mysql> UPDATE performance_schema.setup_consumers SET ENABLED = 'YES'
-> WHERE NAME LIKE '%events_stages_%';
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0
好了配置完成,我們來(lái)簡(jiǎn)單測(cè)試一下怎么用。
創(chuàng)建一個(gè)test數(shù)據(jù)庫(kù)。
mysql> create database test;
Query OK, 1 row affected (0.00 sec)
切換到test數(shù)據(jù)庫(kù)
mysql> use test
Database changed
創(chuàng)建一個(gè)測(cè)試表test_profile,插入幾行數(shù)據(jù)。
mysql> create table test_profile as select * from information_schema.columns limit 1,5;
Query OK, 5 rows affected (0.10 sec)
Records: 5 Duplicates: 0 Warnings: 0
運(yùn)行一下,我們根據(jù)這個(gè)語(yǔ)句來(lái)得到一些詳細(xì)的統(tǒng)計(jì)信息。
mysql> select * from test.test_profile limit 1,2;
根據(jù)下面的語(yǔ)句查詢一個(gè)歷史表,從表名可以看出是和事件相關(guān)的,感覺(jué)越來(lái)越像Oracle了。
mysql> SELECT EVENT_ID, TRUNCATE(TIMER_WAIT/1000000000000,6) as Duration, SQL_TEXT
-> FROM performance_schema.events_statements_history_long WHERE SQL_TEXT like '%limit 1,2%';
+----------+----------+-------------------------------------------+
| EVENT_ID | Duration | SQL_TEXT |
+----------+----------+-------------------------------------------+
| 4187 | 0.000424 | select * from test.test_profile limit 1,2 |
+----------+----------+-------------------------------------------+
1 row in set (0.00 sec)
我們通過(guò)上面的語(yǔ)句可以得到一個(gè)概覽,對(duì)應(yīng)的事件和執(zhí)行時(shí)間。
然后到stage相關(guān)的歷史表中查看事件的詳細(xì)信息,這就是我們期望的性能數(shù)據(jù)了。如此一來(lái)應(yīng)該就明白上面的配置表中所要做的工作是什么意思了。
mysql> SELECT event_name AS Stage, TRUNCATE(TIMER_WAIT/1000000000000,6) AS Duration
-> FROM performance_schema.events_stages_history_long WHERE NESTING_EVENT_ID=4187;
+--------------------------------+----------+
| Stage | Duration |
+--------------------------------+----------+
| stage/sql/starting | 0.000113 |
| stage/sql/checking permissions | 0.000008 |
| stage/sql/Opening tables | 0.000025 |
| stage/sql/init | 0.000062 |
| stage/sql/System lock | 0.000013 |
。。。
| stage/sql/freeing items | 0.000031 |
| stage/sql/cleaning up | 0.000002 |
+--------------------------------+----------+
15 rows in set (0.01 sec)
整體來(lái)看,看到這個(gè)特性的輸出,讓我忍不住想起了Oracle中的Datapump,因?yàn)檩敵鰧?shí)在是太像了,很有條理嘛。
以上就是如何理解MySQL Profile在MySQL5.7的簡(jiǎn)單測(cè)試,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。