Skip to article content
FrontendXLesson 13 / 24

Indexing and reading query plans

Build a Node HTTP API from scratch, model data in SQL and MongoDB, and reason about transactions, indexes, caching and authentication so the endpoint you ship stays correct under load.

advanced · about 60 minutes · 2 exercises

  • Explain
  • Implement

What you will be able to do

#
  • Explain how a B-tree index turns a scan into a seek.
  • Use EXPLAIN to confirm whether a query uses an index.
  • Design a composite index for a known filter and sort.

Overview

#

An index is a sorted data structure that lets the database find rows without reading the whole table. The default is a B-tree, which supports equality, range and ordered access.

When an index helps

A query that filters or sorts on indexed columns can seek to the matching range. A query with no usable index performs a sequential scan.

snippet.sql
CREATE INDEX idx_articles_status_created
  ON articles (status, created_at DESC);

EXPLAIN ANALYZE
SELECT id FROM articles
WHERE status = 'published'
ORDER BY created_at DESC
LIMIT 20;

A composite index follows the leftmost prefix rule: it can serve status, or status plus created_at, but not created_at alone. A covering index includes every column the query needs, so the database answers from the index without touching the table.

The write cost

Every index must be updated on insert, update and delete. Many indexes slow writes and consume storage, so add indexes for real query patterns, not speculatively.

Common pitfalls

  • Wrapping an indexed column in a function, for example WHERE lower(email) = ..., which disables the plain index.
  • Creating a separate index per column instead of one composite index for the actual filter and sort.
  • Assuming an index is used without checking the plan.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Implement

    Write the composite index for a query that filters status = 'published' and orders by created_at DESC, then limits 20.

    starter.sql
    -- articles(id, status, created_at, title)
    

    Reveal the reference answer
    reference.sql
    CREATE INDEX idx_articles_status_created
      ON articles (status, created_at DESC);

    Authored expected output: One composite index on (status, created_at DESC).

    Equality on status first, then the sort column, lets the index satisfy both the filter and the order so the limit reads only the needed rows.

  2. Exercise 2Explain

    This query ignores an index on email. Explain why and how to fix it.

    starter.text
    SELECT id FROM users WHERE lower(email) = lower($1);

    Reveal the reference answer
    reference.text
    Wrapping email in lower() means the index on email cannot be used, because the index stores the raw value. Either store a normalized lowercase column with its own index, or create an expression index: CREATE INDEX ON users (lower(email)).

    Authored expected output: Explains the function-around-column problem and offers a normalized column or an expression index.

    A plain index on a column is only usable when the predicate references the column directly, not a function of it.

Common misconceptions

#
  • More indexes are always faster: each index taxes every write and can be ignored by the planner.
  • An index on each column helps any query: composite queries need a composite index in the right column order.

Follow-up practice

#
  • backend-databases-migrations

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.