How to use EXPLAIN ANALYZE in Rails 6

Category: Ruby :: Published at: 06.07.2025

As you might now, Rails 7 allows us to use explain with parameters.

From now on, we can use it for example with analyze: true flag, which gives us much more information about the query.

In Rails this option is not available, but we can use some tricks to show the result simply.

All we need to do is to run rails console and type:

sql = User.joins(:organisation).where(users: { organisations.id: 12 }).to_sql

ActiveRecord::Base.connection.execute("EXPLAIN ANALYZE #{sql}").to_a

This will give us much more information about our query.


- Click if you liked this article

Views: 776