Created
March 19, 2025 10:32
-
-
Save moofkit/7b4b532570b9dc11467feb794aa2126b to your computer and use it in GitHub Desktop.
ActiveRecord slow query log
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
# 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