SQLite CREATE INDEX Statement
What is an Index?
An index helps SQLite find records quickly, like the index at the back of a book. Without an index, SQLite must read every record in the table to find the ones you want.
Indexes make searches faster, but inserts and updates a little slower, because the index must be updated too. Create indexes for the columns you often search in.
CREATE INDEX
CREATE INDEX index_name
ON table_name (column1, column2, ...);
Example
Create an index on the City column of the Customers table:
CREATE INDEX idx_city
ON Customers (City);
Try it Yourself »
See If a Query Uses an Index
EXPLAIN QUERY PLAN shows how SQLite will run a query. Without an index, it shows SCAN, which means that every record is read:
With an index, it shows SEARCH ... USING INDEX:
Example
CREATE INDEX IF NOT EXISTS idx_city
ON Customers (City);
EXPLAIN QUERY PLAN
SELECT * FROM Customers
WHERE City = 'London';
Try it Yourself »
UNIQUE Index
A unique index also makes sure that all the values are different:
Example
CREATE UNIQUE INDEX IF NOT EXISTS idx_productname
ON Products (ProductName);
Try it Yourself »
List the Indexes
In a new Northwind database there are no indexes yet, so the result is empty until you create one.
DROP INDEX
DROP INDEX index_name;