简体   繁体   English

计算每个页面加载PDO的查询数

[英]Count Number of Queries Each Page Load with PDO

I'm specifically looking to extend the PDO object somehow to add an internal counter every time a query is executed. 我特意想要以某种方式扩展PDO对象,以便在每次执行查询时添加内部计数器。 I want to do this in a way were I wouldn't have to change any of my existing queries or add an extra function like this suggests . 我希望以某种方式执行此操作,因为我不必更改任何现有查询或添加此类建议的额外功能。

I not that familiar with classes or the PDO class in particular, but could this be done? 我不熟悉类或PDO类,但是可以这样做吗? How would I go about this? 我该怎么做?

I'm thinking extend query and execute and increment a stored variable each time they're called. 我正在考虑扩展queryexecute每次调用时execute和增加存储的变量。

Extending PDO would be done like any other class. 扩展PDO将像任何其他类一样完成。 Would this suit your needs? 这会满足你的需求吗? The only other code change would be having to instantiate this class instead of the PDO class when making your initial connection. 唯一的其他代码更改是在进行初始连接时必须实例化此类而不是PDO类。

class PDOEx extends PDO
{
    private $queryCount = 0;

    public function query($query)
    {
    // Increment the counter.
        ++$this->queryCount;

    // Run the query.
        return parent::query($query);
    }

    public function exec($statement)
    {
    // Increment the counter.
        ++$this->queryCount;

    // Execute the statement.
        return parent::exec($statement);
    }

    public function GetCount()
    {
        return $this->queryCount;
    }
}

Just execute: 只需执行:

SHOW PROFILES;

The greatest Query ID is what you are after. 最大的Query ID就是您所追求的。 This will also give you information about the last 15 (default) query execution time. 这还将为您提供有关最近15(默认)查询执行时间的信息。

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

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