advanced24 min studyLesson 275 in Engineering

Composite keys: match the ordered prefix to the question

Trace lexicographic ranges, conditional selectivity, and workload tradeoffs without relying on a universal most-selective-first rule.

On this page

What you’ll understand

  • Describe the contiguous region selected by equality prefixes and a following range.
  • Distinguish examined entries from returned rows.
  • Use conditional probabilities and actual query shapes to compare key orders.

An index on several columns creates one ordered sequence of tuples, not several independent indexes packed into one name. Equality on the leading columns selects a region within that sequence; a range on the next column narrows it further. The useful order depends on which constraints, ordering requirements, and query variants the workload actually supplies.

Walk a lexicographic sequence

For an ascending index on tenant_id, event_time, and status, rows are grouped first by tenant, then time within a tenant, then status for equal times. Equality on tenant and a time interval usually defines one straightforward contiguous scan interval. A status condition to the right of that range can reject entries without necessarily shortening the whole interval examined. Distinguish navigation that skips entries from a filter evaluated on entries already reached. Both can help, but they save different kinds of work.

Changing the order to tenant_id, status, event_time can make tenant equality plus status equality plus a time range describe a tighter interval. However, a query omitting status now has a different navigation problem. It may require broader scanning or repeated searches across status groups, and its requested time ordering may need additional work. PostgreSQL 18 can apply B-tree skip scan when repeated searches over missing leading values are cost-effective. Therefore a missing equality prefix does not make an index categorically unusable; it changes the available paths and their costs.

(a,b)<lex(a′,b′)  ⟺  a<a′ ∨ (a=a′∧b<b′)(a,b)<_{\rm lex}(a',b')\iff a<a'\ \lor\ (a=a'\land b<b')
This definition assumes two totally ordered non-null domains and ascending lexicographic comparison. SQL null placement, collation, mixed sort directions, and longer keys require the index's specified comparator rules.↔ Scroll wide equations horizontally.

Selectivity is conditional on the population

Selectivity is the fraction of a defined population satisfying a predicate. Tenant and status may be correlated: one tenant can contain almost all failed events while the global failed fraction is small. Multiplying global selectivities as though predicates were independent can badly misestimate the result. The exact chain rule uses the probability of the second condition given the first. Estimates based on statistics must state which conditional distributions they approximate and whether the workload's parameter values resemble the sampled population.

Returned-row selectivity and scan selectivity differ. An index may examine a thousand entries in a time interval and return only fifty after checking status. A different key order might navigate directly to fifty entries yet be worse for another common query or require an extra sort. Also account for row fetching, LIMIT behavior, grouping, key width, and writes. The slogan put the most selective column first ignores equality availability and order requirements. Start with actual query templates and compare estimated and observed plans for representative and skewed parameters.

P(A∩B)=P(A)P(B∣A),P(A)>0P(A\cap B)=P(A)P(B\mid A),\qquad P(A)>0
A and B are predicates over one defined row population. The conditional probability makes no independence assumption. Replacing P(B|A) with P(B) is justified only by an appropriate independence model, not by the existence of separate column statistics.↔ Scroll wide equations horizontally.

Evaluate a portfolio of query paths

One composite index may serve a frequent query family better than several independent indexes, while separate indexes can be combined for more varied predicates. Every added structure consumes storage and maintenance. Test common parameter combinations, including heavy tenants and rare statuses, rather than one conveniently selective example. Inspect how many entries or rows are filtered, whether sorting occurs, and which order a LIMIT can exploit. An index that returns the first twenty useful rows cheaply may be valuable even if its complete unbounded scan would be expensive.

The probability lab isolates a hypothetical filter fraction. Reset, set p=0.05 and n=20, predict one matching item on average, then Play or Step to inspect the exact binomial bars. Separately Reset, set p=0.50 and n=20, predict ten matches on average, then Play or Step. These independent common-probability trials do not traverse composite keys or represent correlated tenant data. Use the contrast to explain why a global five-percent rate is inadequate if the selected tenant's conditional rate is fifty percent.

Separate scanned entries from final matches

A million-row table has exactly 10,000 rows for one tenant. A time interval contains 10% of that tenant's rows. Of those interval rows, 5% have status ready. Assume both proposed indexes are available and ignore other costs.

  1. Tenant equality and the time interval identify 10,000×0.10=1,000 entries. In order (tenant,time,status), a straightforward interval scan may examine those thousand entries before filtering status.

  2. The final ready count is 1,000×0.05=50. The 5% value is conditional on this tenant and interval, so no global independence assumption is needed.

  3. With order (tenant,status,time), equality on tenant and ready status followed by the time range can directly delimit the fifty matches in this example. Queries lacking status, required ordering, skip scans, and maintenance must still be compared before choosing the overall design.

The result

Fifty returned rows do not imply only fifty entries were examined. Key order controls the navigation path, while conditional selectivity describes the data.

Explore the model

Interactive lab

Interactive lab · probability

A conditional filter can differ from a global rate

Reset, set p=0.05 and n=20, predict mean count 1, then Play or Step. Separately Reset, set p=0.50 and n=20, predict mean count 10, then Play or Step. The exact independent binomial model does not implement composite indexes, skip scans, or correlated predicates.

A conditional filter can differ from a global rateThese are exact binomial probabilities for independent trials with one fixed success probability. Playback tours the distribution; it does not resample outcomes. Current readings: Mean · np: 5; Variance · np(1−p): 2.5; Total probability: 1. All parameters are adjustable with the labeled controls below.03581000.070.150.220.3Successes kP(X = k)k = 0 · P = 9.77e-4
Exact probabilityHighlighted outcomeMean np
Mean · np
5
Variance · np(1−p)
2.5
Total probability
1
t = 0 s

These are exact binomial probabilities for independent trials with one fixed success probability. Playback tours the distribution; it does not resample outcomes. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forAn estimated match fraction must describe the population selected by earlier predicates.

Check your understanding

What is the main flaw in always putting the globally most selective column first?

Try it yourself

A chosen tenant contains 4,000 rows, of which 25% are in a requested date range. Within that range, 40% have the requested status. Find the interval size and final match count, and name the relevant conditional rate.

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

Apply each fraction to its stated population, not to the full table.

Compare with the solution

The tenant-and-date interval has 4,000×0.25=1,000 rows. The final matches number 1,000×0.40=400. The 40% rate is status given this tenant and date interval; substituting an unrelated global status rate can misestimate the query. The scan count still depends on key order and execution strategy.

Take it with you

  • Composite keys define one lexicographically ordered navigation space.
  • A later filter can reduce returned rows without reducing all examined entries.
  • Choose key order from query shapes and conditional distributions, including skip-scan possibilities.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.