QR CookingNotes

CookingNotes

Your Personal Recipe Book

Get it on Google Play
QR FiNoteMe

FiNoteMe

Smart Finance Tracker

Get it on Google Play
database Database indexing is the secret to making your queries fast and efficient, especially on large tables. Indexes act like the index of a book, helping the database find information quickly without scanning every row. In this tutorial, you’ll learn what indexes are, why they matter, and how to create them in a way that's beginner-safe and easy to understand.

Understanding Database Indexing (Beginner-Friendly Guide)

5 Min Read Verified Content

Let’s be honest:
The word “indexing” scares many beginners.
But indexing is extremely simple once someone explains it the right way.

So let’s do exactly that.




What Is an Index (Explained Like You're 10 Years Old)

Imagine a book with 1,000 pages.
If you want to find “Chapter: Networking Basics,” you don’t flip page by page.

You go to the index at the back.
It tells you:

Networking Basics .......... page 213

Instant access.

A database index works exactly like that.




Why Indexes Matter


Indexes help the database:

  • find data faster

  • avoid scanning the entire table

  • speed up WHERE queries

  • speed up JOINs

  • improve performance for large datasets

If your table has 1,000,000 rows, indexing becomes critical.



When Should You Create an Index?


You should index columns that are used in:

WHERE
JOIN
ORDER BY
GROUP BY
✔ Columns frequently searched


Examples:

  • user email

  • username

  • order_date

  • product_id

  • customer_id



How to Create an Index


In SQL:

CREATE INDEX idx_users_email ON users(email);

This creates an index on email.

You can drop it with:

DROP INDEX idx_users_email;

In MySQL, sometimes you use:

ALTER TABLE users ADD INDEX (email);


Composite Index (Index With Multiple Columns)


If your query looks like this:

SELECT * FROM orders WHERE customer_id = 5 AND product_id = 10;

Create an index like:

CREATE INDEX idx_orders_customer_product ON orders(customer_id, product_id);

The order matters — always put the most selective column first.




Important Warning for Beginners: Indexes Are Not Free

Indexes speed up reads
but
indexes slow down writes (INSERT/UPDATE/DELETE).

Why?

Because the database must update the index every time data changes.

So rule of thumb:

👉 Don’t index everything — only index what you actually query often.




How to Check If Your Query Uses an Index


In MySQL:

EXPLAIN SELECT * FROM users WHERE email = 'hello@example.com';

In PostgreSQL:

EXPLAIN ANALYZE SELECT * FROM users WHERE email = 'hello@example.com';

If you see “index scan” → great!
If you see “full table scan” → you need an index.




Real-Life Example


Without index:

  • Table: users

  • Rows: 500,000

  • Query:

  • SELECT * FROM users WHERE email = 'john@mail.com'
  • Database checks all 500,000 rows.

This is slow.


With index on email:

  • Database jumps straight to the matching row.


  • Fast, efficient, instant.



Conclusion: What You Should Remember


  • Indexes make your queries MUCH faster.

  • Indexes work like the index of a book.

  • Use indexes on columns frequently used in WHERE and JOIN.

  • Don’t over-index or your writes will slow down.

  • Learn to use EXPLAIN to understand performance.

Advertisement
Back to Database