Indexing Strategy Language
5 exercises — practise the English vocabulary for indexing strategy discussions: B-tree limitations, partial indexes, the leftmost prefix rule, covering indexes, and arguing about the trade-offs of excessive indexing.
0 / 5 completed
1 / 5
A DBA says: "B-tree indexes support equality and range queries on sortable types." A new column description TEXT is proposed for full-text search. Which query type would NOT benefit from a standard B-tree index on description?
-- Query A: WHERE description = 'exact match'
-- Query B: WHERE description LIKE 'prefix%'
-- Query C: WHERE description LIKE '%substring%'
-- Query D: WHERE description > 'alpha' AND description < 'beta'B-tree index limitations vocabulary:
Option D is correct. A B-tree index stores values in sorted order, which enables equality lookups, prefix scans (LIKE 'prefix%'), and range queries. However, a leading wildcard pattern (
Alternatives for substring/full-text search:
Option D is correct. A B-tree index stores values in sorted order, which enables equality lookups, prefix scans (LIKE 'prefix%'), and range queries. However, a leading wildcard pattern (
LIKE '%substring%') cannot use the B-tree because there is no sorted starting point — the matching substring could appear anywhere in the value.| Query pattern | B-tree usable? | Why |
|---|---|---|
= 'value' | Yes | Equality — direct lookup in sorted tree |
LIKE 'prefix%' | Yes (with text_pattern_ops) | Prefix — B-tree can seek to the start of the prefix |
LIKE '%substring%' | No | Leading wildcard — no sorted traversal entry point |
> 'a' AND < 'z' | Yes | Range scan — B-tree traversal between two sorted bounds |
Alternatives for substring/full-text search:
- GIN index with
to_tsvector— PostgreSQL full-text search; enables@@match operator - pg_trgm extension + GIN/GiST index — enables fast
LIKE '%substring%'and fuzzy matching - Dedicated search engine — Elasticsearch, OpenSearch for complex search requirements