Shakthivel Arumugam

Apr 19, 2025 • 1 min read

Magic of Indexes in Databases

Supercharging Your Queries

Magic of Indexes in Databases

Database indexes are like the cheatcode to query your table in databases.Like a well-organized book index that helps you quickly find specific information without reading every page, database indexes work silently behind the scenes to dramatically improve query performance.

What Are Database Indexes?

At their core, indexes are specialized data structures that store a small portion of a database's data in an easily searchable form. They create a shortcut that allows the database engine to find rows in a table without scanning the entire table.

Think of them as the difference between:

  • Searching for someone's phone number by flipping through every page of a phone book

  • Looking up their name in an alphabetically organized index

The Cost of Magic

Like all powerful tools, indexes come with tradeoffs:

  • Each index requires additional storage space

  • Writes to the database become slower as indexes must be updated

  • Too many indexes can actually decrease performance

Let's see how indexes improve the performance of a query

The above table has p_id,blog_owner and blog_cnt

Let's see the performance of the following query which return record which has blog_cnt=49

SELECT * FROM BLOG WHERE blog_cnt=49;

Performance without Index

Now,we apply index on the blog_cnt column

CREATE INDEX idx_blog_cnt ON blog (blog_cnt);

Now we run the same query

For getting the detailed analysis,use EXPLAIN ANALYZE before your queries

EXPLAIN ANALYZE

SELECT * FROM BLOG WHERE blog_cnt=49;

Performance with Index

Comparative Analysis between the two approaches

Join Shakthivel on Peerlist!

Join amazing folks like Shakthivel and thousands of other people in tech.

Create Profile

Join with Shakthivel’s personal invite link.

1

18

0