Skip to content

Instantly share code, notes, and snippets.

@moofkit
Created March 19, 2025 10:32
Show Gist options
  • Save moofkit/7b4b532570b9dc11467feb794aa2126b to your computer and use it in GitHub Desktop.
Save moofkit/7b4b532570b9dc11467feb794aa2126b to your computer and use it in GitHub Desktop.
ActiveRecord slow query log
# frozen_string_literal: true
ActiveSupport::Notifications.monotonic_subscribe("sql.active_record") do |_name, start, finish, _id, payload|
duration = (finish.to_f - start.to_f) * 1000
# NOTE: duration is in milliseconds
if duration >= 500
sql = payload[:sql].to_s.tr("\n", " ").squeeze(" ")
Rails.logger.info("Slow SQL: #{sql} (duration: #{duration.to_i}ms)")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment