I recently had a technical interview where I was asked about multicolumn indexes.I brought up an example: an index on (a, b, c). If a query filters only on b and c, I wouldn’t expect PostgreSQL to use that index. Column order matters, and the query skips the leading column, a.I was told I was wrong and that the index would be used. No one explained why.After the interview, I tested it. I created a table with 100,000 rows, inserted numbers in random order, and added an index on (a, b, c). For a query filtering on b and c, PostgreSQL chose a sequential scan.If you ask this question in an interview or tell someone their answer is wrong, at least specify which PostgreSQL version you mean and explain your reasoning. Candidates shouldn’t have to guess.PostgreSQL 18 introduced skip scans, which can make this index useful even without a condition on a. But PostgreSQL 18 isn’t widely used everywhere yet. Earlier versions can also use the index, but they would have to scan the whole thing, so PostgreSQL will often choose a sequential scan instead.So «the index will be used» is just as questionable as «the index won’t be used» if you don’t know the version, the data, or the query plan.
1 Aralık 2026 Salı
MultiColumn Index - Skip Scans
Açıklaması şöyle
Kaydol:
Kayıt Yorumları (Atom)
Hiç yorum yok:
Yorum Gönder