Skip to content

Instantly share code, notes, and snippets.

@joeyschoblaska

joeyschoblaska/queries.rb

Last active Aug 29, 2015
Embed
What would you like to do?
tracking down which lines of code are executing mysql queries
$queries = 0
$locations = Hash.new { |h, k| h[k] = 0 }
class ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter
def execute_with_tracking(sql, name = nil)
$queries += 1
caller.select{ |c| c =~ /app/ }.each{ |c| $locations[c] += 1 }
execute_without_tracking(sql, name)
end
alias_method_chain :execute, :tracking
end
#
# 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