Designing Database Performance Optimization

1. Designing Query Optimization Strategy

StepAction
EXPLAIN ANALYZEInspect plan + actual times
Identify scansConvert seq scans to index scans
Reduce rows earlyPush WHERE before joins
Avoid SELECT *Project needed columns only
StatisticsANALYZE; tune autovacuum
RewritePrefer EXISTS over IN; CTE inlining

2. Designing Database Indexing Strategy

Index TypeBest For
B-treeEquality, range, sort
HashEquality only
GINJSONB, arrays, full-text
GiST / SP-GiSTGeometric, geo
BRINVery large, naturally ordered (time series)
CompositeMatch leftmost-prefix queries
PartialWHERE filter on subset (e.g., active=true)
Covering (INCLUDE)Index-only scans
Warning: Each index slows writes. Drop unused indexes (pg_stat_user_indexes idx_scan = 0).

3. Designing Database Query Caching

LayerToolNotes
Result cacheApp-level RedisCache by query hash + params
Plan cachePrepared statementsAvoid repeated parsing
DB cacheInnoDB/Postgres buffer poolTune to working set
Materialized viewPrecomputed resultREFRESH on schedule or trigger

4. Designing N+1 Query Solutions

SolutionTool
Eager loadingJPA fetch join, Hibernate @EntityGraph
Batch fetchingHibernate @BatchSize, Sequelize include
DataLoaderGraphQL batched/cached fetch per request
Single SQLJOIN + group by

5. Designing Batch Operations Strategy

OpPattern
Bulk insertCOPY (Postgres), LOAD DATA (MySQL), batched VALUES
Bulk updateUPDATE ... FROM (VALUES ...) AS v
UpsertINSERT ... ON CONFLICT DO UPDATE
Batch size500–5000 rows; balance lock time

6. Designing Database Materialized Views

StrategyRefresh
Full refreshREFRESH MATERIALIZED VIEW
Concurrent refreshNo write lock; needs unique index
Incrementalpg_ivm, triggers, CDC + custom
Use casesDashboards, leaderboards, search

7. Designing Lock and Deadlock Management

IssueMitigation
Lock contentionShorter txns, smaller scope
DeadlocksConsistent lock order, retry on detection
Long-running txnStatement timeout, idle_in_transaction_session_timeout
Optimistic lockingVersion column + WHERE version=?
PessimisticSELECT ... FOR UPDATE [SKIP LOCKED]

8. Designing Read Replica Strategy

DecisionRecommendation
Number of replicasBased on read RPS / replica capacity
RoutingRW endpoint = primary; RO endpoint = LB over replicas
Lag handlingRead-after-write to primary; bounded staleness
Workload isolationDedicated replica for analytics

9. Designing Database Schema Optimization

TechniqueBenefit
Right datatypesSMALLINT vs INT, TIMESTAMPTZ vs TEXT
Normalize for writesAvoid duplication / anomalies
Denormalize for readsMaterialized columns/tables
ConstraintsEnforce in DB; planner uses them
PartitioningManage large tables

10. Designing Connection Pool Tuning

ParamGuideline
maxPoolSize≈ ((cpu × 2) + spindles) × replica count
minIdle0 in cloud; small in on-prem
connectionTimeout200–500ms
idleTimeout5–10 min
maxLifetime30 min (rotates conns)
External poolerPgBouncer for >1000 client conns

11. Designing Slow Query Detection

ToolWhat it Provides
log_min_duration_statement (PG)Logs queries above threshold
slow_query_log (MySQL)Same
pg_stat_statementsAggregated stats per query
APMDatadog, New Relic, Honeycomb
auto_explainPlan capture for slow queries

12. Designing Query Plan Analysis

Plan OperatorMeaningAction
Seq ScanFull table readAdd index if selective
Index ScanB-tree lookupOK for small result
Bitmap Heap ScanMulti-row index lookupOK for medium results
Nested LoopO(N×M)Bad for large sets; need hash join
Hash JoinO(N+M)Good when one side fits memory
SortExternal sort if > work_memAdd index or raise work_mem