Difference between revisions of "Top MySQL Slow Queries from log"

From James Dooley's Wiki
Jump to: navigation, search
(Display 2 lines before and 3 lines after Query_time | less)
Line 1: Line 1:
 +
[[Category:One Liners]]
 
==Overview==
 
==Overview==
 
Sort the slow query log based on the query time and display the lines after it
 
Sort the slow query log based on the query time and display the lines after it

Revision as of 17:40, 17 February 2012

Overview

Sort the slow query log based on the query time and display the lines after it

Script

Display 2 lines before/after Query_time

[bash,n] slowlog=`grep log-slow-queries /etc/my.cnf | cut -d'=' -f2`; \ if [ -e "$slowlog" ]; then \ for i in $(grep -n Query_time $slowlog | sort -k3 -nr | head | cut -d: -f1); do line=`sed -n \`echo $i\`p $slowlog`; grep -B2 -A2 "$line" $slowlog;done; \ fi

Display 2 lines before and 3 lines after Query_time | less

[bash,n] slowlog=`grep log-slow-queries /etc/my.cnf | cut -d'=' -f2`; \ if [ -e "$slowlog" ]; then \ for i in $(grep -n Query_time $slowlog | sort -k3 -nr | head | cut -d: -f1); do line=`sed -n \`echo $i\`p $slowlog`; grep -B2 -A3 "$line" $slowlog;done \ fi | less