Check Index Usage With EXPLAIN

Read an EXPLAIN plan to confirm whether a query uses the intended index and where a sequential scan or sort is introduced.

SqlData Fetching

Code

sqlsnippet
10 lines
EXPLAIN (ANALYZE, BUFFERS)
SELECT id, email
FROM users
WHERE tenant_id = 42
  AND created_at >= now() - interval '7 days'
ORDER BY created_at DESC
LIMIT 50;

-- A covering index such as (tenant_id, created_at DESC) lets the planner
-- use an index scan and avoid a sort when the ORDER BY matches the index.

How it works

What runs is SELECT / EXPLAIN, with users as the object involved.

Clause by clause, expect: a WHERE clause that keeps only the rows a statement is meant to touch; an ORDER BY that fixes the output order; a row cap (LIMIT) bounding the result size.

Applying it

The users table is the working surface here, and the LIMIT bounds each probe.

Statements that matter

EXPLAIN (ANALYZE, BUFFERS)
SELECT id, email
FROM users
WHERE tenant_id = 42
  AND created_at >= now() - interval '7 days'
ORDER BY created_at DESC
LIMIT 50;

Interactive Tools for This Topic

FAQ

Which relations are involved here?
The references are `users`.