In silence I feel full; With speech I sense emptiness.

Tools to Help to Improve SQL in Rails


Have you ever encountered a Rails app that is slow in rendering pages? Are you trying to find out what's causing it to be "slow"? Where is the performance bottleneck? Is it the slow execution of SQL queries (maybe N+1, or not having the right indexes), or is it something else, like the view being too big?

Performance optimization has been a typical "hard to locate" problem; currently I personally use the following tools to find out where the problem lies:

  1. New Relic old school performance checking tool, after integrating with Rails, it can show the slowest Query in Database monitoring or something like that, but it requires New Relic to subscribe to at least an Essentials level plan.

  2. Gem: active-record-query-trace You can see in the log which code each SQL is coming from.

    Usage Scenario: When you use it, you will find that even though the data has been queried/preloaded in the controller, there is still a SQL query when rendering the view. This tool can help you track down which line of code in the view caused the database query. However, this tool does not actually distinguish whether the SQL is queried in the controller or view, as long as the SQL operation is performed, the first N records of the call stack will be displayed (N defaults to 5).

  3. Gem: bullet This gem can be used to alert you that N+1 queries have occurred in the project, and can be used in Sinatra as well.

< Back