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:
- The indexed key value (
Dept#) - 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
SQLappear 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 doesSQLappear 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: onlyDept#. - Result: An unclustered (secondary) index on
EMP.Dept#is a covering index. The DBMS counts entries perDept#straight from the index and never reads theEMPtable.
Source Example 9 (Slide 39) — Covering index inside a join
SELECT Mgr
FROM DEPT, EMP
WHERE DEPT.Dept# = EMP.Dept#;- Columns needed from
EMP: NoticeMgrcomes fromDEPT, notEMP. The onlyEMPattribute referenced anywhere in the query isEMP.Dept#. - Result: An unclustered index on
EMP.Dept#covers theEMPside of the join, avoiding reading theEMPtable 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 > 20matches almost all employees, so do not build a B⁺-Tree onAge. - Single-column secondary index on
Dept#fails to cover: BecauseAgeis not in aDept#-only index, the DBMS would have to jump randomly intoEMPdata pages for nearly every employee just to checkAge > 20, causing too many disk reads. - Clustered index on
Dept#works well: Physical rows (which carryAge) are already stored grouped byDept#, so the DBMS scans contiguous blocks ready forGROUP BYwithout 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 readingEMP, at the cost of extra update overhead wheneverDept#orAgechanges.
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 toRossiand reads one contiguous range (A..G). - Looking up
FirstName = 'Marco'withoutLastNamecannot 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 withAge = 25sit together, sorted bySalary. The DBMS jumps directly toAge = 25and scans one tiny contiguous slice from3000to5000. - In
<Salary, Age>, entries are ordered bySalaryfirst, so the DBMS must scan every salary between3000and5000across all ages (18, 19, …, 25, …, 65) and discard every non-25 entry.
- In
- Bonus covering property:
<Age, Salary>already containsSalary, soAVG(Salary)is computed directly from the index without readingEMP.
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)
- Key order limits reuse:
<Age, Salary>helps queries on(Age)or(Age, Salary), but cannot efficiently serve a query filtering only onSalary. - Update overhead grows: Any
INSERT,DELETE, orUPDATEmodifying any attribute in the composite key forces an index update (and composite indexes occupy more space, especially if extraSELECTcolumns 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 rangeOrderDatesecond inside a single B⁺-Tree. - Appending
Totalat the end gives<Cust#, OrderDate, Total>:Cust#andOrderDatelocate the contiguous Q1 slice for customer 42, andTotalsits right inside those index entries soSUM(Total)never touches theORDERtable. - However,
<Cust#, OrderDate, Total>does not help a standalone query filtering onlyWHERE OrderDate BETWEEN '2026-01-01' AND '2026-03-31'; that query would need its own (ideally clustered) B⁺-Tree onOrderDate.
Common confusion
- “Adding a column to
GROUP BYvs. adding it to an index”: To coverWHERE Age > 20 GROUP BY Dept#, you do not addAgetoGROUP BY(which changes the query result from counts per department to counts per(Dept#, Age)pair). You keep the SQL query untouched and includeAgeinside a composite index<Dept#, Age>. - Covering requires all referenced columns from that table: A single missing column in
WHERE,SELECT, orJOINforces 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.