FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Query Execution Time





macky
Hi guys have you ever wonder how to compute the query time execution in mysql and prompt it to your web page using php like phpmyadmin does?

thanks in advance.. so far my best solution was microtime() but it's not what mysql does so basically im stuck on this one.. Smile
Fire Boar
Easy: extend or encapsulate the PDO and PDOStatement classes and use that for all database access, rather than the outdated mysql_ functions. You basically want your central query function(s) to be wrapped with two microtime calls, storing the difference on the object.
macky
Fire Boar wrote:
Easy: extend or encapsulate the PDO and PDOStatement classes and use that for all database access, rather than the outdated mysql_ functions. You basically want your central query function(s) to be wrapped with two microtime calls, storing the difference on the object.


ah okay.. it almost like this one if i'm not mistaken...

$start = microtime();
// query goes here..
$end = microtime();

//then
$time = $end-$start;

and i think just to have a great look and not such a long figure appearance of numbers, i will just use sprintf()...

thanks a lot.. i know its easy if we will use php functions to get as same effect what the mysql does...
jmraker
Code:
function microtime_float(){
   list($usec, $sec) = explode(' ', microtime());
   return ((float)$usec + (float)$sec);
}

function SQLTimer($sql, $db){
   $startTime = microtime_float();
   $res = @mysql_query($sql, $db);
   echo ' Time: ' .  round(microtime_float() - $startTime, 5);
   return $res;
}
Fire Boar
Mm... I'm not sure I like jmraker's solution much. As proof of concept, it's fine: microtime_float is necessary. But there are two things wrong with using SQLTimer in practice. First, it shouldn't use "echo", it should store the result in a variable, since ideally you'd be able to turn the debug output on or off at will, and you never know at what point the "echo" is going to happen. Worst case, it happens before essential headers are sent. You should preferably use a member variable, but since you're not using OOP here it would have to be either a global variable or a parameter by-reference variable. Second, it shouldn't use mysql_query - that relies too much on MySQL. Better to use the PDO class for all database access.
Related topics
shop mod for phpbb?
very simple PHP code to get search query string
Programming links, info, and tutorials
PHPBB MYSQL Database Question.
Help with mysql query
An Example to mySQL class
Creating a MYSQL "most common" query
a grouping question?
[MySQL] Complicated query
SQL query problem
AJAX tutorial [2nd part now updated]
The Chinese build mobile execution chambers
Upgrading MySQL
PHP script execution time
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.