Rails developers often faced challenges optimizing performance due to logs that lacked detailed SQL query information.
This made it difficult to identify specific performance bottlenecks, as the logs only provided general data on database interactions and view rendering times.
A recent update to the Rails framework, offers an insightful enhancement to how Rails logs SQL queries during template rendering.
This feature is particularly useful for developers who need to monitor SQL queries to optimize performance and debug issues efficiently.
Before
Prior to the implementation of this feature, Rails logs displayed basic metrics about the requests processed, including total time, view rendering time, and database interaction time. However, they lacked detailed information about the count and type of SQL queries executed.
Let’s take a code snippet to understand the behavior better
The typical log output for the above code looked something like this:
After
With the new change, Rails logs have become significantly more informative by including the count of SQL queries directly in the log output.
This addition helps developers quickly assess whether their code modifications have led to an increase or decrease in database queries, helping in performance optimization and debugging.
The enhanced log output for the same code will look like this:
This output not only shows the total time spent on ActiveRecord but also breaks down the number of queries executed and how many of them were served from the cache.
To know more about this feature, please refer to this PR