溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何使用MySQL查看語句運行時間

發布時間:2020-05-29 17:44:59 來源:網絡 閱讀:2224 作者:三月 欄目:編程語言

不知道大家之前對類似如何使用MySQL查看語句運行時間的文章有無了解,今天我在這里給大家再簡單的講講。感興趣的話就一起來看看正文部分吧,相信看完如何使用MySQL查看語句運行時間你一定會有所收獲的。

方法1> 使用 show profiles 進行查看

# 查看 profile 是不是打開的,默認是不打開

mysql> show variables like "%pro%";

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

| Variable_name                            | Value |

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

| check_proxy_users                        | OFF   |

| have_profiling                           | YES   |

| mysql_native_password_proxy_users        | OFF   |

| performance_schema_max_program_instances | -1    |

| profiling                                | OFF   |

| profiling_history_size                   | 15    |

| protocol_version                         | 10    |

| proxy_user                               |       |

| sha256_password_proxy_users              | OFF   |

| slave_compressed_protocol                | OFF   |

| stored_program_cache                     | 256   |

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

11 rows in set (0.01 sec)


# 開啟 profile

mysql> set profiling=1;

Query OK, 0 rows affected, 1 warning (0.00 sec)


# 查詢所有語句的執行時間

mysql> show profiles;

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

| Query_ID | Duration   | Query                                                                                                                                  |

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

|        1 | 0.02008300 | SELECT a.* FROM (SELECT t1.*,(SELECT COUNT(*)+1 FROM a WHERE name=t1.name AND age<t1.age) AS group_id FROM a t1) a WHERE a.group_id<=2 |

|        2 | 0.00034425 | SELECT * FROM a                                                                                                                        |

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

2 rows in set, 1 warning (0.00 sec)


# 即可查看第1個 sql語句 執行的各個操作的耗時詳情

mysql> show profile for query 1;

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

| Status               | Duration |

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

| starting             | 0.000151 |

| checking permissions | 0.000009 |

| checking permissions | 0.000005 |

| Opening tables       | 0.019543 |

| init                 | 0.000080 |

| System lock          | 0.000021 |

| optimizing           | 0.000003 |

| optimizing           | 0.000002 |

| statistics           | 0.000011 |

| preparing            | 0.000022 |

| optimizing           | 0.000009 |

| statistics           | 0.000009 |

| preparing            | 0.000008 |

| statistics           | 0.000007 |

| preparing            | 0.000003 |

| executing            | 0.000007 |

| Sending data         | 0.000006 |

| executing            | 0.000001 |

| Sending data         | 0.000033 |

| executing            | 0.000002 |

| Sending data         | 0.000021 |

| executing            | 0.000003 |

| Sending data         | 0.000008 |

| executing            | 0.000002 |

| Sending data         | 0.000006 |

| executing            | 0.000002 |

| Sending data         | 0.000007 |

| executing            | 0.000002 |

| Sending data         | 0.000007 |

| executing            | 0.000002 |

| Sending data         | 0.000007 |

| executing            | 0.000002 |

| Sending data         | 0.000005 |

| executing            | 0.000002 |

| Sending data         | 0.000014 |

| end                  | 0.000003 |

| query end            | 0.000006 |

| closing tables       | 0.000002 |

| removing tmp table   | 0.000003 |

| closing tables       | 0.000004 |

| freeing items        | 0.000036 |

| cleaning up          | 0.000011 |

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

42 rows in set, 1 warning (0.00 sec)

 

方法2> 使用 timestampdiff 來查看執行時間

mysql> DELIMITER ;;

mysql> SET @d=now();

    -> SELECT * FROM a;

    -> SELECT TIMESTAMPDIFF(second,@d,NOW());;

Query OK, 0 rows affected (0.00 sec)


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

| id | name  | age  | work  |

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

|  1 | name1 |   12 | work1 |

|  2 | name2 |   14 | work2 |

|  3 | name1 |   15 | work3 |

|  4 | name1 |   16 | work4 |

|  5 | name3 |   17 | work5 |

|  6 | name1 |   18 | work6 |

|  7 | name4 |   19 | work7 |

|  8 | name1 |   22 | work8 |

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

8 rows in set (0.00 sec)


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

| timestampdiff(second,@d,now()) |

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

|                              0 |

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

1 row in set (0.00 sec)

mysql> DELIMITER ;

看完如何使用MySQL查看語句運行時間這篇文章,大家覺得怎么樣?如果想要了解更多相關,可以繼續關注我們的行業資訊板塊。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女