Jump to content

How many queries on page load??


karimali831

Recommended Posts

Hi!

 

I am looking for a script that counts how many queries is executed on page load?

I hadn't realised one of my scripts was doing hundreds of queries simultaneously

and never would of thought this would be the cause of high load.

 

As I'm developing stuff in PHP, I want to ensure minimum queries are executed

especially because I am doing this in a procedural manner and not OOP.

 

Thanks in advance.

Link to comment
Share on other sites

procedural manner and not OOP

 

^^^ That has nothing directly to do with how many queries you have written your code to execute on a page.

 

I'm going to guess that you have written your code to perform query(ies) inside of loop(s).

 

Counting your queries would involve using some sort of database abstraction layer, which could be as simply as making a function/class that executes the supplied query that you use to replace your existing query calls with. The code in the replacement query function/class would keep a count of the number of queries and possibly keep other information such as type of query and line number where the query was called from.

Link to comment
Share on other sites

I did some research and there is nothing under my search, I thought this sort of thing a lot of people would want to get their hands on. I am using many while loops from different files with many include() 's and calling many functions from different files that also has while loops inside.

Link to comment
Share on other sites

There's no function for getting the number of queries executed in one session, so your only solution is to replace all mysql_query calls with a custom function that calls it and increases a counter. Or if you're using a custom mysql class that handles all your MySQL, you can add it to that. Sorry for repeating what PFMaBiSmAd basically said, but that is the answer.

Link to comment
Share on other sites

A) You haven't supplied us with any specific information about even which database you are using, so a little hard to help,

 

B) If all you are using are mysql_query() statements, the following should get you started. It works for the few test cases I tried.

 

You should do this on an off-line back-up copy of your code and since you are likely going to be modifying and testing the code to eliminate the excess queries, you would be doing this on a development system anyway -

 

Add (include) this somewhere near the start of the main page you want to check -

 

class db_count{
public static $count = 0;
public static $stat = array();

public static function query($query,$con=null){

	self::$count++; // count this query

	// produce backtrace information
	$bt = debug_backtrace();
	$function = array();
	$file = array();
	$line = array();
	foreach($bt as $key => $arr){
		if($key >= 1){
			$function[] = $arr['function'];
			$file[] = basename($arr['file']);
			$line[] = $arr['line'];
		}
	}
	$function = implode('<--',$function);
	$file = implode('<--',$file);
	$line = implode('<--',$line);
	self::$stat[] = array('query'=>$query,'func'=>$function,'line'=>$line,'file'=>$file);

	// execute the query
	if(is_null($con)){
		$result = mysql_query($query);
	} else {
		$result = mysql_query($query,$con);		
	}
		return $result;
}
}

// functions are global so you can search and replace mysql_query( with my_query(
function my_query($query,$con=null){
return db_count::query($query,$con); // call the query method of the class
}

 

Search and replace all the occurrences of mysql_query( with my_query(

 

Add (include) the following near the end of the main page you want to check -

 

// display count and query information -
echo db_count::$count . " query(ies) on this page!<br />";
echo '<pre>',print_r(db_count::$stat,true),'</pre>';

 

When you are done, remove the two pieces of code and search and replace all occurrences of  my_query( with mysql_query(

Link to comment
Share on other sites

Thanks for your replies.

 

Well I am using a custom function to call mysql_query() which I recall safe_query()

 

// -- MYSQL QUERY FUNCTION -- //
$_mysql_querys = array();
function safe_query($query="") {
global $_mysql_querys;
if(stristr(str_replace(' ', '', $query), "unionselect")===FALSE AND stristr(str_replace(' ', '', $query), "union(select")===FALSE){
	$_mysql_querys[] = $query;
	if(empty($query)) return false;
	if(DEBUG == "OFF") $result = mysql_query($query) or die('Query failed!');
	else {
		$result = mysql_query($query) or die('Query failed: '
		.'<li>errorno='.mysql_errno()
		.'<li>error='.mysql_error()
		.'<li>query='.$query);
	}
	return $result;
}
else die();
}

 

and for every query I use safe_query()

 

does this help ?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.