From f6b2b3528ea817c360c76701998700f7850f9fd1 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 1 Feb 2016 10:07:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E9=A9=B1=E5=8A=A8?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0getExplain=E6=96=B9=E6=B3=95=E7=94=A8?= =?UTF-8?q?=E4=BA=8E=E6=80=A7=E8=83=BD=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Driver.php | 14 +++++--------- library/think/db/driver/Mysql.php | 19 +++++++++++++++++++ library/think/db/driver/Oracle.php | 11 +++++++++++ library/think/db/driver/Pgsql.php | 11 +++++++++++ library/think/db/driver/Sqlite.php | 12 ++++++++++++ library/think/db/driver/Sqlsrv.php | 10 ++++++++++ 6 files changed, 68 insertions(+), 9 deletions(-) diff --git a/library/think/db/Driver.php b/library/think/db/Driver.php index 39cdb060..e9e491c8 100644 --- a/library/think/db/Driver.php +++ b/library/think/db/Driver.php @@ -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'); } diff --git a/library/think/db/driver/Mysql.php b/library/think/db/driver/Mysql.php index 9bde9fdb..42a5711b 100644 --- a/library/think/db/driver/Mysql.php +++ b/library/think/db/driver/Mysql.php @@ -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; + } } diff --git a/library/think/db/driver/Oracle.php b/library/think/db/driver/Oracle.php index d3ab4727..da47b8bf 100644 --- a/library/think/db/driver/Oracle.php +++ b/library/think/db/driver/Oracle.php @@ -198,4 +198,15 @@ class Oracle extends Driver { return 'DBMS_RANDOM.value'; } + + /** + * SQL性能分析 + * @access protected + * @param string $sql + * @return array + */ + protected function getExplain($sql) + { + + } } diff --git a/library/think/db/driver/Pgsql.php b/library/think/db/driver/Pgsql.php index 1c38141b..10f9fdc2 100644 --- a/library/think/db/driver/Pgsql.php +++ b/library/think/db/driver/Pgsql.php @@ -120,4 +120,15 @@ class Pgsql extends Driver { return 'RANDOM()'; } + + /** + * SQL性能分析 + * @access protected + * @param string $sql + * @return array + */ + protected function getExplain($sql) + { + + } } diff --git a/library/think/db/driver/Sqlite.php b/library/think/db/driver/Sqlite.php index dfb91b53..ccec4874 100644 --- a/library/think/db/driver/Sqlite.php +++ b/library/think/db/driver/Sqlite.php @@ -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) + { + + } } diff --git a/library/think/db/driver/Sqlsrv.php b/library/think/db/driver/Sqlsrv.php index e28df391..44710268 100644 --- a/library/think/db/driver/Sqlsrv.php +++ b/library/think/db/driver/Sqlsrv.php @@ -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) + { + + } }