mysql:show profile分析一個SQL執行慢的具體原因

在做數據庫優化的過程中可以通過explain來查看查詢有沒有走索引,但是

mysql從5.0.37版本開始增加了對show profiles和show profile 語句的支持,通過

have_profiling參數,能夠看到當前mysql是否支持profile;

mysql> select @@have_profiling;

+-------------------------------+

|@@have_profiling |

+-------------------------------+

| YES |

+-------------------------------+

1 row in set (0.00 sec)

默認profiling是關閉的,可以通過set語句在session級別開啟profiling:

mysql> select @@profiling;

+-------------------------------+

| @@profiling |

+-------------------------------+

| 0 |

+------------------------------+

1 row in set (0.00 sec)

mysql> set profiling=1;

query ok ,0 rows affected(0.00 sec)

通過 profile,我們能夠更清楚地瞭解sql執行的過程,例如,我們知道myisam表有表元數據的緩

存(例如行數,即count(*)值),那麼對一個myisam表的count(*)是不需要消耗太多資源的,而對於

innodb來說,就沒有這種元數據緩存,count(*) 執行地較慢,下面做個實驗驗證一下。

首先,在一個innodb引擎的付款表payment上,執行一個count(*)查詢:

mysql> select count(*) from payment;

+------------------------------+

| count(*) |

+------------------------------+

| 16049 |

+------------------------------+

1 row in set (0.00 sec)

執行完畢後,通過show profiles語句,看到當前sql的queryID為4:

mysql>show profiles;

mysql:show profile分析一個SQL執行慢的具體原因

通過 show profile for query語句能夠看到執行過程中線程的每個狀態和消耗的時間:

mysql:show profile分析一個SQL執行慢的具體原因

show profile cpu for query 4;


分享到:


相關文章: