2012-07-31 225 views

回答

10

试试这个简单的例子

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) 
2

您可以编写子查询中查询与COUNT这样的伎俩是:

SELECT COUNT(1) 
FROM (SELECT * FROM your_table WHERE ...) a 

它可以查询略有放慢,因为它做COUNT还,但我认为它可以忽略不计。

用于测量查询的性能,你可以在MySQL开启PROFILES为:

SET profiling = 1; 

PROFILES欲了解更多详情,请参见here

+0

它说: “ERROR 1248(42000):每一个派生表必须有它自己的别名”? – TIMEX 2012-07-31 08:06:46

+0

给派生表赋予任何别名,因为我在最后给出了'a' – Omesh 2012-07-31 08:08:10

0
$starttime = microtime(true); 

//Do your query and stuff here 

$endtime = microtime(true); 
$duration = $endtime - $starttime; //calculates total time taken