Tracing SQL queries created by Ruby-on-Rails
In开发者_Go百科 the web application I am currently developing, I have quite a few database queries being performed. I would like to know what parts of the code are producing these queries so that I can perhaps refactor the code to reduce them. Is there an easy way to do this?
Typically, the database queries are like:
SELECT count(*) AS count_all FROM 'stores' WHERE ('stores'.'distributor_id' = 1)
Thanks very much for your suggestions!
Gav
I think you're looking for QueryTrace.
To show the SQL of a query on a Ruby on Rails console just follow these steps:
if ENV.include?('RAILS_ENV') && !Object.const_defined?('RAILS_DEFAULT_LOGGER')
require 'logger'
RAILS_DEFAULT_LOGGER = Logger.new(STDOUT)
end
path_to_project$ sc
Loading development environment (Rails 2.3.7)
>> User.first
User Load (0.8ms) SELECT * FROM users LIMIT 1
精彩评论