Skip to content

Instantly share code, notes, and snippets.

@rysk92
Created April 14, 2010 17:08
Show Gist options
  • Save rysk92/366064 to your computer and use it in GitHub Desktop.
Save rysk92/366064 to your computer and use it in GitHub Desktop.
mysqldumpslowの結果をcsvにする
<?php
#how to use, mysqldumpslow mysql-slow.log > hoge
# php MysqldumpslowResult2Csv hoge > hoge.csv
$log_filename = $argv[1];
echo 'Count, Time, Time(total), Lock, Lock(total), Rows, Rows(total), Src, SQL'."\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=(\d+\.\d) \((\d+)\), (.+)$/', $information, $line);
array_shift($line);
array_push($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