---
title: "Optimizing Database Performance in Rails"
description: "Production database performance in Rails can degrade as application and data volumes grow, with slow queries, unnecessary queries, and other database bottlenecks becoming significant constraints. The post recommends establishing monitoring early, using Heroku Postgres events, data.heroku.com diagnostics, and the pg-extras pg:outliers command to locate expensive queries, then checking Rails access patterns such as unnecessary association loads. It shows how ActiveRecord includes can reduce a collection example from 11 queries to two, and explains when indexes may help, including Rails migrations for indexed columns and foreign keys, while noting storage and write costs. It also presents fragment caching with Redis as a further optimization and points to connection leaks, locks, and VACUUM as additional areas to investigate, urging benchmarking and metrics for every change."
---

# Optimizing Database Performance in Rails

[Heroku](https://yomu.fyi/company/heroku) · Jonan Scheffler · Apr 15, 2019

**Type:** Tutorial

## Summary

Production database performance in Rails can degrade as application and data volumes grow, with slow queries, unnecessary queries, and other database bottlenecks becoming significant constraints. The post recommends establishing monitoring early, using Heroku Postgres events, data.heroku.com diagnostics, and the pg-extras pg:outliers command to locate expensive queries, then checking Rails access patterns such as unnecessary association loads. It shows how ActiveRecord includes can reduce a collection example from 11 queries to two, and explains when indexes may help, including Rails migrations for indexed columns and foreign keys, while noting storage and write costs. It also presents fragment caching with Redis as a further optimization and points to connection leaks, locks, and VACUUM as additional areas to investigate, urging benchmarking and metrics for every change.

## Context

As a Rails application and its data grow, database-related performance bottlenecks can affect production. The post focuses on diagnosing slow or excessive queries and identifying other database issues through monitoring and performance investigation.

## Approach / What changed

The post recommends monitoring database behavior, locating expensive queries with Heroku Postgres tools and pg-extras, reducing unnecessary ActiveRecord queries with includes, adding indexes when benchmarking supports them, and using fragment caching. It also identifies connection leaks, database locks, and VACUUM management as further investigation areas.

## Takeaways

- Heroku Postgres Standard and Premium databases expose logstream events for database size, active connections, and cache hit rate; pg:outliers identifies long-running queries.
- Using ActiveRecord includes for associated records reduces the example from 11 queries to two by fetching clients and addresses in separate bulk queries.
- Indexes can speed lookups but consume disk space and add work to writes, so the post recommends testing locally and benchmarking before adopting them.

**Tags:** [Caching](https://yomu.fyi/topic/caching), [Postgres](https://yomu.fyi/topic/postgres), [Ruby on Rails](https://yomu.fyi/topic/ruby-on-rails)

- Source: [Heroku](https://www.heroku.com/blog/rails-database-optimization)
- Source URL: https://www.heroku.com/blog/rails-database-optimization
- Ingested by Yomu: 2026-09-09T14:27:26.046Z

[Read original post](https://www.heroku.com/blog/rails-database-optimization)
