TExcelTAD
Home › SQL › SQL INDEX

SQL INDEX

An index is a separate, sorted data structure that helps the database find rows quickly — like the index at the back of a book. Without one, a query like WHERE email = '…' must read every row (a full table scan). With an index on email, the database jumps straight to the matching entry.

sql
CREATE INDEX index_name ON table_name (column1, column2, ...);
CREATE UNIQUE INDEX index_name ON table_name (column);
DROP INDEX index_name;

Indexes do not change query results — only how fast they are produced.

Creating an index

sql · try it yourself
CREATE INDEX idx_orders_customer ON orders (customer_id);

SELECT name, tbl_name FROM sqlite_schema WHERE type = 'index' AND name LIKE 'idx%';
name                | tbl_name
--------------------+---------
idx_orders_customer | orders
(1 row)

Seeing whether an index is used

EXPLAIN QUERY PLAN shows how SQLite intends to run a query. Compare before and after creating the index:

sql · try it yourself
EXPLAIN QUERY PLAN
SELECT * FROM orders WHERE customer_id = 17;

CREATE INDEX idx_orders_customer ON orders (customer_id);

EXPLAIN QUERY PLAN
SELECT * FROM orders WHERE customer_id = 17;
id | parent | notused | detail
---+--------+---------+------------
2  | 0      | 216     | SCAN orders
(1 row)

id | parent | notused | detail
---+--------+---------+--------------------------------------------------------------
3  | 0      | 61      | SEARCH orders USING INDEX idx_orders_customer (customer_id=?)
(1 row)

Look at the detail column: SCAN orders reads the whole table; SEARCH orders USING INDEX jumps into the index. (MySQL and PostgreSQL use EXPLAIN, SQL Server shows an execution plan.)

Unique indexes

A unique index both speeds up lookups and forbids duplicates — it is how UNIQUE constraints are implemented:

sql · try it yourself
CREATE UNIQUE INDEX idx_customers_email ON customers (email);

INSERT INTO customers (name, email) VALUES ('Copycat', 'ava.nguyen@example.com');
Error: UNIQUE constraint failed: customers.email

Multi-column indexes

An index on (status, order_date) is sorted by status, then by date within each status. It helps queries that filter on status, or on status and order_date — but not on order_date alone, just as a phone book sorted by last name does not help you find everyone named "Anna".

sql · try it yourself
CREATE INDEX idx_orders_status_date ON orders (status, order_date);

EXPLAIN QUERY PLAN
SELECT order_id FROM orders WHERE status = 'processing' AND order_date >= '2025-10-01';

EXPLAIN QUERY PLAN
SELECT order_id FROM orders WHERE order_date >= '2025-10-01';
id | parent | notused | detail
---+--------+---------+--------------------------------------------------------------------------------------
2  | 0      | 47      | SEARCH orders USING COVERING INDEX idx_orders_status_date (status=? AND order_date>?)
(1 row)

id | parent | notused | detail
---+--------+---------+--------------------------------------------------------
2  | 0      | 214     | SCAN orders USING COVERING INDEX idx_orders_status_date
(1 row)

The first query can SEARCH the index. The second can only SCAN it from start to end — SQLite reads the index instead of the table here only because it happens to contain every column the query needs (a covering index).

Costs of indexes

Benefit Cost
Fast WHERE, JOIN, ORDER BY on the indexed columns Extra storage
Enforces uniqueness (unique indexes) Every INSERT, UPDATE, DELETE must update the index too

Good candidates for indexes:

  • foreign key columns used in joins (orders.customer_id, order_items.order_id)
  • columns frequently searched by exact value (email, sku)
  • columns used to sort large results

Primary keys and UNIQUE columns are indexed automatically.

Dropping an index

sql · try it yourself
CREATE INDEX idx_products_price ON products (price);
DROP INDEX idx_products_price;

SELECT COUNT(*) AS remaining FROM sqlite_schema WHERE name = 'idx_products_price';
remaining
---------
0
(1 row)
Database Drop syntax
SQLite, PostgreSQL, Oracle DROP INDEX idx_name
MySQL DROP INDEX idx_name ON table or ALTER TABLE t DROP INDEX idx_name
SQL Server DROP INDEX table.idx_name

Test yourself

Does adding an index change the rows a query returns?

  1. Yes
  2. No, only how fast they are found
  3. Only for unique indexes
  4. Only for ORDER BY

An index on (status, order_date) is most useful for…

  1. WHERE order_date = …
  2. WHERE status = … AND order_date >= …
  3. WHERE customer_id = …
  4. SELECT COUNT(*)

What is a downside of many indexes?

  1. Queries return wrong data
  2. Writes become slower and more storage is used
  3. Tables cannot be dropped
  4. Joins are impossible