mysql-slow-queries 可视化

标签 mysql logfile

我从我的 web-hosting 提供商那里收到了一个名为 mysqld-slow-queries.log 的文件。我需要分析这个文件,因为可能有一些函数对服务器造成巨大的负载。

但是,当我使用 Notepad++ 或类似工具打开文件时,我看到 14000 行 包含信息,没有任何结构。

有没有可以导入.log 文件并可视化报告的程序?基本上我希望能够对 Query_time 值进行排序。

最佳答案

这是我用来帮助我整理它们的点头脚本。更改顶部的文件名并执行它。以 CSV 格式显示详细信息,以便您轻松操作它们(请注意,删除 CSV 文件的第二行,因为它是垃圾)。请注意,我无法确认您拥有的文件格式是否完全相同,但希望您可以使用它来整理您需要的内容。

<?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);
}
?>

关于mysql-slow-queries 可视化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14479199/

相关文章:

mysql - 为什么我可以使用外键约束自由输入值

python - Django 中菜单和子菜单的递归函数,直到最后一个子菜单出现

python - 如何在Python中使用正则表达式获取两个特定字符之间的第一个整数?

python - 寻找常闭日志文件的 Python 方法

MySQL根据列值授予权限?

php - 如何在mysql和php中检查上个月是否已经过去?

mysql - 如何向用户授予 Cloud Foundry 上 Aurora 的访问权限?

tfs-2015 - 是否有用于 vnext 构建日志的日志查看器?

tomcat - 如何在访问日志文件中捕获 post 方法请求?

linux - 如何从日志文件中删除日志信息(时间戳等)