Composite Indexes and Lexicographic Ranges

Use the order of a two-column index to identify contiguous search ranges and explain which predicates can narrow them.

Source
Anbloq
Language
English
Created
Updated
Chapters
2
Reading time
1 min
In this article2 chapters
  1. Leftmost equality creates one contiguous region
  2. Sorting and scanning depend on the same order

Leftmost equality creates one contiguous region

In an index ordered by \((a,b)\), entries for \(a=7\) lie together in order of \(b\). A condition \(a=7\) and \(10\le b<20\) then describes a contiguous interval within that region. A condition on \(b\) alone can match separate intervals for every distinct \(a\). Some engines can use repeated seeks under suitable statistics, while the simple one-range scan does not capture all such values together.

Sorting and scanning depend on the same order

If \(a\) is fixed to one value, scanning its part of the index can often produce results in \(b\) order. With several \(a\) groups, their local \(b\) orders do not automatically form global \(b\) order. B-tree nodes define lookup by separators. Cost-based optimization weighs useful output ordering against alternative access costs.[1][2]

References

  1. PostgreSQL Global Development Group, PostgreSQL Documentation ↩
  2. CMU 15-445/645, Introduction to Database Systems: Lectures ↩