如何估算SQL查询时间? [英] How to estimate SQL query timing?

查看:133
本文介绍了如何估算SQL查询时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试大致估算(以下量级)以下查询可能要花费的时间:

I'm trying to get an rough (order-of-magnitude) estimate of how long time the following query could take:

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> 

推荐答案

使用

This can be done when using SHOW PROFILES syntax. When you open a MySQL session, you could set the variable "profiling" to 1 or ON.

mysql> SET profiling = 1;

因此,发送到服务器的所有语句将被概要分析并存储在历史记录中,并在以后通过键入以下命令显示出来:

So all the statements sent to the server will be profiled and stored in a historical and shown later by typing the command:

mysql> SHOW PROFILES;

请参阅MySQL手册:

See, from MySQL manual:

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查询分析(数字海洋最近发表了一篇有关此问题的好文章.)


References (updated at: 2014-09-04):
- SHOW PROFILE Syntax
- The INFORMATION_SCHEMA PROFILING Table
- How To Use MySQL Query Profiling (The Digital Ocean recently published a great article concerning this issue.)

这篇关于如何估算SQL查询时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆