Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何记录查询执行时间?_Php_Mysql - Fatal编程技术网

Php 如何记录查询执行时间?

Php 如何记录查询执行时间?,php,mysql,Php,Mysql,我试图在我的数据库类中实现缓慢的查询日志记录,但我得到了奇怪的结果。它目前正在记录“大部分”所有查询,并且它说大多数查询运行大约需要0.8秒,我不明白,因为这是在我的开发人员机器上运行的,该机器没有重载 见鬼,我甚至试着在中运行查询,它们在那里运行得非常快 我放错地方了吗?还是我错过了什么 class database { protected $_mysqli; protected $_debug; public function __construct($host,

我试图在我的数据库类中实现缓慢的查询日志记录,但我得到了奇怪的结果。它目前正在记录“大部分”所有查询,并且它说大多数查询运行大约需要0.8秒,我不明白,因为这是在我的开发人员机器上运行的,该机器没有重载

见鬼,我甚至试着在中运行查询,它们在那里运行得非常快

我放错地方了吗?还是我错过了什么

class database {
    protected $_mysqli;
    protected $_debug;

    public function __construct($host, $username, $password, $database, $debug) {
        $this->_mysqli = new mysqli($host, $username, $password, $database);
        $this->_debug = (bool) $debug;
        if (mysqli_connect_errno()) {
            if ($this->_debug) {
                echo mysqli_connect_error();
                debug_print_backtrace();
            }
            return false;
        }
        return true;
    }

    public function q($query) {
        $incomingq = $query;
        if ($query = $this->_mysqli->prepare($query)) {
            if (func_num_args() > 1) {
                $x = func_get_args();
                $args = array_merge(array(func_get_arg(1)),
                    array_slice($x, 2));
                $args_ref = array();
                foreach($args as $k => &$arg) {
                    $args_ref[$k] = &$arg;
                }
                call_user_func_array(array($query, 'bind_param'), $args_ref);
            }
            // Settings
            $SLOW_LOG_TIME = 0.8; // (sec)
            $SLOW_LOG_FILE = 'php_slow.txt';
            $SLOW_LOG_START = time(); // (sec)

            $query->execute();

            // Logging
            $SLOW_LOG_END = microtime(TRUE);
             $time = $SLOW_LOG_END - $SLOW_LOG_START;
            if ($time > $SLOW_LOG_TIME)
            {
                $log = date('Y-m-d H:i:s') . "\t" . round($time, 3) . "\t" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'] . "\t" . str_replace(array("\t" , "\n"), "", $incomingq) . "\r\n";
                $fp = fopen($SLOW_LOG_FILE, 'a+');
                fwrite($fp, $log);
                fclose($fp);
            }

            if ($query->errno) {
                if ($this->_debug) {
                    echo mysqli_error($this->_mysqli);
                    debug_print_backtrace();
                }
                return false;
            }

            if ($query->affected_rows > -1) {
            // if ($query->sqlstate == "00000") {
                return $query->affected_rows;
            }
            $params = array();
            $meta = $query->result_metadata();
            while ($field = $meta->fetch_field()) {
                $params[] = &$row[$field->name];
            }
            call_user_func_array(array($query, 'bind_result'), $params);

            $result = array();
            while ($query->fetch()) {
                $r = array();
                foreach ($row as $key => $val) {
                    $r[$key] = $val;
                }
                $result[] = $r;
            }
            $query->close();
            return $result;
        }
        else {
            if ($this->_debug) {
                echo $this->_mysqli->error;
                debug_print_backtrace();
            }
            return false;
        }
    }

    public function handle() {
        return $this->_mysqli;
    }

    public function last_insert_id()
    {
        return $this->_mysqli->insert_id;
    }
}

您对此使用了错误的单位度量:
$SLOW\u LOG\u START=time(),,
时间()以秒为单位

它应该与
$SLOW\u LOG\u END=microtime(真)一致

而microtime(TRUE)的单位是微秒


因此,更改
$SLOW\u LOG\u START=microtime(TRUE)

您得到的是最接近秒的开始时间,以及最接近微秒的结束时间

$SLOW\u LOG\u START
使用
microtime
,否则您的测量值可能只差一整秒钟


这应该可以解决您的问题。

我相信您已经知道了,但是MySQL可以自己记录任何超过指定时间的查询。这样,您需要自己构建它。

谢谢!有时简单的错误是最糟糕的:)谢谢Gaurav,我会在以后的阶段研究:)