Logging sql calls

Is there an easy way to log every SQL call to the database? I’d like to
log
mysql queries and their respective execution times into a table so that
I
can closely monitor/report the performance of my application and
identify
potential bottlenecks.

Thanks,
Chad

Chad A. wrote:

Is there an easy way to log every SQL call to the database? I’d like to
log
mysql queries and their respective execution times into a table so that
I
can closely monitor/report the performance of my application and
identify
potential bottlenecks.

If your config.log_level is :debug (which it is for the development
environment, production defaults to :info) then all your sql calls will
be logged.

Fred

I might have been a little unclear…

I’d like to log those sql calls TO a database table.

in example:

class SQLQueries < ActiveRecord::Migration
def self.up
create_table :sql_queries do |t|
t.column :query, :string
t.column :execution_time, :decimal
t.column :date, :timestamp
end
end

thanks,
Chad

anybody else? is it really that tough?

In case anyone in the future cares, I found a pretty good solution for
tracking db layer performance:

  1. I implemented the Query Stats plugin:
    http://code.google.com/p/query-stats/
  2. set an after_filter to insert the results of those calls to the
    database. see below:

after_filter :logging_dbstats

def logging_dbstats
# log sql stats here (pull from footer)
sql = “insert into
logging_dbstats(controller,action,controller_queries,view_queries,execution_time)
values
(’#{params[:controller]}’,’#{params[:action]}’,’#{queries.count_with_label
:controller}’, ‘#{queries.count_with_label :view}’,
‘#{queries.total_time}’)”
ActiveRecord::Base.connection.execute(sql)
end

cheers

Chad wrote:

anybody else? is it really that tough?

Well you could write your implementation of Logger (eg there is one that
logs to syslog) that stuck things in the database, but then you’d be
getting all the logging statements, not just the ones about db queries.
It might be easier to just patch the code for the DB driver you use (or
possibly for the corresponding ActiveRecord db adaptor

Fred

ignore the comment in the code

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs