[英]Ruby on Rails: Get the total number of queries for an ActiveRecord connection for statistics
What is an easy way to get the number/count of queries for an ActiveRecord connection in Ruby on Rails? 在Ruby on Rails中获取ActiveRecord连接的查询数/计数的简单方法是什么?
Subscribing, as explained in this SO thread , seems a bit complex to implement, because I only want the number of real total queries issued over the whole lifetime of the connection. 如该SO线程中所述 ,订阅的实现似乎有点复杂,因为我只希望在整个连接生命周期中发出的实际总查询数。
So i did a monkeypatch initializer on my own :) 所以我自己做了一个Monkeypatch初始化器:)
### Query Counter for mysql2 adapter
module ActiveRecord
module ConnectionAdapters
class AbstractAdapter
@@querycount = 0
attr_reader :querycount
def self.querycount; @@querycount; end
end
class Mysql2Adapter < AbstractMysqlAdapter
def exec_query(sql, name = 'SQL', binds = [])
@@querycount += 1
@querycount = 0 if @querycount.nil?
@querycount += 1
result = execute(sql, name)
ActiveRecord::Result.new(result.fields, result.to_a)
end
end
end
end
You can then get the total querycount of all connections with 然后,您可以使用以下命令获取所有连接的总查询计数
ActiveRecord::ConnectionAdapters::AbstractAdapter.querycount
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.