Skip to content

Instantly share code, notes, and snippets.

@ferblape
Created May 16, 2012 16:10
Show Gist options
  • Save ferblape/2711712 to your computer and use it in GitHub Desktop.
Save ferblape/2711712 to your computer and use it in GitHub Desktop.
Filter slow queries from Rails log file
# Usage:
# cat <environment>.log | grep Load | grep SELECT | ruby filter.rb
MAX_TIME = 30.0
h = {}
STDIN.read.split("\n").each do |line|
list = line.split(' ')
time = list[2].match(/\d+\.\d+/)[0].to_f
query = ([list[3],"SQL_NO_CACHE"] + list[4..-1]).join(" ") + ';'
h[query] = time
end
h.sort_by{ |query, time| time }.reverse.select{ |query, time| time > MAX_TIME }.each do |query, time|
puts "#{time} - #{query}"
puts
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment