Course contentsShow
← Databases & SQL
Lesson 45 of 1,9531. Relational FoundationsFree lesson

Managing Indexes for Retrieval

Apply indexing to optimize search performance without sacrificing write speed.

In this lesson, you will learn how to balance the speed of finding data with the performance cost of saving data by using indexes effectively.

The Indexing Trade-off

Think of a database table like a massive library without a catalog. If you want to find a book by a specific author, you have to walk down every aisle and check every spine—this is a "full table scan," and it is very slow.

An index is like the library’s card catalog. It keeps a sorted list of references (the author’s name and the book’s location) in a separate structure. When you search for that author, the database checks the small index first, finds the exact row location, and jumps straight there.

Performance Impacts

While indexes make SELECT queries lightning-fast, they are not "free." Every time you INSERT, UPDATE, or DELETE data, the database must not only update the table but also update every index associated with that table. If you create too many indexes, your write operations will become sluggish because the system is constantly "re-filing" its cards.

Practical Application

You create an index on columns you frequently use in WHERE clauses, JOIN conditions, or when ordering results. Here is how you define a simple index:

CREATE INDEX idx_customer_lastname 
ON customers(last_name);

Once this exists, any query filtering by last_name will use the index to find records instantly rather than scanning every single row in the customers table.

Strategy for Beginners

As you design your schema, don't index every single column. Focus on:

  1. Primary Keys: These are usually indexed automatically.
  2. Search Columns: Columns used frequently in WHERE clauses.
  3. Join Keys: Columns frequently used in JOIN conditions to speed up the connection between tables.

If you find that your INSERT statements are taking a long time, it may be because you have too many indexes. Always monitor your workload—if a column is rarely used for filtering, the overhead of maintaining an index on it is likely hurting your system more than it is helping.

Key Takeaway: Indexes act as a map for your database to find data quickly, but they require extra work from the system during updates; create them strategically to balance fast reads with efficient writes.