<?xml version="1.0" encoding="utf-8"?>
    <feed xmlns="http://www.w3.org/2005/Atom">
     <title>BigBinary Blog</title>
     <link href="https://www.bigbinary.com/feed.xml" rel="self"/>
     <link href="https://www.bigbinary.com/"/>
     <updated>2026-10-05T12:06:36+00:00</updated>
     <id>https://www.bigbinary.com/</id>
     <entry>
       <title><![CDATA[Rails 7.2 adds SQL query counts to rendering logs]]></title>
       <author><name>Navaneeth D</name></author>
      <link href="https://www.bigbinary.com/blog/rails-8-adds-sql-queries-count-to-template-rendering-logs"/>
      <updated>2024-08-20T12:00:00+00:00</updated>
      <id>https://www.bigbinary.com/blog/rails-8-adds-sql-queries-count-to-template-rendering-logs</id>
      <content type="html"><![CDATA[<p>For Rails developers, debugging database queries is a frequent task. Whetherit's addressing the notorious N+1 query problem or fine-tuning cachingstrategies, developers often find themselves diving into logs to scrutinize SQLquery counts.</p><p>Traditionally, this involved manually inspecting the logs and counting thenumber of queries. Needless to say, this becomes tedious and error-prone foractions generating a significant number of queries in the order of tens orhundreds.</p><p>Thankfully, Rails 7.2 introduces a helpful improvement by enhancing the logoutput to include the query count alongside existing information.</p><p><img src="/blog/images/images_used_in_blog/2024/rails-8-adds-sql-queries-count-to-template-rendering-logs/sql-query-count-in-template-log.gif" alt="sql-query-count-in-template-log"></p><p>The improved log output now includes the query count within the ActiveRecordsection. It shows <code>3 queries, 1 cached</code>, indicating that three database querieswere executed, with one being served from the cache.</p><p>This seemingly small addition allows for quick identification of query volumeand potential optimization areas. You can easily see if caching is workingeffectively and if the number of queries aligns with expectations, savingdevelopers valuable time and effort.</p><p>Check <a href="https://github.com/rails/rails/pull/51457">this pull request</a> for moredetails:</p>]]></content>
    </entry><entry>
       <title><![CDATA[Rails 7.2 makes counter_cache integration safer and easier]]></title>
       <author><name>Navaneeth D</name></author>
      <link href="https://www.bigbinary.com/blog/rails-8-adds-ability-to-ignore-counter_cache-column-while-backfilling"/>
      <updated>2024-07-16T12:00:00+00:00</updated>
      <id>https://www.bigbinary.com/blog/rails-8-adds-ability-to-ignore-counter_cache-column-while-backfilling</id>
      <content type="html"><![CDATA[<p>Counter caches are a cornerstone of performance optimization in Railsapplications. They efficiently keep track of the number of associated recordsfor a model, eliminating the need for frequent database queries. However, addingcounter caches to existing applications, especially those with large tables,often can be challenging. Rails 7.2 brings an exciting update to address justthat!</p><h2>Counter cache integration challenges</h2><p>When introducing counter caches to large datasets, developers often encountertwo primary challenges:</p><ul><li>Backfilling data efficiently: Adding a counter cache column to an existingtable with a substantial amount of data can be problematic. Backfilling thecounter cache values separately from the column addition is necessary toprevent prolonged table locks, which can significantly impact applicationperformance. This process requires careful consideration to ensure dataintegrity while minimizing downtime and avoiding disruptions to userexperience.</li><li>Ensuring data consistency: Once the counter cache is in place, maintainingdata consistency becomes paramount. Methods such as <code>size</code>, <code>any?</code>, and othersthat utilize counter caches internally must return accurate results. However,during the backfilling process, relying solely on the counter cache mayproduce incorrect counts until all records are appropriately updated.</li></ul><h2>Safer counter cache implementation in Rails 7.2</h2><p>The new update in Rails 7.2 introduces a feature that allows developers tomanage counter caches more effectively, especially in scenarios involvingexisting large datasets. By introducing the <code>active</code> option in the counter cacheconfiguration, developers can control when the counter cache is activelyutilized. This enables them to backfill counter cache columns separately fromtheir addition, minimizing table locks and potential performance issues. Oncethe backfilling process is complete, developers can activate the counter cache,ensuring accurate association counts without compromising applicationperformance.</p><p>Let's illustrate the implementation of this update in Rails using astraightforward example involving a blog application with articles and comments.In our blog application, each article can have multiple comments. We want to adda counter cache to track the number of comments associated with each article.However, our database already contains a significant amount of data, makingtraditional counter cache implementation challenging.</p><h3>Implementation Steps</h3><ol><li>Define the association: Initially, we define the association between the<code>Article</code> model and the <code>Comment</code> model, specifying the counter cache withthe <code>active: false</code> option to keep it inactive during the initial setup.</li></ol><pre><code class="language-ruby">class Comment &lt; ApplicationRecord  belongs_to :article, counter_cache: { active: false }end</code></pre><ol start="2"><li><p>Backfill the Counter Cache: With the association configured, we proceed tobackfill the counter cache column in the <code>articles</code> table. During this phase,the counter cache remains inactive, and methods like <code>size</code>, <code>any?</code>, etc.,retrieve results <strong>directly from the database</strong>. This prevents incorrectvalues from getting displayed during backfilling.</p></li><li><p>Activate the Counter Cache: Once the backfilling process is complete, weactivate the counter cache by removing the <code>active: false</code> option from thecounter cache definition.</p></li></ol><pre><code class="language-ruby">class Comment &lt; ApplicationRecord  belongs_to :article, counter_cache: trueend</code></pre><p>Upon activation, the counter cache integrates into the association, efficientlytracking the number of comments associated with each article.</p><p>This <a href="https://github.com/rails/rails/pull/51453">PR introduced</a> active option.Checkout full feature discussion<a href="https://discuss.rubyonrails.org/t/new-feature-to-make-introducing-counter-caches-safer-and-easier/85456">here</a>.</p>]]></content>
    </entry>
     </feed>