Video in progress

A column has an index, but the planner isn't using it. What causes do you check?

The most common causes: the column is inside a function or cast to another type, the condition returns too many rows and a seq scan is cheaper, the statistics are stale, or the LIKE pattern starts with %. I check each one with EXPLAIN.

📖 Answer

My checklist goes roughly like this:

-- 1. A function "hides" the index
WHERE lower(email) = 'a@b.uz'          -- the (email) index doesn't work
CREATE INDEX ON app_user (lower(email)); -- you need an expression index

-- 2. Type mismatch: a bigint column, a numeric parameter
WHERE id = 42::numeric                  -- the column gets cast, the index is skipped

-- 3. Truncating a date column
WHERE date(created_at) = '2026-11-01'   -- bad
WHERE created_at >= '2026-11-01' AND created_at < '2026-11-02'  -- good
  1. Low selectivity. If the condition returns 30% of the table, a sequential read is cheaper than random index reads. That's not a mistake, the planner is doing the right thing.
  2. Stale statistics: ANALYZE.
  3. LIKE '%abc': a B-tree works only with a prefix. Another subtle point: LIKE 'abc%' also works only in the C collation or with a text_pattern_ops index.
  4. OR conditions: sometimes splitting into UNION ALL gives a better plan.
  5. On an SSD, random_page_cost is still 4 (the HDD value), so the planner thinks the index is expensive. It's usually lowered to 1.1-1.5.

On the Java side the second cause comes up often: passing a parameter with the wrong type in JDBC, or an entity field type in Hibernate that differs from the DB column.