AI Hints
Home›PostgreSQL Tutorial›GIN, BRIN and Index Mistakes
PostgreSQL · Lesson 57 of 120

Index Mistakes

Why it is not used.

⏱ 2 min read▶ Video at 50:25🎓 Free
Watch this lesson in the PostgreSQL courseWatch on YouTube ↗Full PostgreSQL course

You add an index, but the query is still slow. Postgres isn't using it. Usually, the query is written in a way the index can't help with.

  • Index added
  • Still slow
  • Why?

A function hides the column

The index is on viewed at. But wrapping the column in a function, like date of viewed at, means the index can't be used. Writing the same filter as a range lets it use the index. Run it. A sequential scan, then an index scan.

query.sqlsql
CREATE INDEX views_time_idx ON page_views (viewed_at);

EXPLAIN (COSTS OFF)
SELECT count(*) FROM page_views WHERE date(viewed_at) = '2025-06-01';

EXPLAIN (COSTS OFF)
SELECT count(*) FROM page_views
WHERE viewed_at >= '2025-06-01' AND viewed_at < '2025-06-02';
Output
CREATE INDEX
                       QUERY PLAN
--------------------------------------------------------
 Aggregate
   ->  Seq Scan on page_views
         Filter: (date(viewed_at) = '2025-06-01'::date)
(3 rows)

                                                                          QUERY PLAN
----------------------------------------------------------------------------------------
----------------------------------------------------------------------
 Aggregate
   ->  Index Only Scan using views_time_idx on page_views
         Index Cond: ((viewed_at >= '2025-06-01 00:00:00+00'::timestamp with time zone)
         AND (viewed_at < '2025-06-02 00:00:00+00'::timestamp with time zone))
(3 rows)
Also uses _setup.sql
SELECT setseed(0.42);
CREATE TABLE page_views (
  view_id     bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  customer_id integer NOT NULL,
  book_id     integer NOT NULL,
  device      text NOT NULL,
  viewed_at   timestamptz NOT NULL,
  ms          integer NOT NULL);
INSERT INTO page_views (customer_id, book_id, device, viewed_at, ms)
SELECT 1 + (random() * 499)::int, 1 + (random() * 29)::int,
       (ARRAY['web','web','web','app','app'])[1 + (random() * 4)::int],
       timestamptz '2025-01-01' + (g * interval '100 seconds'),
       50 + (random() * 900)::int
FROM generate_series(1, 300000) g;
VACUUM ANALYZE page_views;
ALTER DATABASE riverside SET max_parallel_workers_per_gather = 0;

Common mistakes

Wrapping the column in a function. Like with a leading percent sign. Indexing columns that are rarely searched. Too many indexes, which slow down writes. And small tables, where a scan is faster anyway.

MistakeFix
func(col) = xrange, or expression index
LIKE '%word'trigram or full-text
Unused indexesdrop them
Too manyslower writes
Tiny tablesseq scan is fine

Summary

Indexes only help when the query matches them, so watch for functions and leading wildcards. Next, full text search.

Key takeaways
  • No functions
  • No leading %
  • Check plans
Next up: Full-Text Search — search words, not text