When to index
- SQL course
- Intermediate
- Lesson 81/115
- 4 min
Choose which columns to index.
When to index?
Indexes go on frequently queried columns, not every column. Low-repetition values are good for indexing.
CREATE INDEX idx_email ON customers (email);Email is ideal for indexing as it is unique.
The index idx_email is created.
When to index? — details
Primary keys and UNIQUE columns are automatically indexed. They need no extra index.
Add indexes to columns used often in WHERE, JOIN and ORDER BY that have many distinct values; low-variety columns (e.g. gender) gain little.
Practice When to index?
Add an index on email (frequently searched).
Starter code
-- index on frequent column
CREATE INDEXSample solution
CREATE INDEX idx_email ON customers (email);Expected output
İndeks oluşturuldu
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Add CREATE INDEX
CREATE ____ idx_email ON customers (email);
CREATE INDEX idx_email ON customers (email);
Explanation: CREATE INDEX idx_email speeds up lookups by email.
2 When to index: match the terms
| Term | Meaning |
|---|---|
frequent query |
index |
unique |
suitable |
primary key |
automatic |
low repeat |
fit |
3 Which column is auto-indexed?
- A date
- Last name
- Description
- The primary key Correct answer
Explanation: Primary keys are auto-indexed.