When we start building a web application, everything often works perfectly. Pages load quickly, database queries return results, and users can complete their tasks without any trouble.
But as the application grows, things can change.
A dashboard that once loaded in a second might suddenly take five seconds. A page displaying customer records becomes slower as the database grows. Users click a button and have to wait longer than expected.
The real problem is not always the amount of data. Sometimes, it is how our application retrieves and processes that data.
As a web developer, I believe understanding these problems and finding practical solutions is an important part of building reliable applications.
Let's look at a common Ruby on Rails performance problem and how we can solve it.
The Problem: Too Many Database Queries
Imagine we are building a Ruby on Rails application for managing customers and their orders.
Each customer has multiple orders, and the dashboard displays a list of customers along with their order counts.
At first, the application works fine because the database contains only a few customers.
However, as the business grows, the page starts loading slowly.
Why?
One possible reason is the N+1 query problem.
Consider this example:
@customers = Customer.limit(100)
@customers.each do |customer|
puts customer.orders.count
end
At first glance, this code looks simple. We retrieve 100 customers and then count the orders for each customer.
But Rails may execute one query to retrieve the customers and additional queries to count their orders individually.
That means we could end up executing approximately 101 database queries for a single operation.
If this pattern appears across several parts of a dashboard, the performance impact can become noticeable.
The problem becomes more serious when multiple users access the application simultaneously.
Why Does This Happen?
Active Record makes database interactions convenient, but convenience can sometimes hide the number of queries being executed.
When we access an association such as customer.orders, Rails may need to retrieve that data from the database.
If we repeat this operation for many records without considering how the data is loaded, we can create unnecessary database traffic.
The main issues are:
- Repeated database queries for related records.
- Increased database workload.
- Longer response times.
- Poor performance as the number of records increases.
- A frustrating experience for users.
Simply adding more server resources may not fix the underlying problem. We should first understand what the application is doing.
Solution One: Use Eager Loading
One common solution is eager loading.
Instead of retrieving associated records separately for each customer, we can use Active Record's includes method to load the required associations more efficiently.
For example:
@customers = Customer
.includes(:orders)
.limit(100)
Now, when we access the customers and their associated orders, Rails can retrieve the necessary data using a small number of queries rather than issuing a separate association query for every customer.
However, there is an important detail.
If we only need the number of orders, loading every order object may not be the most efficient approach. The best solution depends on what the page actually needs.
For a dashboard that only displays order counts, we should consider calculating those counts more efficiently.
Solution Two: Use a Database-Level Count
If the application only needs to display the number of orders per customer, we can use a database-level aggregation.
For example, with a suitable SQL query:
SELECT customers.id,
customers.name,
COUNT(orders.id) AS orders_count
FROM customers
LEFT JOIN orders
ON orders.customer_id = customers.id
GROUP BY customers.id, customers.name;
This allows the database to calculate the order counts instead of loading every order into Ruby just to count them.
For an application using Active Record, we can also explore methods such as left_joins and group to express this query through Rails.
The important idea is to retrieve only the information we need.
Less unnecessary data processing can mean better performance and lower resource usage.
Solution Three: Add Database Indexes Where Needed
Another possible issue is slow database lookups.
Suppose an application frequently searches orders by customer_id. If the table contains many records and the relevant column lacks an appropriate index, the database may need to examine many rows to find matching orders.
A migration could add an index like this:
class AddIndexToOrdersCustomerId < ActiveRecord::Migration[7.1]
def change
add_index :orders, :customer_id
end
end
The migration version should match the Rails version used by the project.
An index can improve certain queries, but it is not a magic fix for every performance issue. Indexes consume storage and can add overhead to inserts and updates.
Before adding one, examine the queries the application actually runs and use the database's query-analysis tools to understand where an index would help.
Solution Four: Measure Before and After
One mistake developers can make is optimizing code based on assumptions.
We might change a query, add an index, or rewrite a method without checking whether the change actually improves performance.
A better approach is to measure the application before and after the change.
In Rails, we can inspect development logs to see how many database queries a request executes and how much time those queries take.
We can also use tools such as the Bullet gem to help identify common inefficient association-loading patterns.
For database-specific investigation, tools such as EXPLAIN and EXPLAIN ANALYZE in PostgreSQL can help us understand how a query is executed.
A practical workflow looks like this:
- Reproduce the slow page or operation.
- Inspect the application logs.
- Identify repeated or expensive queries.
- Apply one targeted improvement.
- Run the same operation again.
- Compare query counts and response times.
- Verify that the output is still correct.
This approach helps us avoid unnecessary changes and understand which improvements actually matter.
Solution Five: Test the Fix
Performance improvements should not introduce functional bugs.
For example, if we change how customer orders are loaded or counted, we should verify that customers with no orders still display the correct count.
We should also check whether the result remains correct when a customer has multiple orders.
Using RSpec, we can write tests for these scenarios and ensure the application's behavior remains consistent.
We can also test that the dashboard displays the correct information after the optimization.
The goal is not only to make the application faster. It is to make it faster without breaking existing functionality.
What Should We Learn From This Problem?
A slow application does not always need a complete rewrite, a new framework, or a bigger server.
Sometimes, a small change in how we retrieve data can make a meaningful difference.
When working with Ruby on Rails, it is useful to understand how Active Record handles associations, how databases execute queries, and how application code affects performance.
These skills become especially important when a project grows beyond its original requirements.
I also believe developers should avoid optimizing everything too early. First, build a solution that works. Then measure its behavior, identify the bottlenecks, and improve the areas where there is clear evidence of a problem.
Final Thoughts
Performance problems are a normal part of developing real-world applications. What matters is how we investigate and solve them.
The N+1 query problem is just one example of how a simple piece of code can create unnecessary work as an application grows.
By using eager loading appropriately, choosing efficient database queries, adding indexes when justified, and measuring performance carefully, we can build Ruby on Rails applications that remain responsive and maintainable.
Good development is not just about writing code that works. It is about understanding why something becomes a problem and finding a solution that makes sense.
Have you ever faced a slow database query or an N+1 query problem in a Rails application? What approach worked best for you?
I'd love to hear your experience.