Lecture: Lecture 16 - Physical Design
Related: Clustered and Unclustered Indexes, Physical Design Workload and Index Selection Heuristics

What a secondary index actually stores

An unclustered (secondary) index on a single column—say EMP.Dept#—does not store full employee rows. Each index entry stores only:

  1. The indexed key value (Dept#)
  2. A row identifier / pointer (RID) pointing to where the full tuple lives on a data page in EMP

Normally, answering a query with an unclustered index takes two phases: search the index to get matching RIDs, then follow those RIDs to read the actual table pages from disk (to inspect other columns like Age, EName, or Salary).

Covering indexes: skipping the table completely (Slides 37–39)

An index is a covering index (also called index-only access) when every column from a table that the query needs—in SELECT, WHERE, JOIN, or GROUP BY—is already present inside the index.

When an index covers a table:

  • The DBMS reads only the compact index entries.
  • It avoids reading the base table on disk altogether, eliminating random table page jumps.

Textbook analogy: Asking “How many times does the word SQL appear in this book?” can be answered directly from the back-of-the-book index (SQL: pp. 12, 45, 88, 102 → 4 times) without flipping into the book’s body pages (covering). Asking “How many times does SQL appear in bold?” forces you to visit each page in the book because bold formatting is not stored in the index (not covering).
Limit of the analogy: Unlike a static book index, a database index must be maintained on every table write—and adding extra columns to make an index cover a query increases both its disk size and write overhead.

Comparing Examples 7, 8, and 9 on EMP and DEPT (Slides 35–39)

Using EMP(Emp#, EName, Dept#, Salary, Age, Hobby) and DEPT(Dept#, DName, Mgr) (Slide 25):

Source Example 8 (Slides 37–38) — Covering index avoids EMP

SELECT Dept#, COUNT(*)
FROM EMP
GROUP BY Dept#;
  • Columns needed from EMP: only Dept#.
  • Result: An unclustered (secondary) index on EMP.Dept# is a covering index. The DBMS counts entries per Dept# straight from the index and never reads the EMP table.

Source Example 9 (Slide 39) — Covering index inside a join

SELECT Mgr
FROM DEPT, EMP
WHERE DEPT.Dept# = EMP.Dept#;
  • Columns needed from EMP: Notice Mgr comes from DEPT, not EMP. The only EMP attribute referenced anywhere in the query is EMP.Dept#.
  • Result: An unclustered index on EMP.Dept# covers the EMP side of the join, avoiding reading the EMP table completely.

Source Example 7 (Slides 35–36) — Why adding WHERE Age > 20 breaks single-column covering

SELECT Dept#, COUNT(*)
FROM EMP
WHERE Age > 20
GROUP BY Dept#;
  • Unselective filter: Age > 20 matches almost all employees, so do not build a B⁺-Tree on Age.
  • Single-column secondary index on Dept# fails to cover: Because Age is not in a Dept#-only index, the DBMS would have to jump randomly into EMP data pages for nearly every employee just to check Age > 20, causing too many disk reads.
  • Clustered index on Dept# works well: Physical rows (which carry Age) are already stored grouped by Dept#, so the DBMS scans contiguous blocks ready for GROUP BY without sorting.
  • Extension to composite covering: As noted during study, keeping the query unchanged while indexing <Dept#, Age> puts both needed columns inside one index—making it a covering index that avoids reading EMP, at the cost of extra update overhead whenever Dept# or Age changes.

Composite (multi-attribute) indexes and key order (Slides 16, 40–41)

When a WHERE clause filters on multiple simple predicates simultaneously (Slide 16), a single composite index on <Col1, Col2, ...> sorts entries by Col1 first, then by Col2 inside ties of Col1, and so on.

Think of a phonebook, which is a composite index on <LastName, FirstName>:

  • Looking up LastName = 'Rossi' AND FirstName BETWEEN 'A' AND 'G' jumps straight to Rossi and reads one contiguous range (A..G).
  • Looking up FirstName = 'Marco' without LastName cannot use the index order, because 'Marco' is scattered across every last name from A to Z.

Source Example 10 (Slides 40–41)

SELECT AVG(Salary)
FROM EMP
WHERE Age = 25
  AND Salary BETWEEN 3000 AND 5000;
  • Fastest solution: A single composite B⁺-Tree index on <Age, Salary>.
  • Why <Age, Salary> beats <Salary, Age>:
    • In <Age, Salary>, all rows with Age = 25 sit together, sorted by Salary. The DBMS jumps directly to Age = 25 and scans one tiny contiguous slice from 3000 to 5000.
    • In <Salary, Age>, entries are ordered by Salary first, so the DBMS must scan every salary between 3000 and 5000 across all ages (18, 19, …, 25, …, 65) and discard every non-25 entry.
  • Bonus covering property: <Age, Salary> already contains Salary, so AVG(Salary) is computed directly from the index without reading EMP.

Rule of thumb for composite key order: Place the equality (=) (and more selective) attribute first, and the range (BETWEEN, >, <) attribute second. Remember also that a composite index is one single data structure (e.g., one B⁺-Tree)—you do not mix hash on the first column and B⁺-Tree on the second inside one composite index.

Two tradeoffs of composite indexes (Slide 41)

  1. Key order limits reuse: <Age, Salary> helps queries on (Age) or (Age, Salary), but cannot efficiently serve a query filtering only on Salary.
  2. Update overhead grows: Any INSERT, DELETE, or UPDATE modifying any attribute in the composite key forces an index update (and composite indexes occupy more space, especially if extra SELECT columns are appended for covering).

Added study example: appending a SELECT column for covering aggregation

(Added example outside the PDF, following the same rules from Slides 38–41):

SELECT SUM(Total)
FROM ORDER
WHERE Cust# = 42
  AND OrderDate BETWEEN '2026-01-01' AND '2026-03-31';
  • Filtering order puts equality Cust# first and range OrderDate second inside a single B⁺-Tree.
  • Appending Total at the end gives <Cust#, OrderDate, Total>: Cust# and OrderDate locate the contiguous Q1 slice for customer 42, and Total sits right inside those index entries so SUM(Total) never touches the ORDER table.
  • However, <Cust#, OrderDate, Total> does not help a standalone query filtering only WHERE OrderDate BETWEEN '2026-01-01' AND '2026-03-31'; that query would need its own (ideally clustered) B⁺-Tree on OrderDate.

Common confusion

  • “Adding a column to GROUP BY vs. adding it to an index”: To cover WHERE Age > 20 GROUP BY Dept#, you do not add Age to GROUP BY (which changes the query result from counts per department to counts per (Dept#, Age) pair). You keep the SQL query untouched and include Age inside a composite index <Dept#, Age>.
  • Covering requires all referenced columns from that table: A single missing column in WHERE, SELECT, or JOIN forces a table lookup per matched row.

Retrieval questions

1. Why is an unclustered secondary index on EMP.Dept# effective for SELECT Dept#, COUNT(*) FROM EMP GROUP BY Dept#, yet risky for SELECT Dept#, COUNT(*) FROM EMP WHERE Age > 20 GROUP BY Dept#?
Without WHERE Age > 20, Dept# is the only column needed from EMP, so the unclustered index is a covering index and skips reading EMP completely. With WHERE Age > 20, Age is missing from a Dept#-only index, forcing random jumps into EMP for nearly every employee because Age > 20 is unselective.

2. In SELECT Mgr FROM DEPT, EMP WHERE DEPT.Dept# = EMP.Dept#, why does an unclustered index on EMP.Dept# avoid reading the EMP table?
Because Mgr comes from DEPT, so EMP.Dept# is the only column of EMP used anywhere in the query; the secondary index on EMP.Dept# covers the EMP table completely.

3. For SELECT AVG(Salary) FROM EMP WHERE Age = 25 AND Salary BETWEEN 3000 AND 5000, why is <Age, Salary> faster than <Salary, Age>, and why might a designer still hesitate to add <Age, Salary, ...> indexes everywhere?
<Age, Salary> places the equality predicate first so the DBMS jumps directly to Age = 25 and reads one contiguous salary slice 3000..5000 (while <Salary, Age> scans salaries 3000..5000 across every age). Adding wide or numerous composite indexes increases storage footprint and increases update overhead, because every write touching any indexed column must update the index.