Mercury SkillsMercury Skills

Database Design

Schema design, normalization, indexing, migrations, and query optimization for SQL and NoSQL

View source60 downloads
databasesqlnosqlschemaindexingmigrations

Database Design#

Design efficient, scalable database schemas.

Normalization#

Normal FormRuleViolation Example
1NFAtomic columns, no repeating groupsphone_numbers: "555-0100,555-0200"
2NF1NF + all non-key cols depend on full PKOrders table with product details
3NF2NF + no transitive dependenciesEmployee with department_name (dept info in dept table)

When to denormalize: Read-heavy workloads, reporting, caching layers.

Indexing Strategy#

Index Types#

IndexUse CaseTradeoff
B-TreeGeneral purpose, equality + rangeDefault, usually best
HashEquality onlyFast lookups, no sorting
GINArray/JSONSlightly slower writes
BRINHuge sorted tablesVery small size

Rules#

  • Index columns used in WHERE, JOIN, ORDER BY
  • Index foreign keys
  • Covering indexes for frequent queries
  • Don't over-index (slows writes, uses space)
  • Drop unused indexes (use pg_stat_user_indexes)

Migrations#

  • One file per change, sequential naming
  • Always reversible (up/down pair)
  • Test on staging before production
  • Run in transactions where possible
  • Avoid locking tables on large tables (use pt-online-schema-change)

Query Optimization#

  • EXPLAIN ANALYZE every slow query
  • Look for: Seq scans, nested loops, temp files
  • Common fixes: missing index, bad join order, OR conditions
  • N+1 problem: batch find / includes / joins
  • Limit offsets for pagination (use cursor-based)

More in Backend

View all →

API Design

REST and GraphQL API design principles, versioning, error handling, and documentation patterns

apirestgraphql

Authentication & Authorization

JWT, OAuth2, SAML, session management, RBAC, ABAC, and MFA implementation

authenticationauthorizationsecurity

Caching Strategies

CDN, Redis, in-memory cache, cache invalidation, and distributed caching patterns

cachingperformanceredis