Lecture: Lecture 16 - Physical Design
Related: Physical Access Structures and Access Methods, Clustered and Unclustered Indexes, Query Cost Estimation and Access Selection, Join Algorithms and Execution Plan Selection

Where physical design fits

Database design progresses through three phases (Slides 2–3):

  1. Conceptual design produces a conceptual schema (ER or UML diagram) capturing entities and relationships.
  2. Logical design translates that model into relational tables (the logical schema).
  3. Physical design decides how those tables are stored on disk and which access structures (indexes) are built so applications perform well.

Two properties define physical design (Slide 3):

  • Data independence: Changing physical storage structures or adding/removing indexes must not affect application software or require rewriting SQL.
  • DBMS dependence: Unlike conceptual and logical design, physical design requires choosing the target DBMS product (such as Oracle or PostgreSQL) because different systems provide different storage structures and access techniques.

Inputs, outputs, and selection dimensions

Before choosing indexes or file layouts, the designer gathers three inputs (Slides 4, 8):

  • The logical schema of the database.
  • Features of the selected DBMS product (available index types, page clustering support).
  • The workload, characterized by:
    • Frequent queries: accessed tables, visualized (output) attributes, attributes used in selections (WHERE) and joins, selectivity of selections, and estimated frequency.
    • Update operations: tables and attributes involved in selections, selection selectivity, update type (INSERT, DELETE, UPDATE), updated attributes, and estimated frequency.
    • Required performance targets for critical queries and updates.

The outputs are the physical schema (table file organization and indexes) plus storage and DBMS configuration parameters—such as initial file size, extensions, initial free space, buffer size, and page size—for which DBMS products provide defaults (Slide 5).

Physical file organizations and index choices (Slides 6–7, 9–10)

For each table, physical design chooses:

  • Physical file organization (how table tuples are stored on disk):
    • Unordered (heap): rows placed wherever free space exists; fast inserts, full scans for searches.
    • Ordered (clustered): rows physically arranged in sort order on disk.
    • Hashing on a hash-key: a hash function maps keys directly to disk buckets.
    • Clustering of several relations: tuples belonging to different tables that are often joined together are physically interleaved on the same disk pages.
  • Index structures and clustering:
    • Structure: typically B⁺-Tree or hash index.
    • Clustered (primary) index: defines the physical order of the table’s rows; only one clustered index can exist per table.
    • Unclustered (secondary) index: stores keys and row pointers independently of physical row order; many can be defined per table.

Optionally, physical tuning may also feed back into changes in the logical schema (Slide 10): choosing an alternative design that still preserves Boyce-Codd Normal Form (BCNF), choosing an alternative that deliberately breaks BCNF (denormalization, e.g., in data warehouses), or partitioning tables across different disks.

Iterative design and database tuning

No single algorithmic methodology solves physical design (Slide 11). Instead, designers combine an iterative trial-and-error process, general criteria, and heuristics—and continue refining choices after deployment through database tuning:

  1. Index the primary key (usually exploited for selections and joins; clustered or unclustered depending on other constraints) (Slide 12).
  2. For remaining common query predicates (Slide 13):
    • Pick a frequent query and inspect its current evaluation plan.
    • Propose a new index and inspect the new evaluation plan.
    • Keep the index only if the query cost drops and the extra cost on the modification workload (every INSERT, UPDATE, DELETE must also maintain affected indexes) and available disk space remain acceptable.

Core indexing heuristics (Slides 14–18)

Situation / SQL PatternRecommended DecisionMechanism & Why
Small tableNever indexReading the whole table takes only a few disk reads; reading an index adds overhead without savings.
Low-cardinality attribute (e.g., Gender)Never index (standard OLTP)Low selectivity: a predicate matches a large fraction of rows across nearly every page, so a scan beats random pointer chases. Exception: data warehouses use bitmap indices effectively on low-cardinality columns (Slide 14).
Single WHERE equality (col = val)Hash (preferred) or B⁺-TreeHash maps straight to the matching bucket in constant expected steps; B⁺-Tree also works well.
Single WHERE range (>, <, BETWEEN)B⁺-Tree onlyLeaf keys stay sorted and linked; hash functions destroy key order.
Slow range or GROUP BY queryConsider a clustered indexMatching rows sit together on consecutive disk pages instead of requiring scattered page reads (Slide 15).
Multiple simple WHERE predicatesComposite index (choose key order carefully)One multi-attribute index can filter multiple conditions together, though update maintenance grows (Slide 16).
Nested loop joinIndex on the inner table’s join attributeOuter loop scans filtered/smaller outer table once; inner loop probes the inner table repeatedly. An index avoids scanning the entire inner table on each probe (Slide 17).
Merge scan joinB⁺-Tree on join attribute (clustered if possible)Merge scan needs both inputs sorted; a clustered B⁺-Tree stores rows already in join-key order (Slide 17).
GROUP BYIndex on grouping attributes (Hash or B⁺-Tree), or group-by push downGroups matching keys or avoids costly post-join sorting (Slide 18).

Clarifying outer vs. inner tables in nested-loop joins

In a nested-loop join, execution behaves like two nested loops:

for each row in OUTER table:              -- scanned once (smaller / filtered input)
    look up matching rows in INNER table  -- probed repeatedly (needs index on join key)

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

  • Source Example 4 (Slide 30):
    SELECT EName, Mgr FROM EMP E, DEPT D
    WHERE E.Dept# = D.Dept# AND DName = 'Toys';
    DName = 'Toys' shrinks DEPT first (via a Hash index on DEPT.DName). Therefore DEPT is the outer table and EMP is the inner table, so we want a Hash index on EMP.Dept#.
  • Source Example 6 (Slides 32–34):
    SELECT EName, Mgr FROM EMP E, DEPT D
    WHERE E.Dept# = D.Dept# AND Salary BETWEEN 10000 AND 12000 AND Hobby = 'Tennis';
    Here the selection predicates shrink EMP first (via a Hash index on Hobby, a B⁺-Tree on Salary, or RID intersection by a smart optimizer). The roles flip: EMP is the outer table and DEPT is the inner table—so the nested loop uses an index on DEPT.Dept#, unless DEPT is very small (in which case indexing DEPT is avoided and a scan or hash join is used).

Optimizer traps and worked examples (Slides 26–29)

Even when an index exists, the optimizer may ignore it or find that using it is slower than a sequential scan:

Source Example 1: Arithmetic expression disables index use (Slide 26)

SELECT * FROM EMP WHERE Salary/12 = 1500;

With a B⁺-Tree index on Salary, the optimizer may disregard the index because Salary is wrapped inside Salary/12, whereas index keys store raw Salary values. Rewriting the predicate to WHERE Salary = 18000 lets the optimizer use the index.

Source Example 2: Skewed value distribution / low selectivity (Slide 27)

SELECT * FROM EMP WHERE Salary = 18000;

The index is syntactically usable, but provides no benefit if 18000 is a very common salary. Fetching a huge fraction of rows through an unclustered index triggers excessive random disk reads compared with a sequential scan.

Source Example 3: Block factor vs. cardinality (Slides 28–29)

Suppose EMP has a block factor of 30 tuples per block (page). Should you define a secondary (unclustered) index on EMP.Dept#?

  • Case A: Card(DEPT) = 50 (50 departments) → NO. With 30 employees per block and only 50 departments, almost every disk block contains employees from nearly all departments. Following secondary index pointers ends up reading virtually every block anyway—so a sequential scan is better.
  • Case B: Card(DEPT) = 2000 (2,000 departments) → YES. Each block holds at most 30 employees, so any single department appears in only a tiny fraction of blocks. The secondary index skips most of the EMP table.

When queries are still slow (Slide 23)

If query performance still misses expectations:

  1. Update database statistics! Cost-based optimizers rely on stored table sizes and value distributions; stale statistics produce poor execution plans.
  2. Continue database tuning by adding or removing indexes based on real execution plans and workload costs.
  3. Avoid optimizer-forcing techniques (called hints in Oracle): hints force a specific index or join method inside the SQL query itself, which should almost never be used because it destroys data independence.

Common confusion

Adding an index does not automatically make a query faster. If the indexed column is wrapped in arithmetic (Salary/12), has low selectivity (Gender, Salary = 18000 when common, or Card(DEPT) = 50 with block factor 30), or belongs to a tiny table (DEPT with a handful of pages), using an unclustered index either gets skipped by the optimizer or performs more disk I/O than a sequential scan—while still slowing down every insert, update, and delete.

Retrieval questions

1. Why does the source say to avoid Oracle-style optimizer hints, and what should you check first when an expected index is ignored?
Hints hardcode physical access choices into SQL statements, breaking data independence (application SQL becomes tied to specific physical structures). First check whether database statistics are stale (and update them), verify the predicate isn’t hiding the column inside an expression (like Salary/12 = 1500), and inspect whether low selectivity makes a sequential scan genuinely cheaper.

2. An EMP table stores 30 tuples per page. Why is a secondary index on EMP.Dept# helpful when Card(DEPT) = 2000, yet harmful when Card(DEPT) = 50?
When Card(DEPT) = 50 and each page holds 30 rows, almost every page contains at least one employee from nearly every department, so an unclustered index still forces the DBMS to read almost every page (making a sequential scan faster). When Card(DEPT) = 2000, each page holds at most 30 distinct departments, so a specific Dept# appears on only a small subset of pages and the index skips most disk I/O.

3. Given SELECT EName, Mgr FROM EMP E, DEPT D WHERE E.Dept# = D.Dept# AND DName = 'Toys', which table is outer and which is inner in a nested-loop join, and where does the join index go?
DEPT is the outer table because DName = 'Toys' filters it down to very few rows (using a hash index on DEPT.DName). EMP is the inner table, probed for each selected department, so the join needs an index (hash) on EMP.Dept#.