mysql 慢日志简单格式化

 

<?php

$str = file_get_contents('/Users/chenbingbing/Downloads/product-slow-report.txt');

$str = preg_replace('#\#\s+(?!Query\s*\d+)[^\n]+#', '', $str);
$str = preg_replace("#(\n)+#", "\n", $str);


$sqlKeys = array('SELECT', 'SET', 'SHOW', 'administrator');
$sqlKeys = implode('|', $sqlKeys);

preg_match_all('#ID (.*) at byte.*\n((' . $sqlKeys . ').*)\\\G#iUs', $str, $matches);

$sep = "\t";

foreach ($matches[1] as $k => $id) {
    $sql = $matches[2][$k];
    $sql = str_replace('\G', '', $sql);
    preg_match('#/\* (.*)\*/#', $sql, $match);


    if (!empty($match)) {
        $sql = trim(str_replace($match[0], '', $sql));
        $visit = trim($match[1]);
    } else {
        $visit = '';
    }
    $sql = preg_replace('#\s+#', ' ', $sql);

    echo $id . $sep . '"' . $visit . '"' . $sep . '暂无' . $sep . '"' . $sql . '"';
    echo PHP_EOL;
}
//

 

 


 

posted on 2020-04-30 15:11  bandbandme  阅读(404)  评论(0编辑  收藏  举报