doutao1282 2009-12-08 12:11
浏览 17
已采纳

如何在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

  • 写回答

3条回答 默认 最新

  • dongxian3574 2009-12-08 15:37
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!