When optimizing database queries in Rails, it’s essential to understand how the database plans to execute a query. Rails provides a built-in method, ActiveRecord::Relation#explain
, to analyze and display a query’s execution plan. The output mimics the format of a database shell, offering valuable insights into query performance and potential bottlenecks.
Background
Before Rails 7.1, the explain
method provided basic query execution plans. Rails 7.1 introduced options like analyze
and verbose
to offer deeper insights into query performance. You can learn more about this in our blog post.
With Rails 7.2, the feature has been further enhanced. The explain
method now supports pluck
, count
, first
, and other methods directly on an ActiveRecord::Relation
, making it even more powerful and user-friendly.
What Is ActiveRecord::Relation#explain
?
The explain
method runs the database’s EXPLAIN
command on the query triggered by the relation and returns the result. This allows us to:
- See how the database will execute the query.
- Understand query efficiency.
- Identify and address performance issues.
Before:
Prior to Rails 7.2, we could use explain
like this:
While explain worked well for basic queries, it failed when used with relation methods like count
, pluck
, or last
. For example:
After
Rails 7.2 adds explain support to ActiveRecord::Relation.
Now we can use explain
method on ActiveRecord::Relation
. The object returned by explain now responds to pluck
, first
, last
, average
, count
, maximum
, minimum
, and sum
.