ActiveRecord SQL执行时间 [英] ActiveRecord SQL execution time
本文介绍了ActiveRecord SQL执行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何获取Rails中SQL查询的执行时间?
我可以在日志中看到时间,例如:
How can I get the SQL query execution time in rails? I can see the time in logs, e.g.:
Posting Load (10.8ms) SELECT "postings".* FROM "postings" ORDER BY "postings"."id" ASC LIMIT 1
但是我怎么得到那个值(10.08)以编程方式在我的代码中进一步使用它?
But how can I get that value (10.08) programmatically to use it further in my code?
推荐答案
您可以使用ActiveSupport::Notifications 来检索SQL语句的运行时
You can use ActiveSupport::Notifications to retrieve the runtime of the SQL statements
能够检测 sql.active_record
来查看SQL调用所花的时间
You should be able to instrument sql.active_record
to see how long the SQL call takes
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
此代码未经测试,因此我不能保证它能正常工作,但应该会
This code was not tested, so I can't guarantee it works, but it should
这篇关于ActiveRecord SQL执行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文