mysql之profile分析(簡單篇)

2021-10-09 13:42:22 字數 1787 閱讀 3917

需優化的4種情況

最近專案上要求弄一下壓測,看下各個服務的瓶頸,然後進行上線前的最後衝刺,於是又拿起了2年前看的mysql優化一書,挑燈夜戰,寫ppt的同時想著順道寫篇部落格~~ 官網資料

profile開關預設是關閉的,所以倒騰profile之前,先得檢視profile是否開啟。

檢視profile是否開啟

select @@profiling

//0關閉,1開啟

或者 show variables like

'profiling'

;//off關閉,1開啟

若查詢出結果為0,則需要手動開啟

開啟profile

set @@profiling=1

或者set profiling=

on;

show profiles;
顯示最近的15條sql執行語句,可以看到:

query_id

duration

query

10.006

select * from table1

22.809

select t1.id, t2.id from table1 t1 left join table2 on table2.uid = ti.id

show profile … for query id 可指定資源型別查詢

資源型別

說明all

所有的開銷資訊

block io

塊io相關開銷

context swtichs

上下文切換相關開銷

cpucpu 相關開銷

ipc傳送和接收相關開銷

memory

記憶體相關開銷

page faults

頁面錯誤相關開銷資訊

source

和source_function ,source_file,source_line 相關的開銷資訊

swaps

交換次數相關的開銷資訊

引數名稱

說明status

sql 語句執行的狀態

duration

sql 執行過程中每乙個步驟的耗時

cpu_user

當前使用者占有的cpu

cpu_system

系統占有的cpu

block_ops_in

i/o 輸入

block_ops_out

i/o 輸出

show profile for query 1
執行後結果類似如下表,可以看到執行過程中的每個步驟和所消耗時間。

status

duration

starting

0.000272

executing hook on transaction

0.000012

executing

2.667285……

query end

0.000012……

cleaning up

0.000034

若執行show profile for query id出現如下4種情況,則需要對sql進行優化!

creating tmp table

copying to tmp table on disk

locked

mysql優化之profile查詢分析

通過慢日誌查詢可以知道哪些sql語句執行效率低下,通過explain我們可以得知sql語句的具體執 況,索引使用等,還可以結合show命令檢視執行狀態。如果覺得explain的資訊不夠詳細,可以同通過profiling命令得到更準確的sql執行消耗系統資源的資訊。這裡還需要注意一點就是,需要安裝pr...

python 效能分析profile

如果希望對程式進行優化,那麼效能分析是必不可少的。標準庫中包含了乙個叫profile的模組,使用起來非常簡單 importprofile,my math profile.run my math.square 100 只需要執行該模組的run方法,需要注意的是引數為字串。即可得到如下結果 4 func...

Python效能分析工具Profile

優化的前提是需要了解效能瓶頸在什麼地方,程式執行的主要時間是消耗在 對於比較複雜的 可以借助一些工具來定位,python 內建了豐富的效能分析工具,如 profile,cprofile 與 hotshot 等。其中 profiler 是 python 自帶的一組程式,能夠描述程式執行時候的效能,並提...