[英]Count number of queries per page request?
I need to know how many sql queries are being executed per page request. 我需要知道每个页面请求正在执行多少个SQL查询。 As the site is already done and I am just running optimization analysis, I would prefer if any solution offered doesnt require that i change the entire website structure.
由于该站点已经完成,并且我正在运行优化分析,因此,如果提供的任何解决方案都不需要我更改整个网站结构,我希望这样做。
I use a single connection to send all queries to the MySQL database: 我使用单个连接将所有查询发送到MySQL数据库:
define('DATABASE_SERVER', '127.0.0.1');
define('DATABASE_NAME', 'foco');
define('DATABASE_USERNAME', 'root');
define('DATABASE_PASSWORD', '');
$DB_CONNECTION = new mysqli(
DATABASE_SERVER,
DATABASE_USERNAME,
DATABASE_PASSWORD,
DATABASE_NAME,
3306
);
Then to execute a query i use: 然后执行查询,我使用:
$query = "SELECT * FROM `sometable`";
$queryRun = $DB_CONNECTION->query($query);
Is there a way to count how many queries have been sent and log the answer in a text file just before php closes the connection? 有没有一种方法可以在PHP关闭连接之前计算已经发送了多少查询并将答案记录在文本文件中?
You can extend the mysqli object and override the query method: 您可以扩展mysqli对象并覆盖查询方法:
class logging_mysqli extends mysqli {
public $count = 0;
public function query($sql) {
$this->count++;
return parent::query($sql);
}
}
$DB_CONNECTION = new logging_mysqli(...);
One of the best ways would be to run a log function inside your $DB_CONNECTION->query()
. 最好的方法之一是在
$DB_CONNECTION->query()
运行一个日志函数。
That way you can either log each individual query to a db table, or perform basic test on query speed and then store this, or just increment the count (for number of queries) and store this. 这样,您既可以将每个查询记录到db表中,也可以对查询速度进行基本测试,然后存储它,或者仅增加计数(查询数量)并存储它。
Create class extending mysqli
, make $DB_CONNECTION object of that class and do your statistics in your implementation of query()
method. 创建扩展
mysqli
类,创建该类的$ DB_CONNECTION对象,并在实现query()
方法时进行统计。 Eventually it shall call parent::query()
to do real job. 最终,它将调用
parent::query()
来完成实际工作。
Create a proxy that you use instead of mysqli directly... 创建一个代替mysqli直接使用的代理...
class DatabaseCounter {
private $querycount = 0;
private $mysqli = null;
// create mysqli in here in constructor
public function query(...) {
$this->queryCount++;
$this->mysqli->query(...);
}
}
$DB_CONNECTION = new DatabaseCounter();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.