mysql show profiles 性能跟蹤診斷工具
前言
Show profiles最大的好處是可以查看執行sql語句時各個環節的執行時間,
Show profiles是5.0.37之后添加的,要想使用此功能,要確保版本在5.0.37之后。
可以執行sqlselect version();查看當前數據庫版本。
開啟 profiling
啟動數據庫后,profiles默認是關閉的,并且開啟后只存在當前會話,因為它是保存在內存中的,不會持久化存儲,重新登錄或者重啟后就會重置;可通過下面的語句開啟
-- 以下2條語句選其一即可 set profiling = 1; set profiling=on;
1
2
3
4
通過下面的語句查看profiles的啟動狀態,Value值為ON 表示是開啟的
-- 這2條語句選其一即可 show variables like 'profiling'; select @@profiling;
1
2
3
4
show profiles 查看sql執行所需時間
接下來我們查詢一條sql,
mysql> select * from student limit 1; +-----+--------+------+ | id | name | age | +-----+--------+------+ | 145 | 王五 | 30 | +-----+--------+------+ 1 row in set (0.00 sec)
1
2
3
4
5
6
7
然后在輸入show profiles;,就會顯示剛剛查詢的sql執行了多長時間,其中(Druation的單位為秒)
mysql> show profiles; +----------+------------+---------------------------------+ | Query_ID | Duration | Query | +----------+------------+---------------------------------+ | 7 | 0.00023875 | select * from student limit 1 | +----------+------------+----------------------------------
1
2
3
4
5
6
我們看到結果中有個Query_ID,輸入 命令show profile for query 7;,則可以看到更加詳細的時間,我們可以非常清楚的查看每一步的耗時,其中Druation的單位為秒)。這樣,當我們遇到一條慢SQL時,就能很清楚的知道,為什么慢,慢在哪一步了。
mysql> show profile for query 7; +----------------------+----------+ | Status | Duration | +----------------------+----------+ | starting | 0.000062 | | checking permissions | 0.000007 | | Opening tables | 0.000023 | | init | 0.000021 | | System lock | 0.000008 | | optimizing | 0.000005 | | statistics | 0.000014 | | preparing | 0.000012 | | executing | 0.000003 | | Sending data | 0.000044 | | end | 0.000004 | | query end | 0.000007 | | closing tables | 0.000008 | | freeing items | 0.000010 | | cleaning up | 0.000012 | +----------------------+----------+ 15 rows in set, 1 warning (0.00 sec)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
那么這些status都代表什么意思呢?我們選擇幾個比較重要的項說明一下
starting
lex+yacc 語法語義解析,得到解析樹
checking permissions
檢查權限,但是在某些SQL語句下,狀態為checking permission時,并不一定真的在做權限檢查。
所以不要頻繁的向數據庫發送show full tables from test like ‘%demo%’,尤其是在表的數量很多時。
Opening tables
打開表做好table cache,做好和innodb表物理文件的關聯,同時加MDL LOCK 主要函數open_tables
System lock
主要函數handler::ha_external_lock,之前會實現myisam等引擎的Mysql層表鎖,innodb做共享表鎖。
Sending data
“Sending data”并不是單純的發送數據,而是包括“收集 + 發送 數據”。這里是收集是從磁盤收集數據;
show warnings 查看警告信息
持此之外我們還看到最后一行有有一個警告
15 rows in set, 1 warning (0.00 sec)
1
接下來我們可以通過# show warnings命令看看這個警告是什么
mysql> show warnings; +---------+------+-------------------------------------------------------------------------------------------------------------+ | Level | Code | Message | +---------+------+-------------------------------------------------------------------------------------------------------------+ | Warning | 1287 | 'SHOW PROFILE' is deprecated and will be removed in a future release. Please use Performance Schema instead | +---------+------+-------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
1
2
3
4
5
6
7
8
這個警告是告訴你“SHOW PROFILE”已棄用,將在將來的版本中刪除。請改用性能模式;雖然說會被棄用,但是在低版本中,這個命令還是會被經常用到。
其他用法
除此之外,show profile [type] 還有一個type屬性,可以指定 可選值以顯示特定的附加信息類型:
ALL
: 顯示所有信息
BLOCK IO
: 顯示塊輸入和輸出操作的計數
CONTEXT SWITCHES
:顯示自愿和非自愿上下文切換的計數
CPU
:顯示用戶和系統 CPU 使用時間
IPC
:顯示發送和接收消息的計數
MEMORY
:目前未實施
PAGE FAULTS
:顯示主要和次要頁面錯誤的計數
SOURCE
:顯示源代碼中函數的名稱,以及函數所在文件的名稱和行號
SWAPS
: 顯示交換計數
完
MySQL SQL
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。