Top MySQL Slow Queries from log

From James Dooley's Wiki
Revision as of 14:35, 15 September 2011 by Smsldoo (talk | contribs) (Display 2 lines before and 3 lines after Query_time | less)
Jump to: navigation, search

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