Skip to content

Instantly share code, notes, and snippets.

@EdwardDiehl
Created March 30, 2018 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EdwardDiehl/329952e329d4670c65bf46da168723a2 to your computer and use it in GitHub Desktop.
Save EdwardDiehl/329952e329d4670c65bf46da168723a2 to your computer and use it in GitHub Desktop.
Ruby on Rails – your own slow query log, no sql configuration required
# http://pdabrowski.com/blog/ruby-on-rails/slow-query-log/
# config/initializer/slow_query_logger.rb
class SlowQueryLogger
MAX_DURATION = 3.0
def self.initialize!
ActiveSupport::Notifications.subscribe('sql.active_record') do |name, start, finish, id, payload|
duration = finish.to_f - start.to_f
if duration >= MAX_DURATION
SomeLogger.log("slow query detected: #{payload[:sql]}, duration: #{duration}")
end
end
end
end
SlowQueryLogger.initialize!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment