Skip to content

πŸ—„οΈ Database Fundamentals ​

Choosing, designing, and querying databases effectively.

Database types ​

TypeExamplesBest for
Relational (SQL)PostgreSQL, MySQLStructured data, transactions, relations
DocumentMongoDB, FirestoreFlexible schemas, nested data
Key-ValueRedis, DynamoDBCaching, sessions, high throughput
GraphNeo4j, ArangoDBRelationships-heavy data
Time-seriesInfluxDB, TimescaleDBMetrics, IoT, logs
VectorPinecone, pgvectorEmbeddings, similarity search

Schema design principles ​

  • Normalize to reduce duplication (3NF for OLTP)
  • Denormalize for read performance when needed
  • Every table needs a primary key
  • Use foreign keys for referential integrity
  • Add indexes on frequently queried columns
  • Use appropriate data types (don't store dates as strings)

Index guidelines ​

  • Index columns used in WHERE, JOIN, ORDER BY
  • Composite indexes: column order matters (leftmost prefix)
  • Don't over-index β€” each index slows writes
  • Monitor slow queries to find missing indexes
  • Use EXPLAIN / EXPLAIN ANALYZE to check query plans

Common query pitfalls ​

PitfallProblemFix
N+1 queries1 query + N sub-queriesUse JOIN or eager loading
SELECT *Fetches unnecessary dataSelect only needed columns
Missing indexFull table scanAdd appropriate index
No paginationReturns all rowsUse LIMIT + OFFSET or cursor
String matchingLIKE '%term%' can't use indexFull-text search or trigram index

Migrations ​

  • Always use migration files, never manual SQL in production
  • Migrations must be reversible (up + down)
  • Test migrations on a copy of production data
  • Deploy migrations separately from application code
  • Never delete data in a migration without a backup plan

Pergame Knowledge Base