2013-11-23 20 views
0

所以當我運行show profile for query N時,我得到了很多有用的信息,比如查詢花費多長時間發送數據,釋放資源等。MySQL profiler在運行時產生與'show profiles'相同的數據

不幸的是,show profile命令只能從命令提示符下運行。這是一個問題,因爲我有一些運行緩慢的查詢,當我從命令提示符運行它們時運行得很好,但在實際場景中運行時效果不佳。

有誰知道如何從show profile獲得相同類型的數據,但在運行時從實時查詢中獲取它?我已經閱讀了關於使用通用查詢日誌的答案,但這是而不是想要的,因爲通用日誌僅列出所做的查詢,而不是他們在查詢的每個階段花費的多少。

回答

0

我從來沒有見過你想要的。但你可以完成你想要的東西。 開發程序,監視慢日誌並立即重新運行慢查詢並打印show profile輸出。

我寫了PHP僞代碼,我查了show profile在PHP中也很好用。但如何監控慢日誌和解析日誌內容未經測試。下面只是僞代碼。它不會工作。

$conn = mysql_connect("localhost", "user", "passwd"); 

monitor_slow_log($conn); 

function monitor_slow_log($conn) 
{ 
    $res = mysql_query("set profiling = 1"); 

    $fp = fopen("/path/to/slow_log", "r"); 

    while (($str = fgets($fp, 10000)) !== false) 
    { 
     $query = parse_and_extract_query($str); 
     get_profile_result($str); 
    } 
} 

function parse_and_extract_query($str) 
{ 
    // I'm not sure how to parse it. 
    // as you know there are more than slow query. 
} 

function get_profile_result($query) 
{ 
    $res = mysql_query($query); 
    while ($row = mysql_fetch_array($res)) 
    { 
     // fetch all record 
    } 
    mysql_free_result($res); 

    // print show profile result 
    $res = mysql_query("show profile"); 

    while ($row = mysql_fetch_assoc($res)) 
    { 
     echo $row["Status"]." ===> ".$row['Duration']."\n"; 
    } 

    mysql_free_result($res); 

    // print SHOW PROCESSLIST 
    mysql_query("show processlist") and print 

    // print SHOW ENGINE INNODB STATUS 
    mysql_query("show engine innodb status") and print 

    // print SHOW GLOBAL STATUS 
    mysql_query("show global status") and print 

    // print `top` output 
    system('top -n 1'); 
} 

另外,不僅show profile,但也需要一些信息如下。

  • SHOW PROCESSLIST
  • SHOW ENGINE INNODB STATUS
  • SHOW全球地位
  • top輸出
+0

謝謝,@ jungsu-HEO,但我不只是想重新運行查詢和得到他們的個人資料,我想在他們運行時得到他們的個人資料*。 –

+0

@TolaOdejayi是的。所以我建議polloing每1秒慢日誌並重新運行查詢和顯示配置文件。 –

相關問題