Skip to content
engineering workbench.by StudyPort Academy
Learning path
Advanced18 min

Composite and Covering Indexes: Beyond Single Columns

Understand the idea

Why column order in multi-column indexes makes or breaks query performance.

The idea

Creating a single-column on category is a great first step, but real-world queries often filter on multiple columns at once (WHERE category = 'gadgets' AND price > 10.00) or filter on one column while sorting by another.

A indexes two or more columns together in a single structure: CREATE INDEX idx_cat_price ON products(category, price). In this index, items are sorted first by category; within each category, items are sorted by price.

To use composite indexes effectively, you need to understand two key ideas:

  1. The Leftmost Prefix Rule: An index on (A, B, C) speeds up queries on (A), queries on (A, B), and queries on (A, B, C). But it CANNOT help a query that only filters on (B) or (C), because the index is organized by column A first! If a query ignores the leftmost column, the database falls back to a .
  2. : When an index includes every single column needed by a query (both in the SELECT and the WHERE clauses), the database answers the query directly from the index without needing to read the actual table rows from disk. This is called an index-only scan, and it is the fastest way to read data in relational databases.

What changes

Single vs Composite vs Covering Index
Index TypeDefinitionOptimal Use Case
Single-Column IndexIndexes one column: (category)Single equality or range filter on that exact column
Composite IndexIndexes multiple columns: (category, price)Filters and sorts referencing the leftmost prefix of columns
Covering IndexIndex contains ALL columns in SELECT and WHEREEliminates table page reads entirely (Index-Only Scan)

The command

Covering index avoiding table page lookups

3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.

The mistake

Check the result

Run the example to see SQLite report 'SEARCH products USING COVERING INDEX', confirming zero table pages were read from disk.

Guided challenge

Verify leftmost prefix compliance

Verify the function returns true when the query includes the leading index column and false when it does not.

Verify leftmost prefix compliance

3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.

EXPECTED RESULT

true
false

Hints 0/1

    Use all hints or make 3 failed checks (0/3).

    Free exploration

    Change this example and run it again. Each run is a new process. Nothing is saved.

    Your variation

    3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01What is the 'leftmost prefix rule' in composite database indexing?
    02What is a 'covering index' (index-only scan)?

    Ready to call this one understood?

    Mark this lesson complete to earn 200 XP. You can always revisit it.

    Sign in to save progress
    Next lesson