Adds SQL analysis and debugging information to Rails 3.x, 4.x apps.
Latest commit 9df9159 Jan 1, 2017 @tjoyal tjoyal committed on GitHub Merge pull request #21 from lightyear/prepare-for-release
Bump version to 3.0.10


SQL Logging

Build Status

The SQL Logging gem adds useful SQL debugging tools to ActiveRecord. The gem is a repackaging of the old SQL Logging plug-in for Rails 2.x. It, in turn, was based on ideas and plug-ins from Adam Doppelt's (standard logging enhancements) and Nathaniel Talbott (the QueryTrace plug-in).

This gem is for Rails 3+ only. It supports MySQL (including the mysql2 gem), PostgreSQL and SQLite.


Once installed, the out-of-the-box behavior gives you:

  • A cleaned-up backtrace after every SQL query, so you know exactly what piece of code triggered the query.

  • A report of the number of rows and rough count of bytes returned after each query.

  • After each request completes, additional summary information telling you the number of statements executed, as well as a rough total number of bytes received from the database.

  • A top 10 list of SQL executions, sorted by total execution time (based on the location of the query in code), including the total number of rows and bytes returned over all executions.

All of this additional information is logged at DEBUG level, so it won't clutter up your production log files.


The Top 10 summary provides the most options to customize its behavior.

If you want more or fewer than 10 queries, you can easily change that:

SqlLogging.configuration.top_sql_queries = 5

If you want the sorted by something other than the total execution time, you can instead sort by the median execution time, number of executions or rows/bytes returned over all executions of the query:

SqlLogging.configuration.show_top_sql_queries = :median_time
SqlLogging.configuration.show_top_sql_queries = :queries
SqlLogging.configuration.show_top_sql_queries = :rows
SqlLogging.configuration.show_top_sql_queries = :bytes

You can also turn the list off entirely:

SqlLogging.configuration.show_top_sql_queries = false

If you don't want to see query backtraces:

SqlLogging.configuration.show_sql_backtrace = false

General Configuration

In addition to the statistics configuration, more general aspects of the gem can be set through the configuration.

For example, the logger can be set here to store results in alternate location:

SqlLogging.configuration.logger =

Additionally, the backtrace cleaner used for generating the call stack can be configured here:



Adam's post describing his logging improvements is at

Nathaniel's QueryTrace is available at


Feedback and/or patches to make this gem better are greatly appreciated!

Steve Madsen Light Year Software, LLC