你知道vBulletin在调试模式下有一个SQL分析器吗?我该如何为自己的Web应用程序构建一个?它是使用过程化PHP构建的。
谢谢。
谢谢。
http://dev.mysql.com/tech-resources/articles/using-new-query-profiler.html
上面的链接介绍了如何在查询之后获取所有SQL分析信息。
最好的实现方法是创建一个数据库类,并在其中添加一个“profile”标志来打开查询日志记录和适当的信息,如上面链接所示。
例如:
Class dbthing{
var $profile = false;
function __construct($profile = false){
if($profile){
$this->query('set profiling=1');
$this->profile = true;
}
...
}
function query($sql, $profile_this == false){
...
if($this->profile && !$profile_this)
$this->query("select sum(duration) as qtime from information_schema.profiling where query_id=1", true);
... // store the timing here
}
}
我使用一个数据库连接包装器,可以在其周围放置一个性能分析包装器。这样,我可以丢弃包装器或更改它,而不需要更改我的基本连接器类。
class dbcon {
function query( $q ) {}
}
class profiled_dbcon()
{
private $dbcon;
private $thresh;
function __construct( dbcon $d, $thresh=false )
{
$this->dbcon = $d;
$this->thresh = $thresh;
}
function queury( $q )
{
$begin = microtime( true );
$result = this->dbcon->query();
$end = microtime( true );
if( $this->thresh && ($end - $begin) >= $this->thresh ) error_log( ... );
return $result;
}
}
使用10秒的阈值进行性能分析:
$dbc = new profiled_dbcon( new dbcon(), 10 );
我使用了error_log()函数来记录错误发生的时间。我不会将查询性能日志记录回数据库服务器,因为这会影响数据库服务器性能。你应该让你的Web服务器吸收这种影响。