数据库驱动增加getExplain方法用于性能分析

This commit is contained in:
thinkphp
2016-02-01 10:07:48 +08:00
parent 35be826195
commit f6b2b3528e
6 changed files with 68 additions and 9 deletions

View File

@@ -16,9 +16,9 @@ use think\Config;
use think\Db;
use think\Debug;
use think\Exception;
use think\exception\DbBindParamException;
use think\exception\DbException;
use think\exception\PDOException;
use think\exception\DbBindParamException;
use think\Log;
abstract class Driver
@@ -295,8 +295,8 @@ abstract class Driver
if (!$result) {
throw new DbBindParamException(
"Error occurred when binding parameters '{$param}'",
$this->config,
$this->queryStr,
$this->config,
$this->queryStr,
$bind
);
}
@@ -1190,12 +1190,8 @@ abstract class Driver
$log = $this->queryStr . ' [ RunTime:' . Debug::getRangeTime('queryStartTime', 'queryEndTime') . 's ]';
// SQL性能分析
if (0 === stripos(trim($this->queryStr), 'select')) {
$pdo = $this->linkID->query("EXPLAIN " . $this->queryStr);
$result = $pdo->fetch(PDO::FETCH_ASSOC);
if (strpos($result['extra'], 'filesort') || strpos($result['extra'], 'temporary')) {
Log::record('SQL:' . $this->queryStr . '[' . $result['extra'] . ']', 'warn');
}
$log .= '[ EXPLAIN : ' . var_export($result, true) . ' ]';
$result = $this->getExplain($this->queryStr);
Log::record('[ EXPLAIN : ' . var_export($result, true) . ' ]', 'sql');
}
Log::record('[ SQL ] ' . $log, 'sql');
}

View File

@@ -12,6 +12,7 @@
namespace think\db\driver;
use think\db\Driver;
use think\Log;
/**
* mysql数据库驱动
@@ -53,6 +54,7 @@ class Mysql extends Driver
$result = $this->query($sql);
$info = [];
if ($result) {
$result = array_change_key_case($result);
foreach ($result as $key => $val) {
$info[$val['field']] = [
'name' => $val['field'],
@@ -113,4 +115,21 @@ class Mysql extends Driver
{
return 'rand()';
}
/**
* SQL性能分析
* @access protected
* @param string $sql
* @return array
*/
protected function getExplain($sql)
{
$pdo = $this->linkID->query("EXPLAIN " . $sql);
$result = $pdo->fetch(PDO::FETCH_ASSOC);
$result = array_change_key_case($result);
if (strpos($result['extra'], 'filesort') || strpos($result['extra'], 'temporary')) {
Log::record('SQL:' . $this->queryStr . '[' . $result['extra'] . ']', 'warn');
}
return $result;
}
}

View File

@@ -198,4 +198,15 @@ class Oracle extends Driver
{
return 'DBMS_RANDOM.value';
}
/**
* SQL性能分析
* @access protected
* @param string $sql
* @return array
*/
protected function getExplain($sql)
{
}
}

View File

@@ -120,4 +120,15 @@ class Pgsql extends Driver
{
return 'RANDOM()';
}
/**
* SQL性能分析
* @access protected
* @param string $sql
* @return array
*/
protected function getExplain($sql)
{
}
}

View File

@@ -42,6 +42,7 @@ class Sqlite extends Driver
$result = $this->query('PRAGMA table_info( ' . $tableName . ' )');
$info = [];
if ($result) {
$result = array_change_key_case($result);
foreach ($result as $key => $val) {
$info[$val['name']] = [
'name' => $val['name'],
@@ -101,4 +102,15 @@ class Sqlite extends Driver
{
return 'RANDOM()';
}
/**
* SQL性能分析
* @access protected
* @param string $sql
* @return array
*/
protected function getExplain($sql)
{
}
}

View File

@@ -176,4 +176,14 @@ class Sqlsrv extends Driver
return $this->execute($sql, $this->getBindParams(true), !empty($options['fetch_sql']) ? true : false);
}
/**
* SQL性能分析
* @access protected
* @param string $sql
* @return array
*/
protected function getExplain($sql)
{
}
}