Tuesday, April 1, 2014

how to convert existing mysql slow queries log in csv file

http://stackoverflow.com/questions/14479199/mysql-slow-queries-visualize

This is a noddy script I use to help me sort them out. Change the file name at the top and execute it. Puts the details out in CSV format so you can manipulate them easily (note, delete the 2nd line of the CSV file as it is garbage). Note that I cannot confirm whether or not the file you have is exactly the same format, but hopefully you can play with this to sort out what you need.


php
$handle = fopen('C:\\somelocation\\mysql-slow.log', "rb");
$fp = fopen('someoutputfile.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);
}
?>

0 comments:

Post a Comment