【问题标题】:How to profile SQL queries in MVC framework?如何在 MVC 框架中分析 SQL 查询?
【发布时间】:2009-12-08 12:11:33
【问题描述】:

我一直在开发自己的 php MVC 框架。我只是想了解您对如何分析在我的框架上运行的所有查询的想法。我已经完成了其他分析工作,但我不清楚如何分析 sql 查询,以便用户查看他们是否打开分析。

谢谢

【问题讨论】:

  • 这取决于您想要什么信息。如果您只需要查询的数量,请在代码中使用相同的实例并在内部存储该数量。
  • 这和 MVC 有什么关系?

标签: php model-view-controller profiling


【解决方案1】:

这真的取决于你想要多少信息,但为了对你来说有点通用,我会考虑这个(类是你用来与数据库交互的任何东西):

<?php
//Your Database access class
class DB{
    //Create private property for holding profiling data
    private $sqlDataProfile = array();

    function query($sql){
        $starttime = time();//Get the time before the query

        //Query and whatnot here

        //Start profiling here:
        $this->addSqlProfile($sql,$starttime);
    }

    //Get total number of queries run
    function getNumberOfQueries(){
        return count($this->sqlDataProfile);
    }

    private function addSqlProfile($sql,$starttime){
        //Create temporary array
        $tempArr = array(
          'sql'         => $sql,
          'time'        => time()-$starttime
        );

        //Push tempArr to the sqlDataProfile var
        $this->sqlDataProfile[] = $tempArr;
    }
}

$db = new DB;
$db->query('SELECT * FROM table');
echo $db->getNumberOfQueries();
?>

这显然是一个非常简单的示例,但这种方式既非常简单,而且在您可以存储的东西方面也非常强大。例如,您还可以存储受查询影响的行数、查询涉及的表数等。

【讨论】:

    【解决方案2】:

    Zend Framework 是一个很好的例子。您可以在网站下看到 SQL 查询。如果您编写自己的框架,则可以遵循这种方式。用户可以打开或关闭一个布尔变量。如果他们将其设置为“true”,您可以在网页下编写 sql 查询。使用该解决方案,编码人员可以查看 MySQL 正在运行哪些查询。

    【讨论】:

      【解决方案3】:

      Nusphere PhpED v10 带有 PHP SQL 分析器,它适用于所有 php 框架和根本没有任何框架的项目。 http://www.nusphere.com/products/php_sql_profiler.htm

      【讨论】:

        猜你喜欢
        • 2012-01-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-03
        • 1970-01-01
        • 2012-03-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多