SHOW PROFILE的MySQL优化原理是什么?

摘要:背景 最近碰到一个 case,通过可传输表空间的方式导入一个 4GB 大小的表,耗时 13 分钟。 通过PROFILE定位,发现大部分耗时竟然是在System lock阶段。 mysql> s
背景 最近碰到一个 case,通过可传输表空间的方式导入一个 4GB 大小的表,耗时 13 分钟。 通过PROFILE定位,发现大部分耗时竟然是在System lock阶段。 mysql>setprofiling=1; QueryOK,0rowsaffected,1warning(0.00sec) mysql>altertablesbtest2importtablespace; QueryOK,0rowsaffected(13min8.99sec) mysql>showprofileforquery1; +--------------------------------+------------+ |Status|Duration| +--------------------------------+------------+ |starting|0.000119| |Executinghookontransaction|0.000004| |starting|0.000055| |checkingpermissions|0.000010| |discard_or_import_tablespace|0.000007| |Openingtables|0.000156| |Systemlock|788.966338| |end|0.007391| |waitingforhandlercommit|0.000041| |waitingforhandlercommit|0.011179| |queryend|0.000022| |closingtables|0.000019| |waitingforhandlercommit|0.000031| |freeingitems|0.000035| |cleaningup|0.000043| +--------------------------------+------------+ 15rowsinset,1warning(0.03sec) 不仅如此,SQL 在执行的过程中,show processlist中的状态显示的也是System lock。 mysql>showprocesslist; +----+-----------------+-----------+--------+---------+------+------------------------+---------------------------------------+ |Id|User|Host|db|Command|Time|State|Info| +----+-----------------+-----------+--------+---------+------+------------------------+---------------------------------------+ |5|event_scheduler|localhost|NULL|Daemon|818|Waitingonemptyqueue|NULL| |10|root|localhost|sbtest|Query|648|Systemlock|altertablesbtest2importtablespace| |14|root|localhost|NULL|Query|0|init|showprocesslist| +----+-----------------+-----------+--------+---------+------+------------------------+---------------------------------------+ 3rowsinset,1warning(0.00sec) 这个状态其实有很大的误导性。 接下来我们从SHOW PROFILE的基本用法出发,从源码角度分析它的实现原理。 最后在分析的基础上,看看 case 中的表空间导入操作为什么大部分耗时是在System lock阶段。 SHOW PROFILE 的基本用法 下面通过一个示例来看看SHOW PROFILE的用法。
阅读全文