Server Load Time and Database Query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
richbate
Forum Newbie
Posts: 1
Joined: Wed Jun 06, 2007 10:27 am

Server Load Time and Database Query

Post 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]
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post 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. :)
Post Reply