Skip to content

Instantly share code, notes, and snippets.

Last active Aug 29, 2015
What would you like to do?
tracking down which lines of code are executing mysql queries
$queries = 0
$locations = { |h, k| h[k] = 0 }
class ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter
def execute_with_tracking(sql, name = nil)
$queries += 1{ |c| c =~ /app/ }.each{ |c| $locations[c] += 1 }
execute_without_tracking(sql, name)
alias_method_chain :execute, :tracking
# code you want to profile here
$locations.sort_by{|l| l[1]}.reverse[0, 20].each do |location, count|
puts "#{count}:\t#{location}"
end; 1
puts "#{$queries} queries"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment