Page 1 of 1

Server Load Time and Database Query

Posted: Wed Jun 06, 2007 10:33 am
by richbate
I'm just wondering, i know its a basic one, but whats the code or process of calculating howmany SQL Querys have been done to generate the page?

Example, some sites have the following at the footer of a page:

[ Load Time: 0.0004 Seconds ] - [ Queries Made: 12 ]

I think it would be useful on the early stages of my site so i can see witch pages make the most strain on the server and such.

Cheers,
Rich[/syntax]

Posted: Wed Jun 06, 2007 11:07 am
by d3ad1ysp0rk
If I recall correctly, most sites do the query count by making use of their custom db functions. ex, db_query.

Code: Select all

function db_query($sql){
  $GLOBALS['db_queries']++;
  return mysql_query($sql);
}
Or, at least, that's how I'd do it.

Posted: Wed Jun 06, 2007 1:08 pm
by RobertGonzalez
I do it in the query method of my database class (because I am not big on using globals).

For timing, have a look at microtime(). For counts, just set a counter in the query function/method you are using and increment it as needed. You can also have a look at the static keyword for PHP 4, which will hold its value in the function scope and allow incrementing of that value without the need for a global variable.

Posted: Wed Jun 06, 2007 2:16 pm
by AKA Panama Jack
If you are using the ADOdb Lite database abstraction layer you can include these at the end of your php code.

Code: Select all

echo "Queries: " . $db->query_count . " - Time: " . sprintf("%01.4f", $db->query_time_total) . "<br>";
ADOdb Lite records all queries and execution times. There is even a debug console that will record how fast every query executed.

http://adodblite.sourceforge.net/debugconsole.php

Image

It can even keep track of any failed queries. :)