Indexes Arenโ€™t Simple โ€” Theyโ€™re Strategic

April 25, 2026

๐™„ ๐™ช๐™จ๐™š๐™™ ๐™ฉ๐™ค ๐™ฉ๐™๐™ž๐™ฃ๐™  ๐™ž๐™ฃ๐™™๐™š๐™ญ๐™š๐™จ ๐™ž๐™ฃ ๐™™๐™–๐™ฉ๐™–๐™—๐™–๐™จ๐™š๐™จ ๐™ฌ๐™š๐™ง๐™š ๐™จ๐™ž๐™ข๐™ฅ๐™ก๐™š: ๐Ÿ‘‰ "๐™๐™๐™š๐™ฎ ๐™ž๐™ข๐™ฅ๐™ง๐™ค๐™ซ๐™š ๐™ง๐™š๐™–๐™™ ๐™ฅ๐™š๐™ง๐™›๐™ค๐™ง๐™ข๐™–๐™ฃ๐™˜๐™š" ๐Ÿ‘‰ "๐™๐™ค๐™ค ๐™ข๐™–๐™ฃ๐™ฎ ๐™ž๐™ฃ๐™™๐™š๐™ญ๐™š๐™จ ๐™จ๐™ก๐™ค๐™ฌ ๐™™๐™ค๐™ฌ๐™ฃ ๐™ฌ๐™ง๐™ž๐™ฉ๐™š๐™จ" Thatโ€™s itโ€ฆ or so I thought. While working on a recent feature, I realized thereโ€™s much more depth to how indexes actually work โ€” especially how you define them. One key thing I learned from my lead is that: The order of columns in an index matters A LOT.

Example:

CREATE INDEX idx_abc ON table(col_a, col_b, col_c); This works efficiently for:

โœ” WHERE col_a = ? โœ” WHERE col_a = ? AND col_b = ? โœ” WHERE col_a = ? AND col_b = ? AND col_c = ?

But surprisingly:

โŒ WHERE col_b = ? โŒ WHERE col_c = ?

๐Ÿ‘‰ Because indexes follow a left-to-right (prefix) rule.


Another mistake I used to overlook:

If most queries are like: WHERE col_a = ? AND col_b = ?

But your index is: (col_z, col_a, col_b)

๐Ÿ‘‰ That index might not be useful at all.


๐Ÿ’ก Big takeaway for me:

Indexes are not just about adding them. Theyโ€™re about designing them based on query patterns.

  • What columns are filtered most?
  • In what order are they used?
  • How selective are they?

Also learned: Every index comes with a tradeoff. More indexes = โฌ† Faster reads โฌ‡ Slower writes (INSERT / UPDATE / DELETE need to maintain them)

Still learning, but this completely changed how I think about performance.

Comments (0)