<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: OPEYEMI OLUWAGBEMIGA</title>
    <description>The latest articles on DEV Community by OPEYEMI OLUWAGBEMIGA (@opeyemi_oluwagbemiga_a213).</description>
    <link>https://dev.to/opeyemi_oluwagbemiga_a213</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2923359%2Fa36090ca-25a9-42b0-951b-0376f52e11f9.png</url>
      <title>DEV Community: OPEYEMI OLUWAGBEMIGA</title>
      <link>https://dev.to/opeyemi_oluwagbemiga_a213</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/opeyemi_oluwagbemiga_a213"/>
    <language>en</language>
    <item>
      <title>Slapping Secondary Indexes on Random Fields is Silently Killing Your Database.</title>
      <dc:creator>OPEYEMI OLUWAGBEMIGA</dc:creator>
      <pubDate>Tue, 03 Mar 2026 06:17:52 +0000</pubDate>
      <link>https://dev.to/opeyemi_oluwagbemiga_a213/slapping-secondary-indexes-on-random-fields-is-silently-killing-your-database-15p3</link>
      <guid>https://dev.to/opeyemi_oluwagbemiga_a213/slapping-secondary-indexes-on-random-fields-is-silently-killing-your-database-15p3</guid>
      <description>&lt;p&gt;Why do we add indexes to our SQL fields? To make the search faster, right?&lt;/p&gt;

&lt;p&gt;But do you know it has a massive downside? Writes become slower, forcing developers to be strategic about which fields should be labelled as secondary indexes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsovz2dclat6sn45t9rss.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsovz2dclat6sn45t9rss.png" alt=" " width="800" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What is the magic behind indexes? Let's say we have a table of books called “book” with fields (id, title, author, pub_date, isbn).&lt;/p&gt;

&lt;p&gt;&lt;code&gt;SELECT * FROM book WHERE author=”C.S. Lewis”&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This causes the database engine to search through the Heap (a physical unstructured file where all data in the database actually lives). Let's say we have a total of 1 million rows in the table and only 20 Lewis books. The database engine would scan through all the 1 million rows, even when it had gotten the total of 20 Lewis books, just to be sure it didn’t miss any.&lt;/p&gt;

&lt;p&gt;This is a Full Table Scan with &lt;em&gt;O(n)&lt;/em&gt; complexity. It is brutally slow.&lt;/p&gt;

&lt;h2&gt;
  
  
  THE FIX
&lt;/h2&gt;

&lt;p&gt;When you add the field “author” as a secondary index&lt;/p&gt;

&lt;p&gt;&lt;code&gt;CREATE INDEX idx_author ON book(author);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The database engine creates a separate, highly organized B-Tree structure. It stores the Author names in alphabetical order, paired with a pointer (physical addresses) to where the full row actually lives.&lt;/p&gt;

&lt;p&gt;So instead of scanning through all 1 million rows, the database traverses through the B-Tree in &lt;em&gt;O(log n)&lt;/em&gt; time to get the 20 records and their pointers. It then uses the pointers to get the data it needs from the heap. That’s how secondary indexes make reads faster.&lt;/p&gt;

&lt;h2&gt;
  
  
  BUT WHY DO WRITES GET SLOWER?
&lt;/h2&gt;

&lt;p&gt;Adding or updating data no longer means just changing the heap data, but also the additional B-Tree created by the secondary indexes. So if you blindly add 5 secondary indexes to a table, every single &lt;code&gt;INSERT&lt;/code&gt; or &lt;code&gt;UPDATE&lt;/code&gt; means writing to the Heap plus updating 5 separate B-Trees on the disk.&lt;/p&gt;

&lt;p&gt;That is the hidden cost. As a backend developer, your job isn’t to index everything; instead, your job is to understand the tradeoff.&lt;/p&gt;

&lt;h2&gt;
  
  
  TIPS FOR SECONDARY INDEXES
&lt;/h2&gt;

&lt;p&gt;Do not index fields that are constantly updated, like page visits and view counts. This is to reduce the overhead of the database’s write performance.&lt;/p&gt;

&lt;p&gt;Only index fields that you actively use in your &lt;code&gt;WHERE&lt;/code&gt;, &lt;code&gt;JOIN&lt;/code&gt;, or &lt;code&gt;ORDER BY&lt;/code&gt; clauses.&lt;/p&gt;

</description>
      <category>database</category>
      <category>systemdesign</category>
      <category>postgres</category>
    </item>
  </channel>
</rss>
