Module 7 · Designing tables

Add indexes for your lookups

By the end you will be able to create an index for a search and check what it is for.

About 14 min

0 of 52 lessons

Before you start, you need

Modify tables with ALTER TABLE

Match an index to a search

In a book, an index helps you locate a topic without reading all the pages. A database index performs a similar function: it organises information from certain columns to make searching easier.

If you often look up orders by customer, you can create an index on customer_id. CREATE INDEX gives it a name and ON orders (customer_id) says where it is built. It does not add a new column or change the orders.

An index can help the engine read less data, but it takes up space and has to be maintained when the data changes. It does not make every query faster. In an index with several columns, their order also affects which searches it can help.

The lab checks the definition; it does not measure speed. The SQLite extension lets you see the search plan: SCAN means a scan and SEARCH means a targeted search, for example through an index. Check whether the engine uses the index for the query you care about.

Minimal syntax
CREATE INDEX name
ON table_name (column_name);

See it in action

We often look up a customer's orders. Which column drives that search?

SQL
CREATE INDEX idx_orders_customer
ON orders (customer_id);

Result in the lab: idx_orders_customer created.

  1. The index uses customer_id, the attribute you search by. It does not add a new attribute to each order.
  2. Creating the index does not change which orders the filter returns. It may change how the engine finds them.
  3. The lab checks the definition. To see a real plan, use the SQLite file in the extension.
  4. A composite index keeps the order of its columns. The variant declares customer_id before status for the requested pattern.

Practice with help

Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.

Guided practice

Prepare the search by customer

Not started

Write the SQL statement that creates the index idx_orders_customer on the customer_id column of orders.

The requested structure is checked.

Every attempt resets the data, so include all the steps you need to check in a single attempt.

View tables and data

Open this panel to see the full data.

Your turn

Solve this variation. Spot the part that has changed before you look for help.

Practice

Define a composite index

Not started

Create idx_orders_customer_status on orders with customer_id first and status second.

The requested structure is checked.

Every attempt resets the data, so include all the steps you need to check in a single attempt.

View tables and data

Open this panel to see the full data.

Key takeaways

  • Design the index around a specific search.
  • An index can reduce the work of reading.
  • Maintaining indexes has a cost on writes.
  • Check the plan to judge whether it is used.

Next: Practice: design a table with rules and an index

Going further with SQLite: download and run it

Compare SCAN and SEARCH before and after the index. SEARCH means a search supported by the index; on its own, it does not prove how much time you will save. The small table is for reading the plan, not for measuring a production workload.

Download the script with the database and ready-made queries. Open an empty SQLite database and import the script. In sqlite3 you can use .read indexes-plan-sqlite.sql. To run it again, import the file into another empty database.