Last active
August 29, 2015 14:18
-
-
Save schoblaska/74f304055931d344366f to your computer and use it in GitHub Desktop.
tracking down which lines of code are executing mysql queries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $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