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

snippet
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.