轉載-查看mysql語句運行時間的2種方法

網站運行很慢的時候,我就特別起知道爲何這麼慢,因此我查啊查,數據庫絕對是很重要的一部分,裏面運行的sql是絕對不能放過的。平時作項目的時候,我也會注意sql語句的書寫,寫出一些高效的sql來,因此我會常常測試本身寫的sql語句。我把我知道的二個方法,總結一下發出來。mysql

一,show profiles 之類的語句來查看sql

1,查一下profile是否是打開了,默認是不打開的。數據庫

mysql> show profiles;
Empty set (0.02 sec)
mysql> show variables like "%pro%";
+---------------------------+-------+
| Variable_name             | Value |
+---------------------------+-------+
| profiling                 | OFF   |
| profiling_history_size    | 15    |
| protocol_version          | 10    |
| slave_compressed_protocol | OFF   |
+---------------------------+-------+
4 rows in set (0.00 sec)

我查看一下profiles裏面沒有東西,因此公司的電腦裏面profile是沒有打開的,我查看了一下mysql變量,果真是OFF的。測試

2,開啓profile,而後測試網站

開啓profile.net

mysql> set profiling=1;
Query OK, 0 rows affected (0.00 sec)

測試以下:命令行

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| aa             |
| bb             |
| comment        |
| string_test    |
| user           |
+----------------+
5 rows in set (0.00 sec)
mysql> select * from aa;
+----+------+------------+------+
| id | name | nname      | sex  |
+----+------+------------+------+
|  2 | tank | bbbb,4bbbb | NULL |
|  3 | zhang| 3,c,u      | NULL |
+----+------+------------+------+
2 rows in set (0.00 sec)
mysql> update aa set name='d';
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2  Changed: 2  Warnings: 0
mysql> delete from bb;
Query OK, 2 rows affected (0.00 sec)
mysql> show profiles;
+----------+------------+------------------------+
| Query_ID | Duration   | Query                  |
+----------+------------+------------------------+
|        1 | 0.00054775 | show tables            |
|        2 | 0.00022400 | select * from aa       |
|        3 | 0.00026275 | update aa set name='d' |
|        4 | 0.00043000 | delete from bb         |
+----------+------------+------------------------+
4 rows in set (0.00 sec)
mysql> show profile;
+----------------------+-----------+
| Status               | Duration  |
+----------------------+-----------+
| (initialization)     | 0.0000247 |
| checking permissions | 0.0000077 |
| Opening tables       | 0.0000099 |
| System lock          | 0.000004  |
| Table lock           | 0.000005  |
| init                 | 0.0003057 |
| query end            | 0.0000062 |
| freeing items        | 0.000057  |
| closing tables       | 0.000008  |
| logging slow query   | 0.0000015 |
+----------------------+-----------+
10 rows in set (0.00 sec)
mysql> show profile for query 1;
+----------------------+-----------+
| Status               | Duration  |
+----------------------+-----------+
| (initialization)     | 0.000028  |
| checking permissions | 0.000007  |
| Opening tables       | 0.0000939 |
| System lock          | 0.0000017 |
| Table lock           | 0.0000055 |
| init                 | 0.000009  |
| optimizing           | 0.0000027 |
| statistics           | 0.0000085 |
| preparing            | 0.0000065 |
| executing            | 0.000004  |
| checking permissions | 0.000258  |
| Sending data         | 0.000049  |
| end                  | 0.0000037 |
| query end            | 0.0000027 |
| freeing items        | 0.0000307 |
| closing tables       | 0.0000032 |
| removing tmp table   | 0.0000275 |
| closing tables       | 0.0000037 |
| logging slow query   | 0.000002  |
+----------------------+-----------+
19 rows in set (0.00 sec)
mysql> show profile for query 3;
+----------------------+-----------+
| Status               | Duration  |
+----------------------+-----------+
| (initialization)     | 0.0000475 |
| checking permissions | 0.0000077 |
| Opening tables       | 0.000026  |
| System lock          | 0.0000042 |
| Table lock           | 0.0000045 |
| init                 | 0.0000205 |
| Updating             | 0.0000787 |
| end                  | 0.0000567 |
| query end            | 0.000004  |
| freeing items        | 0.0000067 |
| closing tables       | 0.000004  |
| logging slow query   | 0.000002  |
+----------------------+-----------+
12 rows in set (0.00 sec)

二,timestampdiff來查看測試時間code

mysql> set @d=now();
Query OK, 0 rows affected (0.00 sec)
mysql> select * from comment;
+------+-----+------+------------+---------------------+
| c_id | mid | name | content    | datetime            |
+------+-----+------+------------+---------------------+
|    1 |   1 | ??   | 2222222211 | 2010-05-12 00:00:00 |
|    2 |   1 | ??   | ????(??)   | 2010-05-13 00:00:00 |
|    3 |   2 | tank | ??????     | 0000-00-00 00:00:00 |
+------+-----+------+------------+---------------------+
3 rows in set (0.00 sec)
mysql> select timestampdiff(second,@d,now());
+--------------------------------+
| timestampdiff(second,@d,now()) |
+--------------------------------+
|                              0 |
+--------------------------------+
1 row in set (0.00 sec)

這種方法有一點要注意,就是三條sql語句要儘可能連一塊兒執行,否則偏差太大,根本不許rem

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

若是是用命令行來執行的話,有一點要注意,就是在select timestampdiff(second,@d ,now());後面,必定要多copy一個空行,否則最後一個sql要你本身按回車執行,這樣就不許了。get

其實我以爲吧,真正要咱們關心的是,那些查詢慢的sql,由於真正影響速度的是他們,關於慢查詢的東西,有空寫一下。

相關文章
相關標籤/搜索