Database45-75 min
PostgreSQL Indexing Basics
Use `EXPLAIN ANALYZE`, add targeted indexes, and verify that query plans improve for real application queries.
PostgreSQLSQLPrisma
Prerequisites
- A slow query or endpoint to investigate.
- Access to run SQL against staging or local data.
- Enough sample data to make query plans realistic.
1
Plan the implementation
Start by choosing the exact page, route, API, or deployment surface you want to improve. A narrow target makes the implementation measurable and easier to verify.
- Write down the current behavior and the user-facing problem it creates.
- Pick one measurable success signal such as bundle size, latency, error rate, security coverage, or UI responsiveness.
- Identify the files, routes, providers, and environment variables involved.
- Create a rollback note before changing production-sensitive configuration.
2
Set up the required tools
Install or configure only the tools needed for this implementation. Keep config close to the feature so future developers can find the moving parts quickly.
Implementation snippet
EXPLAIN ANALYZE SELECT * FROM orders WHERE user_id = '...' ORDER BY created_at DESC;Checklist
- Dependencies are added to the correct workspace package.
- Environment variables are documented in `.env.example` when needed.
- Local development still starts without production-only secrets.
- The change is small enough to review in one pull request.
3
Implement the core pattern
- Capture the exact SQL query from logs or ORM debugging.
- Run `EXPLAIN ANALYZE` before adding an index.
- Create an index that matches filters and sort order.
- Use concurrent index creation for large production tables when possible.
- Run the same query plan again and compare actual time and scan type.
Implementation snippet
CREATE INDEX CONCURRENTLY idx_orders_user_created_at
ON orders (user_id, created_at DESC);4
Handle edge cases
Checklist
- The index supports a real frequent query.
- Write-heavy tables are not over-indexed.
- Unique constraints are used for data integrity where needed.
- Production index creation avoids long table locks.
5
Verify before production
- Run the app locally and test the normal success path.
- Test one failure path, one empty state, and one slow-network or retry path.
- Run the project build and any related unit or integration tests.
- Check browser console, server logs, and network responses for hidden warnings.
- Document the final behavior, commands used, and any follow-up work.