如何在 MVC 框架中分析 SQL 查询?

发布于 2024-08-13 16:24:48 字数 114 浏览 4 评论 0原文

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

谢谢

I have been working on my own php MVC framework. I just wanted to get your ideas on how to profile all the queries run on my framework. I have done other profiling stuff but i have no clear idea of how to profile the sql queries too for the users to see if they turn profiling on.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

难以启齿的温柔 2024-08-20 16:24:48

这实际上取决于您想要多少信息,但为了对您来说有点通用,我会考虑这样的东西(该类是您用来与数据库交互的任何内容):

<?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();
?>

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

It really depends on how much information you want, but to be a bit generic for you I would consider something this this (the class is whatever you are using to interact with the database):

<?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();
?>

This is obviously a very simple example, but this way would be both very easy, as well has pretty powerful in terms of things you can store. For example you could also store the number of rows affected by a query, the number of tables a query touches, etc.

草莓酥 2024-08-20 16:24:48

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

Zend Framework is a good example for you. You can see SQL queries under the web site. If you write your own framework you can follow that way. Users can open or close one boolean variable. If They set it "true" you can write sql queries under the web page. With that solution coders can see which queries are running by MySQL.

糖粟与秋泊 2024-08-20 16:24:48

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

Nusphere PhpED v10 comes with PHP SQL profiler that works great with all php frameworks and projects without any frameworks at all.
http://www.nusphere.com/products/php_sql_profiler.htm

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文