ActiveRecordQueryStats

Gem Version License: MIT Maintainability Test Coverage

ActiveRecordQueryStats produces simple ActiveRecord query stats at the end of each request in the following format:

Query Stats
-----------
total: 6, real: 5, cached: 1
select: 4, insert: 0, update: 1, delete: 0
transaction: 0, savepoint: 0, rollback: 0, lock: 0, other: 0
  • total: total queries occurred during the request.
  • real: queries having run against the database.
  • cached: queries that Rails encounters again and returns the cached result instead of hitting the database (see SQL Caching).
  • select: SELECT queries.
  • insert: INSERT queries.
  • update: UPDATE queries.
  • delete: DELETE queries.
  • transaction: TRANSACTION related queries.
  • savepoint: SAVEPOINT related queries.
  • lock: LOCK related queries.
  • rollback: ROLLBACK related queries.
  • other: the rest queries go here.

Install

Add the following line to file Gemfile.

gem 'active_record_query_stats', group: :development

And run re-bundle in terminal.

bundle install

That's it. Start the Rails server and see the stats!

Configuration

The query stats template can be customized by overwriting the translation for active_record_query_stats.stats_template, e.g.:

# config/locales/en.yml
en:
  # ...
  active_record_query_stats:
    stats_template: |
      Query Stats
      -----------
      total: %{total}, real: %{real}, cached: %{cached}
      select: %{select}, insert: %{insert}, update: %{update}, delete: %{delete}
      transaction: %{transaction}, savepoint: %{savepoint}, rollback: %{rollback}, lock: %{lock}, other: %{other}

Implementation

ActiveRecordQueryStats is based on the Active Support Instrumentation to implement the features by subscribing the following Rails events:

  • sql.active_record: collect and analyze the query executed by ActiveRecord from this event.
  • process_action.action_controller: display the stats when a request is finished.

Documentation

Want to contribute?

Raise an issue, discuss and resolve!

License

This project uses MIT License.