CodeBlanks Logo Icon CodeBlanks

When to index

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

query.sql
-- index on frequent column
CREATE INDEX

Sample solution

query.sql
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

Question
CREATE ____ idx_email ON customers (email);
Answer
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.

All lessons in Intermediate