如何在 Rails 中获取 SQL 查询执行时间? 我可以在日志中看到时间,例如:
Posting Load (10.8ms) SELECT "postings".* FROM "postings" ORDER BY "postings"."id" ASC LIMIT 1
但是我如何以编程方式获取该值 (10.08) 以在我的代码中进一步使用它?
最佳答案
您可以使用 ActiveSupport::Notifications检索 SQL 语句的运行时间
您应该能够检测 sql.active_record
以查看 SQL 调用需要多长时间
ActiveSupport::Notifications.subscribe('sql.active_record') do |*args|
event = ActiveSupport::Notifications::Event.new(*args)
event.duration #how long it took to run the sql command
end
这段代码没有经过测试,所以我不能保证它有效,但它应该有效
关于ruby-on-rails - ActiveRecord SQL 执行时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34279304/