开发者

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜