Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count number of MySQL queries executed on page

Tags:

php

mysql

how would I count the number of sql queries executed on one page load?

I have a similar script to time taken for page to be generated, but not for how many queries have been executed.

You know what I mean, such as on SMF forums, in the footer, they have:

Page created in 0.136 seconds with 7 queries.

in the footer?

Replacing all of the mysql_query(ies) isn't really an option, there are way too many mysql_queries to replace, although I could spent a day doing it if needs be.

Thanks

like image 437
bear Avatar asked Aug 17 '09 22:08

bear


3 Answers

After Quassnoi comment i put this in the start of script:

$res = mysql_query("SHOW SESSION STATUS LIKE 'Questions'");
$row = mysql_fetch_array($res, MYSQL_ASSOC);
define("START_QUERIES",$row['Value']);

and this:

$res = mysql_query("SHOW SESSION STATUS LIKE 'Questions'");
$row = mysql_fetch_array($res, MYSQL_ASSOC);
define("STOP_QUERIES",$row['Value']);

you can see total num of queries with:

echo "No of queries: ".(STOP_QUERIES-START_QUERIES-1);

I don't know why use -1, maybe it counts mysql_select_db statement (or smth) also, but it works pretty good on my localhost

like image 194
vladkras Avatar answered Sep 30 '22 11:09

vladkras


SHOW SESSION STATUS LIKE 'Questions'
like image 31
Quassnoi Avatar answered Sep 30 '22 12:09

Quassnoi


SMF does its query counting by having its own custom query function:

function db_query($db_string, $file, $line)
{
    global $db_cache, $db_count, $db_connection, $db_show_debug, $modSettings;

    // One more query....
    $db_count = !isset($db_count) ? 1 : $db_count + 1;

    ...

The simplest way to achieve what you're trying to do would be to do the same; make a wrapper for mysql_query and use that instead of mysql_query.

like image 26
Sebastian Paaske Tørholm Avatar answered Sep 30 '22 10:09

Sebastian Paaske Tørholm