Before you start, you need
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.
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?
CREATE INDEX idx_orders_customer
ON orders (customer_id);Result in the lab: idx_orders_customer created.
- The index uses customer_id, the attribute you search by. It does not add a new attribute to each order.
- Creating the index does not change which orders the filter returns. It may change how the engine finds them.
- The lab checks the definition. To see a real plan, use the SQLite file in the extension.
- 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
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.
The search starts from the customer's id, stored in customer_id.
Give the index a descriptive name.
Measure before and after in a real environment.
Solution explained
CREATE INDEX idx_orders_customer ON orders (customer_id);customer_id is part of the relationship with customers and can be a reasonable candidate.
Your turn
Solve this variation. Spot the part that has changed before you look for help.
Practice
Define a composite index
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.
Think about the search's first criterion.
The index keeps the order of its columns.
The list is customer_id, status.
Solution explained
CREATE INDEX idx_orders_customer_status ON orders (customer_id, status);The column order suits searches by customer followed by status. Whether it actually helps is checked in the prepared SQLite plan.
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.
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.