共計 2979 個字符,預計需要花費 8 分鐘才能閱讀完成。
自動寫代碼機器人,免費開通
這篇文章給大家介紹 profile 怎么在 mysql 中使用,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
profile 是什么
當我們要對某一條 sql 的性能進行分析時,可以使用它。
Profiling 是從 mysql5.0.3 版本以后才開放的。
啟動 profile 之后,所有查詢包括錯誤的語句都會記錄在內。
關閉會話或者 set profiling=0 就關閉了。(如果將 profiling_history_size 參數設置為 0,同樣具有關閉 MySQL 的 profiling 效果。)
此工具可用來查詢 SQL 執行狀態,System lock 和 Table lock 花多少時間等等,
對定位一條語句的 I / O 消耗和 CPU 消耗 非常重要。(SQL 語句執行所消耗的最大兩部分資源就是 IO 和 CPU)
– 在 mysql5.7 之后,profile 信息將逐漸被廢棄,mysql 推薦使用 performance schema
mysql 官網定義
The SHOW PROFILE and SHOW PROFILES statements display profiling information that indicates resource usage for statements executed during the course of the current session.
簡單的說,當前會話資源的消耗情況。
注意:show profile 和 show Profiles 都是不建議使用的,在 mysql 后期的版本中可能會被刪除;官網建議使用 Performance Schema
怎么使用
profile 默認關閉,生產環境中也建議關閉。
查看當前環境的 profile 設置
mysql show variables like %profiling%
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| have_profiling | YES |
| profiling | OFF |
| profiling_history_size | 15 |
+------------------------+-------+
profiling off 表示 profile 關閉,profiling_history_size 15 表示保存最近 15 條 SQL 的資源消耗情況。
開啟 profile 功能,可以使用命令
set global profiling = 1;
然后就可以使用下面命令
show profiles;
查看最近 15 條 SQL 的情況;
如果要查看某一條的具體情況,SQL 格式為:
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
}
官網對 type 中各個字段的解釋為:
ALL displays all information
BLOCK IO displays counts for block input and output operations
CONTEXT SWITCHES displays counts for voluntary and involuntary context switches
CPU displays user and system CPU usage times
IPC displays counts for messages sent and received
MEMORY is not currently implemented
PAGE FAULTS displays counts for major and minor page faults
SOURCE displays the names of functions from the source code, together with the name and line number of the file in which the function occurs
SWAPS displays swap counts
profiling 對每個會話有效,當會話結束后,當前的 profiling 信息就會丟失。
使用案例
mysql show profiles;
+----------+------------+----------------------------+
| Query_ID | Duration | Query |
+----------+------------+----------------------------+
| 1 | 0.00060275 | select * from customers |
| 2 | 0.00222450 | show tables |
| 3 | 0.00567425 | select * from offices |
| 4 | 0.00052050 | show tables |
| 5 | 0.01123300 | select * from payments |
| 6 | 0.00111675 | show tables |
| 7 | 0.02049625 | select * from productlines |
+----------+------------+----------------------------+
在排查 SQL 執行情況,或者是哪條 SQL 執行非常慢,慢在哪里;profile 都是非常的輔助工具。
顯示一條 SQL 的具體花銷在哪里
mysql show profile for query 7;
+----------------------+----------+
| Status | Duration |
+----------------------+----------+
| starting | 0.000043 |
| checking permissions | 0.000005 |
| Opening tables | 0.014552 |
| init | 0.000025 |
| System lock | 0.000009 |
| optimizing | 0.000004 |
| statistics | 0.000011 |
| preparing | 0.000010 |
| executing | 0.000003 |
| Sending data | 0.005653 |
| end | 0.000010 |
| query end | 0.000009 |
| closing tables | 0.000020 |
| freeing items | 0.000121 |
| cleaning up | 0.000023 |
+----------------------+----------+
關于 profile 怎么在 mysql 中使用就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
向 AI 問一下細節