Jump to content

Time Previous MySQL Query Took To Execute


JustinK101

Recommended Posts

this really isn't but surrond your query with this:

<?php
// Function to calculate script execution time.
function microtime_float ()
{ 
    list ($msec, $sec) = explode(' ', microtime()); 
    $microtime = (float)$msec + (float)$sec;
    return $microtime; 
}

// Get starting time.
$start = microtime_float();

/*
Query Here
*/

$end = microtime_float(); 

// Print results.
echo '<br/><br/>Search Execution Time: ' . round($end - $start, 3) . ' seconds<br/><br/>';
?>

Link to comment
Share on other sites

Ok I see, but I dont want to insert this code into my project which is well over 50 php pages, and find all the queries. I was hoping there was just a way to get the time the previous query took? That way, I can just stick the value into my footer include file.

 

I know phpMyAdmin displays the time the previous query took, do they do it this way?

Link to comment
Share on other sites

The short answer is no, you can't override functions (well you can, but I wouldn't recommend it).

 

What you can do is make your own "wrapper" function:

 

function my_mysql_query($query) {
  # Do timing stuff
  $res = mysql_query($query);
  # Do timing stuff
  return $res;
}

 

And then replace every call to mysql_query() with my_mysql_query().  You can even do fancy stuff like store time for each individual query in an array, and then dump that array if a debugging flag is set.  It's great for seeing which queries are eating up your time.

 

It's called a wrapper function because it wraps around the real function, doing a bit of extra stuff.

Link to comment
Share on other sites

btherl,

 

Ok that makes sense.

 

I think I just want to do total queries executed per page instead of time, makes more sense, since the time would just be the last executed query. How would I keep track of that. For example I have a header.php file which calls a query, then in the page there might be three queries, then in the footer there is another query. I would want 4 retutrned in this case.

 

Thanks.

Link to comment
Share on other sites

Ok, here is what I  have to track the number of queries per page:

 

//Tracks The Number Of Queries Per Page.
global $numb_queries;
$numb_queries = 0;

function smart_mysql_query($sql) {
$numb_queries++;
echo "<script>alert('" . $numb_queries . "');</script>";
return (mysql_query($sql));	
}

 

The echo and JS alert is just to help me to debug this thing. For some reason the alert keeps on popuping up 1, no matter how many queries it runs, then in the page the value of numb_queries is 0.

 

Any ideas? I don't get what is gonig on.

Link to comment
Share on other sites

OHh I got it. Duhhhh!

 

//Tracks The Number Of Queries Per Page.
$numb_queries = 0;

//Mysql_query() Wrapper Function.
function smart_mysql_query($sql) {
global $numb_queries;
$numb_queries++;
return (mysql_query($sql));	
}

 

Hopefully this helps somebody else, this seems to work well, simply echo out the value of $numb_queries to get the number of queries used on a page.

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.