首先開啓profilingsql
set profiling=1; root@localhost>select * from nicer_but_slower_film_list; root@localhost>show profiles; +----------+------------+-------------------------------------------+ | Query_ID | Duration | Query | +----------+------------+-------------------------------------------+ | 1 | 0.00029875 | show warnings | | 2 | 0.00135925 | show variables like '%profi%' | | 3 | 0.19135775 | select * from nicer_but_slower_film_list | +----------+------------+-------------------------------------------+
對剛纔的查詢語句進行性能分析性能
root@localhost>show profile for query 3; +----------------------+----------+ | Status | Duration | +----------------------+----------+ | starting | 0.000069 | | checking permissions | 0.000016 | | Opening tables | 0.000384 | | checking permissions | 0.000063 | | checking permissions | 0.000015 | | checking permissions | 0.000012 | | checking permissions | 0.000011 | | checking permissions | 0.000116 | | init | 0.000033 | | System lock | 0.000057 | | optimizing | 0.000013 | | optimizing | 0.000415 | | statistics | 0.023471 | | preparing | 0.000168 | | Creating tmp table | 0.000301 | | Sorting result | 0.000021 | | statistics | 0.000019 | | preparing | 0.000018 | | executing | 0.000622 | | Sending data | 0.000089 | | executing | 0.000013 | | Sending data | 0.074418 | | Creating sort index | 0.080036 | | removing tmp table | 0.000624 | | Creating sort index | 0.009102 | | end | 0.000016 | | query end | 0.000013 | | closing tables | 0.000008 | | removing tmp table | 0.000296 | | closing tables | 0.000027 | | freeing items | 0.000062 | | removing tmp table | 0.000011 | | freeing items | 0.000794 | | cleaning up | 0.000031 | +----------------------+----------+ 34 rows in set, 1 warning (0.00 sec)
結果是無序的,不容易閱讀。而且show profiling不支持order by排序,經過查看information_schema方案能夠獲取有序結果。code
root@localhost>set @query_id=3; Query OK, 0 rows affected (0.00 sec) root@localhost>select state,sum(duration) as total_R, -> round( -> 100*sum(duration)/ -> (select sum(duration) from information_schema.profiling where query_id=@query_id -> ),2) as pct_R, -> count(*) as Calls, -> sum(duration)/count(*) as "R/Call" -> from information_schema.profiling -> where query_id=@query_id -> group by state -> order by total_R desc; +----------------------+----------+-------+-------+--------------+ | state | total_R | pct_R | Calls | R/Call | +----------------------+----------+-------+-------+--------------+ | Creating sort index | 0.089138 | 46.58 | 2 | 0.0445690000 | | Sending data | 0.074507 | 38.93 | 2 | 0.0372535000 | | statistics | 0.023490 | 12.28 | 2 | 0.0117450000 | | removing tmp table | 0.000931 | 0.49 | 3 | 0.0003103333 | | freeing items | 0.000856 | 0.45 | 2 | 0.0004280000 | | executing | 0.000635 | 0.33 | 2 | 0.0003175000 | | optimizing | 0.000428 | 0.22 | 2 | 0.0002140000 | | Opening tables | 0.000384 | 0.20 | 1 | 0.0003840000 | | Creating tmp table | 0.000301 | 0.16 | 1 | 0.0003010000 | | checking permissions | 0.000233 | 0.12 | 6 | 0.0000388333 | | preparing | 0.000186 | 0.10 | 2 | 0.0000930000 | | starting | 0.000069 | 0.04 | 1 | 0.0000690000 | | System lock | 0.000057 | 0.03 | 1 | 0.0000570000 | | closing tables | 0.000035 | 0.02 | 2 | 0.0000175000 | | init | 0.000033 | 0.02 | 1 | 0.0000330000 | | cleaning up | 0.000031 | 0.02 | 1 | 0.0000310000 | | Sorting result | 0.000021 | 0.01 | 1 | 0.0000210000 | | end | 0.000016 | 0.01 | 1 | 0.0000160000 | | query end | 0.000013 | 0.01 | 1 | 0.0000130000 | +----------------------+----------+-------+-------+--------------+ 19 rows in set (0.01 sec)