mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-01 20:52:48 +08:00
数据库驱动增加getExplain方法用于性能分析
This commit is contained in:
@@ -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');
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -198,4 +198,15 @@ class Oracle extends Driver
|
||||
{
|
||||
return 'DBMS_RANDOM.value';
|
||||
}
|
||||
|
||||
/**
|
||||
* SQL性能分析
|
||||
* @access protected
|
||||
* @param string $sql
|
||||
* @return array
|
||||
*/
|
||||
protected function getExplain($sql)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,4 +120,15 @@ class Pgsql extends Driver
|
||||
{
|
||||
return 'RANDOM()';
|
||||
}
|
||||
|
||||
/**
|
||||
* SQL性能分析
|
||||
* @access protected
|
||||
* @param string $sql
|
||||
* @return array
|
||||
*/
|
||||
protected function getExplain($sql)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user