snax

ruby performance

slow query raiser plugin

module SlowQueryRaiser
  TIME = 0.75
  RAISE = (RAILS_ENV == 'development')
  
  class SlowQueryError < StandardError
  end  
end

module ActiveRecord
  module ConnectionAdapters
    class AbstractAdapter

      def reset_runtime_with_raising      
        runtime = reset_runtime_without_raising
        if runtime > SlowQueryRaiser::TIME

          msg = "Query is too slow (took #{runtime} seconds)"
          if SlowQueryRaiser::RAISE
            raise SlowQueryRaiser::SlowQueryError, msg
          else
            RAILS_DEFAULT_LOGGER.warn "** #{msg}"
          end
        end            
        runtime
      end

      alias_method_chain :reset_runtime, :raising      
    
    end
  end
end

October 22, 2007

3 comments

stijn says (October 23, 2007):

Hello,

Looks really interesting, but a bit more information about how and where would be really helpful :-). Somebody ?

zerohalo says (October 24, 2007):

Nice little tool for finding those slow queries that need to be optimized. Thanks, Evan.

evan says (October 24, 2007):

Unfortunately when MySQL is cold, bringing it out of swap counts towards the DB time, so you can get a false exception. Not sure how to avoid that without adding a lot of complexity.

Add a comment

Various HTML tags allowed. Use <pre> for code blocks and <code> for inline references.