首先我們需要看下 profiling 是否開啟,開啟它可以讓 MySQL 收集在 SQL 執(zhí)行時所使用的資源情況,命令如下:
select @@profiling;

image.png
profiling=0 代表關(guān)閉,我們需要把 profiling 打開,即設(shè)置為 1:
mysql> set profiling=1;
然后我們執(zhí)行一個 SQL 查詢(你可以執(zhí)行任何一個 SQL 查詢):
mysql> select * from legends.heros;
查看當前會話所產(chǎn)生的所有 profiles:

image.png
你會發(fā)現(xiàn)我們剛才執(zhí)行了兩次查詢,Query ID 分別為 1 和 2。如果我們想要獲取上一次查詢的執(zhí)行時間,可以使用:
mysql> show profile;

image.png
你也可以查詢指定的 Query ID,比如:
mysql> show profile for query 2;