如何计算每页使用的MySQL查询总数? [英] How can I count the total number of MySQL queries used per page?

查看:358
本文介绍了如何计算每页使用的MySQL查询总数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP或MySQL中是否有内置函数,它将提供页面上使用的MySQL查询的总数?我在许多网站(主要是论坛)上看到他们在底部有一个消息,像页面生成在0.6秒与20个查询。

Is there a built-in function in PHP or MySQL that will provide the total number of MySQL queries used on a page? I've seen on a lot of sites (mainly forums) they have a message at the bottom saying something like "Page generated in 0.6 seconds with 20 queries".

如果没有内置的,我会添加一些东西到我的数据库类来计数,但它似乎已经是可用的那种功能。

If there's nothing built in then I'll add something to my database class to count them, but it seems like the kind of functionality that would already be available.

推荐答案

选项一是通过包装器传递所有查询:

Option one would be to pass all of your queries through a wrapper:

function custom_mysql_query($sql)
{
    $GLOBAL['query_count'] ++;
    return mysql_query($sql);
}

请注意,仅供说明之用,无错误处理等。

Please note that's for illustration only and without error handling, etc.

您可以查询MySQL的运行次数:

You could query MySQL for the number of queries run:

mysql> SHOW STATUS LIKE 'Com_select';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Com_select    | 2     | 
+---------------+-------+
1 row in set (0.00 sec)

您可能需要执行以下操作:

You might want to do something like:

SHOW STATUS LIKE 'Com_%';

,然后将Com_select,Com_update,Com_insert和Com_delete添加在一起

and then add together Com_select, Com_update, Com_insert and Com_delete

这篇关于如何计算每页使用的MySQL查询总数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆