Skip to content

Instantly share code, notes, and snippets.

@nanasess
Forked from rysk92/MysqldumpslowResult2Csv.php
Last active October 12, 2016 01:32
Show Gist options
  • Save nanasess/ac68ec0bdb46c25878ffa1513256c484 to your computer and use it in GitHub Desktop.
Save nanasess/ac68ec0bdb46c25878ffa1513256c484 to your computer and use it in GitHub Desktop.
mysqldumpslow to csv for MySQL 5.5
<?php
#how to use, mysqldumpslow mysql-slow.log > hoge
# php MysqldumpslowResult2Csv hoge > hoge.csv
$log_filename = $argv[1];
echo 'SQL, Count, Time, Time(total), Lock, Lock(total), Rows_sent, Rows_sent(total), Rows_examined, Rows_examined(totao), Src'."\n";
if (file_exists($log_filename))
{
$contents = file_get_contents($log_filename);
$queries = preg_split('/\n{2,}/', $contents);
foreach ($queries as $query)
{
if (!$query)
{
continue;
}
$line = array();
$sql = preg_split('/\n /', $query);
$information = array_shift($sql);
preg_match('/^Count: (\d+) Time=(\d+\.\d{2})s \((\d+)s\) Lock=(\d+\.\d{2})s \((\d+)s\) Rows_sent=(\d+\.\d) \((\d+)\), Rows_examined=(\d+\.\d) \((\d+)\), (.*)/', $information, $line);
array_shift($line);
array_unshift($line, sprintf('"%s"', implode(' ', $sql)));
echo implode(',', $line)."\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment