如何检查MySQL查询需要多长时间?
问题描述:
答
试试这个简单的例子
mysql> set profiling=1;
mysql> select count(*) from comment;
mysql> select count(*) from message;
mysql> show profiles;
+----------+------------+------------------------------+
| Query_ID | Duration | Query |
+----------+------------+------------------------------+
| 1 | 0.00012700 | select count(*) from comment |
| 2 | 0.00014200 | select count(*) from message |
+----------+------------+------------------------------+
2 rows in set (0.00 sec)
答
您可以编写子查询中查询与COUNT
这样的伎俩是:
SELECT COUNT(1)
FROM (SELECT * FROM your_table WHERE ...) a
它可以查询略有放慢,因为它做COUNT
还,但我认为它可以忽略不计。
用于测量查询的性能,你可以在MySQL开启PROFILES
为:
SET profiling = 1;
约PROFILES
欲了解更多详情,请参见here。
答
$starttime = microtime(true);
//Do your query and stuff here
$endtime = microtime(true);
$duration = $endtime - $starttime; //calculates total time taken
它说: “ERROR 1248(42000):每一个派生表必须有它自己的别名”? – TIMEX 2012-07-31 08:06:46
给派生表赋予任何别名,因为我在最后给出了'a' – Omesh 2012-07-31 08:08:10