How to determine which Ruby-on-Rails code generated some SQL?

I have an application written in Ruby using Rails 2.3. We use ActiveRecord.

Sometimes ActiveRecord generates quite terribly inefficient SQL. We can determine that SQL is problematic using slow query logs and using a new relic . However, it is very difficult to determine the line of code in our software that created the problematic SQL. This is usually a query created using associations and named_scopes.

I would really like to somehow mark SQL in our log files with the file name and line of our code that generated it (and not the ActiveRecord line that executed the query).

Information should be accessible through an array of callers. I don’t want to unload the whole array, I just want the file name and line of our code to be most directly responsible. Is there a stone that is already doing this? If not, any suggestions?

+5
source share
3 answers

you should find some inspiration in the active-record-query-trace gem

EDIT: wooops, read it too fast, this is for RoR 3+. > It <seems to work for rails 2.3 . You can also find inspiration here .

+4
source

rack-mini-profiler , ruby ​​ sql.

+1

The new relic will give you full stack traces for individual slow SQL queries using Slow SQL and the Trace function.

0
source

All Articles