2009-10-30 39 views
4

您知道vBulletin在調試模式下如何具有sql分析器嗎?我將如何去爲自己的Web應用程序構建一個?它建立在程序化的PHP中。PHP + MySQL分析器

謝謝。

回答

7

http://dev.mysql.com/tech-resources/articles/using-new-query-profiler.html

上面的鏈接鏈接,您如何能得到任何人查詢後的SQL配置文件信息。

實現它的最佳方法是創建一個數據庫類並讓它有一個「配置文件」標誌來打開查詢記錄和相應的信息,如上面鏈接所示。

實施例:

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 
    } 

} 
3

我使用數據庫連接的包裝,我可以角落找尋放置一個分析包裝。這樣我可以丟棄包裝器,或者更改它,而無需更改我的基礎連接器類。

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中()什麼時間是。我不會將查詢性能記錄回數據庫服務器,這會影響數據庫服務器的性能。你寧願讓你的網站負責人吸收這種影響。

+0

這不會給出一個準確的結果,因爲microtime()提供服務器時間而不是實際的操作所需的處理器時間(或HD訪問時間等),因此最好使用內置的函數得到一個過程需要多長時間 – 2009-10-30 22:53:02

+0

@mike v:非常好的一點。感謝您發佈關於mysql profiler功能的鏈接,我很樂意詳細瞭解這一點。 – 2009-11-01 19:33:51

+0

你不是在爲自己創造工作嗎?爲什麼不使用mysql慢速查詢日誌? – starmonkey 2010-10-15 00:45:54

0

雖然很晚,但Open PHP MyProfiler可以幫助您實現這一目標,並且您可以從代碼中提取功能部分以供您使用。