tags:

views:

105

answers:

4

I have a select and query like below...

    $sql = "SELECT * 
        FROM notifications 
        WHERE to_id='".$userid."' 
            AND (alert_read != '1' OR user_read != '1') 
        ORDER BY alert_time DESC";
    $result = mysql_query($sql);

how do I test how long the query took to run?

A: 

Might want to look at this discussion:

http://stackoverflow.com/questions/1191067/how-to-test-mysql-query-speed-with-less-inconsistencies

AJ
I have already seen that but it didn't really help. I'm new to speed testing and I didn't quite understand it. I tried using microtime, but it took the speed of PHP and not the query.
Chris
+1  A: 

If you run the query from the mysql console you, the speed will be displayed right after they query is ran.

Rob
I didn't even think of this, thanks!
Chris
Yaa! K.I.S.S. :)
AJ
Yeah I considered a more complex answer, but honestly if you are interested in just basic tuning, just use the feature already built into the mysql client :)
Rob
A: 

use php function microtime() before and after mysql_query, and then compare results.

$time1 = (int) microtime();
$result = mysql_query($sql);
$time2 = (int) microtime();

echo ($time2 - $time1);

(if you use microtime(true) - which returns float number, you will get less acurate number)

praksant
you wrote in comment to another post, that microtime measures speed of PHP. I think for simple queries which runs shorter than 1 milisecond it might be useless, but also i don't see a reason to measure fast queries like this. I think for queries slower than 1milisecond (and problematic queries are slower than that) it's accurate enough. Only if you run db server on remote machine, than it wouldn't be that accurate.
praksant
+2  A: 
VAC-Prabhu