Mysql - 慢查询输出

时间:2013-06-26 15:49:50

标签: mysql formatting

我从我的amazon RDS实例运行了slowquery日志,我可以使用“sloquery.log”格式的自定义脚本来检索它们。

我的问题很简单,是否有任何格式化程序可以使报告更具“可读性”。

当前输出是这样的:

http://i.imgur.com/RUew7gS.png

由于

2 个答案:

答案 0 :(得分:1)

是的,mysqldumpslow在所有标准MySQL发行版中都可用。

答案 1 :(得分:1)

我打破了这个原始的PHP脚本,将日志提取为CSV格式,这意味着我可以使用电子表格轻松操作它。

您可能需要更改输入和输出位置(我只需将日志拖到我的localhost,然后在那里运行脚本)。

<?php
set_time_limit(240);
$handle = fopen('C:\\Users\\kwalker\\Downloads\\mysql-slow.log', "rb");
$fp = fopen('file.csv', 'w');
$inline = '';
$inline = fgets($handle, 8192);
$OutLine = array();
$OutLine['Time'] = 'Time';
$OutLine['Timestamp'] = 'Timestamp';
$OutLine['User'] = 'User';
$OutLine['Query_time'] = 'Query_time';
$OutLine['Lock_time'] = 'Lock_time';
$OutLine['Rows_sent'] = 'Rows_sent';
$OutLine['Rows_examined'] = 'Rows_examined';
$OutLine['Database'] = 'Database';
$OutLine['SqlOut'] = 'SqlOut';
WriteOut($fp, $OutLine);
$OutLine = array();
$OutLine['Time'] = '';
$OutLine['Timestamp'] = '';
$OutLine['User'] = '';
$OutLine['Query_time'] = '';
$OutLine['Lock_time'] = '';
$OutLine['Rows_sent'] = '';
$OutLine['Rows_examined'] = '';
$OutLine['Database'] = '';
$OutLine['SqlOut'] = '';
$PossibleUse = true;
$TimeTriggeredOut = true;
$CurrentTime = '';
$CurrentDatabase = '';

while (!feof($handle)) 
{
    switch (true)
    {
        case substr($inline, 0, 8) == '# Time: ' :
            WriteOut($fp, $OutLine);
            $PossibleUse = true;
            $Timings = explode(': ', $inline);
            $CurrentTime = $Timings[1];
            $OutLine = array();
            $OutLine['Time'] = $CurrentTime;
            $OutLine['Timestamp'] = '';
            $OutLine['User'] = '';
            $OutLine['Query_time'] = '';
            $OutLine['Lock_time'] = '';
            $OutLine['Rows_sent'] = '';
            $OutLine['Rows_examined'] = '';
            $OutLine['Database'] = $CurrentDatabase;
            $OutLine['SqlOut'] = '';
            $TimeTriggeredOut = true;
            break;
        case substr($inline, 0, 6) == '# User' :
            if (!$TimeTriggeredOut)
            {
                WriteOut($fp, $OutLine);
                $PossibleUse = true;
                $OutLine = array();
                $OutLine['Time'] = $CurrentTime;
                $OutLine['Timestamp'] = '';
                $OutLine['User'] = '';
                $OutLine['Query_time'] = '';
                $OutLine['Lock_time'] = '';
                $OutLine['Rows_sent'] = '';
                $OutLine['Rows_examined'] = '';
                $OutLine['Database'] = $CurrentDatabase;
                $OutLine['SqlOut'] = '';
            }
            $OutLine['User'] = $inline;
            $TimeTriggeredOut = false;
            break;
        case substr($inline, 0, 12) == '# Query_time' :
            $Timings = explode(' ', $inline);
            //print_r($Timings);
            $OutLine['Query_time'] = $Timings[2];
            $OutLine['Lock_time'] = $Timings[5];
            $OutLine['Rows_sent'] = $Timings[7];
            $OutLine['Rows_examined'] = $Timings[10];
            $PossibleUse = true;
            break;
        case substr($inline, 0, 14) == 'SET timestamp=' :
            $Timings = explode('=', $inline);
            $OutLine['Timestamp'] = $Timings[1];
            $PossibleUse = true;
            break;
        case $PossibleUse AND substr($inline, 0, 4) == 'use ' :
            $Timings = explode(' ', $inline);
            $CurrentDatabase = $Timings[1];
            $OutLine['Database'] = $CurrentDatabase;
            $PossibleUse = false;
            break;
        default;
            $OutLine['SqlOut'] .= $inline;
    }
    $inline = fgets($handle, 8192);
}
fclose($fp);
fclose($handle);

function WriteOut($fp, $OutLine)
{
    foreach($OutLine as &$aOutLine)
    {
        $aOutLine = str_replace("\n", " ", $aOutLine);
        $aOutLine = str_replace("\r", " ", $aOutLine);
        $aOutLine = str_replace("\t", " ", $aOutLine);
    }
    fputcsv($fp, $OutLine);
}
?>