简体   繁体   English

计算页面上执行的MySQL查询数

[英]Count number of MySQL queries executed on page

how would I count the number of sql queries executed on one page load? 我如何计算在一个页面加载上执行的sql查询的数量?

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: 你知道我的意思,比如在SMF论坛上,在页脚中,他们有:

Page created in 0.136 seconds with 7 queries. 页面在0.136秒内创建,包含7个查询。

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. 替换所有mysql_query(ies)并不是一个真正的选择,有太多的mysql_queries需要替换,尽管我可以花一天的时间去做它。

Thanks 谢谢

SHOW SESSION STATUS LIKE 'Questions'

SMF does its query counting by having its own custom query function: SMF通过拥有自己的自定义查询功能来进行查询计数:

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. 为mysql_query创建一个包装器并使用它而不是mysql_query。

我认为以下命令是每个会话计数:

SHOW STATUS LIKE 'com_select%'

You can get the number of queries ever executed by calling. 您可以通过调用获取执行的查询数。

show session status like "Queries";

Call this at the beginning and at the end of page creation, and then you can see how many queries there have been. 在页面创建的开始和结束时调用它,然后您可以看到已经有多少查询。 Don't forget that this command itself is also counted as one. 不要忘记这个命令本身也算作一个。

Here's an example which might be easier to follow than the SMF one. 这是一个比SMF更容易遵循的例子。

class QueryLogger
{
    public $queries = array();

    public function query($sql)
    {
        $start = microtime(true);

        $query = mysql_query($sql);

        $queries[] = microtime(true) - $start;

        return $query;
    }

    public function getCount()
    {
        return sizeof($this->queries);
    }

    public function getTime()
    {
        return array_sum($this->queries);
    }
}

$queryLogger = new QueryLogger;
$query1 = $queryLogger->query('...');
$query2 = $queryLogger->query('...');
echo 'Ran '.$queryLogger->getCount().' queries in '.$queryLogger->getTime().' seconds.';

After Quassnoi comment i put this in the start of script: Quassnoi评论后,我把它放在脚本的开头:

$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 我不知道为什么使用-1,也许它会计算mysql_select_db语句(或smth),但它在我的localhost上工作得很好

To count the number of queries, you need to count the number of queries. 要计算查询数,您需要计算查询数。 Sorry to sound redundant, but it really is that simple. 很抱歉听起来多余,但确实很简单。

Make a counting function ( mysql_query_counted ?), then use grep to search through your codebase for mysql_query( , and it shouldn't take more than an hour or two. Possibly even think about using sed or similar to replace the function calls. 创建一个计数函数( mysql_query_counted ?),然后使用grep在你的代码库中搜索mysql_query(并且它不应该花费超过一两个小时。可能甚至考虑使用sed或类似函数来替换函数调用。

A note on SMF and similar that have this built-in. 关于SMF和类似内容的注释。 They use DB abstraction layers, so they are already using their own query function, and adding query counting at a later date would have been as simple as adding a line incrementing a counter to to that function. 它们使用数据库抽象层,因此它们已经在使用自己的query函数,并且在以后添加查询计数就像添加一个将计数器递增到该函数的行一样简单。 +1 for abstraction and encapsulation I suppose. 我认为+1用于抽象和封装。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM