查看mysql语句运行时间

时间:2021-10-20 09:49:59

show profiles 之类的语句来查看

mysql> show profiles;
Empty set mysql> show variables like "%pro%";
+---------------------------+-------+
| Variable_name | Value |
+---------------------------+-------+
| have_profiling | YES |
| profiling | OFF |
| profiling_history_size | |
| protocol_version | |
| proxy_user | |
| slave_compressed_protocol | OFF |
| stored_program_cache | |
+---------------------------+-------+
rows in set

profile是没有打开的

开启profile

mysql> set profiling=;
Query OK, rows affected mysql> show variables like "%pro%";
+---------------------------+-------+
| Variable_name | Value |
+---------------------------+-------+
| have_profiling | YES |
| profiling | ON |
| profiling_history_size | |
| protocol_version | |
| proxy_user | |
| slave_compressed_protocol | OFF |
| stored_program_cache | |
+---------------------------+-------+
rows in set

测试

执行几条sql语句

mysql> show profiles;
+----------+------------+-----------------------------------------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+-----------------------------------------------------------------------+
| | 0.0020095 | show variables like "%pro%" |
| | 0.0012765 | select * from shop where shop_tradearea_id= and shop_fristtype= |
| | 0.004761 | show tables |
| | 0.0360445 | show profile for query2 |
| | 0.00023675 | set profiling= |
| | 0.36132975 | select * from account where type= |
+----------+------------+-----------------------------------------------------------------------+
rows in set mysql> show profile for query ;
+----------------------+----------+
| Status | Duration |
+----------------------+----------+
| starting | 0.000107 |
| checking permissions | 1.3E-5 |
| Opening tables | 5E- |
| System lock | 2.5E-5 |
| init | 0.000161 |
| optimizing | 2.5E-5 |
| statistics | 2.8E-5 |
| preparing | 3E- |
| executing | 4E- |
| Sending data | 0.000536 |
| end | 1E- |
| query end | 4E- |
| closing tables | 1.4E-5 |
| freeing items | 0.000242 |
| logging slow query | 6E- |
| cleaning up | 2.2E-5 |
+----------------------+----------+

timestampdiff来查看测试时间

mysql> set @d=now();
mysql> select * from comment;
mysql> select timestampdiff(second,@d,now());