记录SQL语句的查询时间

      发布在:后端技术      评论:0 条评论

记录SQL语句的查询时间,摘抄自Discuz论坛程序方便学习,在此记录

public function query($sql, $silent = false, $unbuffered = false) {
if(defined('DISCUZ_DEBUG') && DISCUZ_DEBUG) {
$starttime = microtime(true);
}

if('UNBUFFERED' === $silent) {
$silent = false;
$unbuffered = true;
} elseif('SILENT' === $silent) {
$silent = true;
$unbuffered = false;
}

$resultmode = $unbuffered ? MYSQLI_USE_RESULT : MYSQLI_STORE_RESULT;

if(!($query = $this->curlink->query($sql, $resultmode))) {
if(in_array($this->errno(), array(2006, 2013)) && substr($silent, 0, 5) != 'RETRY') {
$this->connect();
return $this->curlink->query($sql, 'RETRY'.$silent);
}
if(!$silent) {
$this->halt($this->error(), $this->errno(), $sql);
}
}

if(defined('DISCUZ_DEBUG') && DISCUZ_DEBUG) {
$this->sqldebug[] = array($sql, number_format((microtime(true) - $starttime), 6), debug_backtrace(), $this->curlink);
}

$this->querynum++;
return $query;
}

这里还用到了 debug_backtrace()还可以基本知道整个调用流程,querynum记录了整个访问流程执行了多少sql语句

相关文章
热门推荐