如何估算SQL查询的时间?

21

我正在尝试获取以下查询可能需要多长时间的粗略(数量级)估计:

mysql> EXPLAIN SELECT t1.col1, t1_col4 FROM t1 LEFT JOIN t2 ON t1.col1=t2.col1 WHERE col2=0 AND col3 IS NULL;
+----+-------------+--------------------+------+---------------+------------+---------+-----------------------------+---------+--------------------------+
| id | select_type | table              | type | possible_keys | key        | key_len | ref                         | rows    | Extra                    |
+----+-------------+--------------------+------+---------------+------------+---------+-----------------------------+---------+--------------------------+
|  1 | SIMPLE      | t1                 | ref  | foobar        | foobar     | 4       | const                       | 9715129 |                          | 
|  1 | SIMPLE      | t2                 | ref  | col1          | col1       | 4       | db2.t1.col1                 |   42318 | Using where; Using index | 
+----+-------------+--------------------+------+---------------+------------+---------+-----------------------------+---------+--------------------------+
2 rows in set (0.00 sec)

mysql> 

1
为什么不直接执行并计时呢?我怀疑我们无法给出准确的估计。 - Chris Laplante
1个回答

34

使用SHOW PROFILES语法,可以完成这个操作。 当打开MySQL会话时,可以将变量"profiling"设置为1或ON。

mysql> SET profiling = 1;

所以发送到服务器的所有语句都将被分析并存储在历史记录中,通过输入以下命令稍后显示:

mysql> SHOW PROFILES;

参见 MySQL 手册:

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

mysql> DROP TABLE IF EXISTS t1;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> CREATE TABLE T1 (id INT);
Query OK, 0 rows affected (0.01 sec)

mysql> SHOW PROFILES;
+----------+----------+--------------------------+
| Query_ID | Duration | Query                    |
+----------+----------+--------------------------+
|        0 | 0.000088 | SET PROFILING = 1        |
|        1 | 0.000136 | DROP TABLE IF EXISTS t1  |
|        2 | 0.011947 | CREATE TABLE t1 (id INT) |
+----------+----------+--------------------------+
3 rows in set (0.00 sec)

mysql> SHOW PROFILE;
+----------------------+----------+
| Status               | Duration |
+----------------------+----------+
| checking permissions | 0.000040 |
| creating table       | 0.000056 |
| After create         | 0.011363 |
| query end            | 0.000375 |
| freeing items        | 0.000089 |
| logging slow query   | 0.000019 |
| cleaning up          | 0.000005 |
+----------------------+----------+
7 rows in set (0.00 sec)

mysql> SHOW PROFILE FOR QUERY 1;
+--------------------+----------+
| Status             | Duration |
+--------------------+----------+
| query end          | 0.000107 |
| freeing items      | 0.000008 |
| logging slow query | 0.000015 |
| cleaning up        | 0.000006 |
+--------------------+----------+
4 rows in set (0.00 sec)

mysql> SHOW PROFILE CPU FOR QUERY 2;
+----------------------+----------+----------+------------+
| Status               | Duration | CPU_user | CPU_system |
+----------------------+----------+----------+------------+
| checking permissions | 0.000040 | 0.000038 |   0.000002 |
| creating table       | 0.000056 | 0.000028 |   0.000028 |
| After create         | 0.011363 | 0.000217 |   0.001571 |
| query end            | 0.000375 | 0.000013 |   0.000028 |
| freeing items        | 0.000089 | 0.000010 |   0.000014 |
| logging slow query   | 0.000019 | 0.000009 |   0.000010 |
| cleaning up          | 0.000005 | 0.000003 |   0.000002 |
+----------------------+----------+----------+------------+


参考资料(更新于:2014-09-04):
- SHOW PROFILE 语法
- INFORMATION_SCHEMA PROFILING 表
- 如何使用 MySQL 查询分析Digital Ocean 最近发表了一篇关于此问题的好文章。


@Problemania,你也可以看一下这个: MySQL服务器日志 - felipsmartins

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接