Benchmark SQL Queries

Hi all,

Been looking around a while for this so sorry if this has been posted but don’t seem to be able to find it.

I am looking for a couple of quick functions that I can stick in a class for timing the execution of SQL queries. Part of what I am looking for is where site have the following:

PHP: 0.002 seconds | SQL: 0.050 seconds | 8 SQL Queries

Got everything except SQL, now I’m thinking in some way I need to start and stop a timer at the end of each query but not sure how to add it up without creating a new variable for each query, think it’s something to do with the += assignment but I can’t get it working!

Any help is greatly appriciated.

Excellent, exactly what I am looking for, thank you.


$query_time = 0;
$query_count = 0;

//Run a query
$t0 = microtime(true);
$sql = "SELECT fields FROM table WHERE condition";
//execute query
$query_time += microtime(true)-$t0;
$query_count++;

//Report
$query_time = round($query_time*1000, 3);
echo "<p>SQL: $query_time seconds | $query_count SQL Queries</p>";