Example 1
Create an index on customer_id and verify the query still returns correct results
The index is transparent to query results — the SELECT returns the same rows as it would without the index. The benefit is speed: on large tables, the database uses the index to find customer_id = 1 rows without scanning every order. Use CREATE UNIQUE INDEX to also enforce uniqueness on the indexed column.
CREATE TABLE orders (id INT, customer_id INT, status VARCHAR(20));
INSERT INTO
orders (id, customer_id, status)
VALUES
(1, 1, 'pending'),
(2, 2, 'shipped'),
(3, 1, 'shipped');CREATE INDEX idx_orders_customer ON orders (customer_id);
SELECT
id,
customer_id,
status
FROM
orders
WHERE
customer_id = 1
ORDER BY
id;| id | customer_id | status |
|---|---|---|
| 1 | 1 | pending |
| 3 | 1 | shipped |
Output is identical across all engines.