Managing Database Performance

1. Monitoring Database Metrics

MetricWhy
QPS / TPSWorkload trend
Latency p50/p95/p99User-visible perf
Cache hit ratioBuffer pool effectiveness (target > 95%)
Active / idle connectionsPool saturation
Locks & waitsContention
Replication lagReplica freshness
CPU / IO / RAMResource saturation
Bloat / fragmentationStorage health

2. Analyzing Slow Queries

SourceDetail
pg_stat_statementsTop queries by total/avg time
Slow query logPer-query threshold logging
EXPLAIN ANALYZEDetailed plan + actual costs
APM traceCorrelate with app flow

3. Tuning Database Configuration Parameters

Postgres ParamGuidance
shared_buffers~25% RAM
effective_cache_size~50–75% RAM (planner hint)
work_memPer-sort/hash; 8–64 MB; multiply by connections
maintenance_work_memVacuum/CREATE INDEX; 256MB–2GB
max_connectionsLow (100–200); use pooler for more
wal_compressionon — reduces WAL size
checkpoint_completion_target0.9
random_page_cost1.1 on SSD

4. Managing Connection Pooling

ToolNotes
PgBouncerTransaction / session pooling for Postgres
Pgpool-IIPooling + load balancing
ProxySQLMySQL connection routing
HikariCPJava app-side pool
pgx, sequelize, etc.Built-in pools in drivers

5. Implementing Query Result Caching

PatternDetail
Cache-asideApp checks cache → DB on miss → store
Read-throughCache fetches on miss transparently
Write-through / behindCache leads writes
InvalidationTTL + explicit busts on writes
Materialized viewDB-managed cache with refresh

6. Optimizing Buffer Pool and Memory Usage

DBSetting
Postgresshared_buffers, effective_cache_size
MySQL InnoDBinnodb_buffer_pool_size (60–80% RAM)
SQL Servermax server memory
OracleSGA / PGA targets
Cache hit ratioAim > 0.95 on OLTP

7. Managing Disk I/O Performance

ActionEffect
NVMe SSDLower latency, higher IOPS
Separate WAL/data disksReduces seek contention
RAID 10Balanced read/write perf
Filesystem (XFS/ext4)Tune mount options (noatime)
Page size alignmentMatch DB block to FS block
CompressionTrade CPU for I/O

8. Implementing Read Replicas for Load Distribution

ConcernDetail
RoutingWrites → primary, reads → replicas
Lag-aware readsAvoid replica if lag > SLA
Read-your-writesPin to primary briefly after write
Connection routingProxySQL, Aurora reader endpoint, Spring AbstractRoutingDataSource

9. Using Database Profiling Tools

ToolDB
pg_stat_statements, pgBadgerPostgres
Performance Schema, sysMySQL
Query Store, DMVsSQL Server
AWR, ASH, SQLTOracle
Datadog, New Relic, pganalyzeCross-DB SaaS

10. Establishing Performance Baselines

StepDetail
Capture normal loadHourly metrics over week
Define SLOse.g., p95 < 100 ms for primary queries
Alert on deviation±20% drift from baseline
Re-baselineAfter schema/traffic shifts