Rails 7.2 brings SQL queries count to template rendering logs

Navaneeth D

Navaneeth D

August 20, 2024

Rails 7.2 brings SQL queries count to template rendering logs

For Rails developers, debugging database queries is a frequent task. Whether it's addressing the notorious N+1 query problem or fine-tuning caching strategies, developers often find themselves diving into logs to scrutinize SQL query counts.

Traditionally, this involved manually inspecting the logs and counting the number of queries. Needless to say, this becomes tedious and error-prone for actions generating a significant number of queries in the order of tens or hundreds.

Thankfully, Rails 7.2 introduces a helpful improvement by enhancing the log output to include the query count alongside existing information.

sql-query-count-in-template-log

The improved log output now includes the query count within the ActiveRecord section. It shows 3 queries, 1 cached, indicating that three database queries were executed, with one being served from the cache.

This seemingly small addition allows for quick identification of query volume and potential optimization areas. You can easily see if caching is working effectively and if the number of queries aligns with expectations, saving developers valuable time and effort.

Check this pull request for more details:

If this blog was helpful, check out our full blog archive.

Stay up to date with our blogs.

Subscribe to receive email notifications for new blog posts.