← Module 05 · Database & SQL
MODULE 05 · DATABASE & SQL · TOPIC 17

Index

A database index helps the database find rows faster without scanning every row.

01 · What is it?

A database index is an additional data structure that helps the database locate matching rows more efficiently.

It stores selected column values in an organized form and keeps a reference to the rows where those values appear.

Simple Definition: An index is a shortcut for finding rows in a table.

02 · Think of it like…

Without an index

You search every page of a phone book from the beginning.

The answer may be there, but you must inspect many entries first.

With an index

You use the alphabetized names to jump close to the answer.

You still verify the matching entry, but you skip most unrelated entries.

03 · What does it do?

An index can help the database:

Find Matching Rows
Sort Results
Support Uniqueness
Reduce Table Scans

The database optimizer decides whether an index is useful for a particular query.

04 · Where is it used?

Indexes are commonly used on columns that applications search, join, sort, or require to be unique.

Common candidates

User email, order status, customer ID, created date, or foreign keys.

Use judgment

An index takes storage and can make writes slower, so every column does not need one.

05 · Example

Suppose an orders table contains millions of rows and the application often finds one customer’s orders.

CREATE INDEX idx_orders_customer_id
ON orders (customer_id);

SELECT *
FROM orders
WHERE customer_id = 42;

The index gives the database a faster path to rows whose customer_id is 42. It does not change the result that the query should return.

06 · How it connects

SQL Query→Query Optimizer→Index or Table Scan→Matching Rows

An index is one possible access path. The optimizer chooses it only when its estimated cost is better for that query.

07 · Do Not Confuse

Index

A structure that helps locate rows faster. It can improve reads but adds storage and write work.

Table

The structure that stores the actual rows and columns. The table remains the source of the returned data.

Index ≠ a copy of the whole table. It is an access aid maintained alongside the table.

09 · What to Remember

An index is a shortcut that can help a database find rows without scanning every row.

Indexes can speed up reads, but they use storage and add work to writes.

The database optimizer chooses whether an index helps a particular query.

Index Key→Faster Access Path→Matching Rows
↑