2012-07-31 223 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