Skip to content

docs: add database indexing best practices guide#126

Merged
PAMulligan merged 1 commit into
mainfrom
22-add-database-indexing-best-practices-guide
Jun 13, 2026
Merged

docs: add database indexing best practices guide#126
PAMulligan merged 1 commit into
mainfrom
22-add-database-indexing-best-practices-guide

Conversation

@PAMulligan

Copy link
Copy Markdown
Contributor

Summary

Adds docs/api-development/database-indexing.md, a reference guide for index design in the Nerva (Drizzle ORM + PostgreSQL) stack. Poor indexing is the most common cause of slow APIs in production. The pipeline generates a baseline (a B-tree on each foreign key and on created_at); this guide is the reference for going beyond it.

Topics covered

  • Defining indexes in Drizzle — the pgTable index callback, naming convention, the generate/migrate workflow, and the CREATE INDEX CONCURRENTLY caveat
  • Single-column vs. composite indexes — the leftmost-prefix rule and how column order decides what a composite serves
  • Unique indexes and constraints.unique() vs. uniqueIndex(), expression/case-insensitive uniqueness, and NULL handling
  • Partial indexes for soft deletesWHERE deleted_at IS NULL, tied to the existing notDeleted() helper, including partial unique indexes so a deleted user's email can be reused
  • B-tree vs. GIN vs. GiST (plus BRIN) — with jsonb containment and pg_trgm trigram search examples
  • Index-only scans and covering indexes — including the honest note that drizzle-orm@0.45.2 exposes no .include(), so a true INCLUDE covering index needs a custom-SQL migration
  • Anti-patterns — over-indexing, redundant indexes, low-cardinality columns, unindexed foreign keys, and functions over indexed columns
  • Validating with EXPLAIN ANALYZE — reading the plan, a before/after, and testing at realistic data volume

Every Drizzle code example is verified against the installed drizzle-orm@0.45.2 index-builder API.

Wiring

  • References the performance-benchmarker agent for automated profiling and index recommendations
  • Linked from docs/api-development/README.md (Performance section)

Notes

  • Markdown-only change; the affected CI job is the lychee link check. All external links return 200 and all internal link targets were verified to exist.

Closes #22

🤖 Generated with Claude Code

Add docs/api-development/database-indexing.md, a reference for index
design in the Drizzle + PostgreSQL stack. The pipeline generates a
baseline (B-tree on foreign keys and created_at); this guide covers
everything past that:

- Defining indexes in Drizzle schema files (pgTable index callback,
  naming, the generate/migrate workflow, the CONCURRENTLY caveat)
- Single-column vs. composite indexes and the leftmost-prefix rule
- Unique indexes and constraints (.unique() vs. uniqueIndex(),
  expression/case-insensitive uniqueness, NULL handling)
- Partial indexes for the soft-delete pattern (WHERE deleted_at IS
  NULL), including partial unique indexes for email reuse
- Choosing B-tree vs. GIN vs. GiST (and BRIN), with trigram search
  and jsonb containment
- Index-only scans and covering indexes, noting drizzle-orm 0.45.2
  has no .include() so true INCLUDE indexes need a custom migration
- Anti-patterns: over-indexing, redundant indexes, low-cardinality
  columns, unindexed foreign keys, functions over indexed columns
- Validating index usage with EXPLAIN ANALYZE

All Drizzle examples are verified against the installed
drizzle-orm@0.45.2 index API. References the performance-benchmarker
agent for automated profiling and links the guide from the API
development standards index.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@PAMulligan PAMulligan linked an issue Jun 13, 2026 that may be closed by this pull request
6 tasks
@github-actions github-actions Bot added the area: docs Documentation label Jun 13, 2026
@PAMulligan PAMulligan self-assigned this Jun 13, 2026
@PAMulligan PAMulligan moved this from Todo to Done in PMDS Open Source Roadmap Jun 13, 2026
@PAMulligan PAMulligan merged commit dd56ad7 into main Jun 13, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: docs Documentation

Projects

Development

Successfully merging this pull request may close these issues.

Add database indexing best practices guide

1 participant