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
Nice little tool for finding those slow queries that need to be optimized. Thanks, Evan.
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.


stijn says (October 23, 2007):
Hello,
Looks really interesting, but a bit more information about how and where would be really helpful :-). Somebody ?