SwadhyAI Notes

All SQL notes

📊Read vs write optimization

Every read shortcut taxes writes

Read-heavy surfaces love replicas, caches, and covering indexes. Write-heavy paths want narrow rows, batching, sequential or BRIN-friendly keys, and as few secondary indexes as you can justify, each index is another structure to update on every mutation.

🎯When signals disagree

Split paths before tuning blindly.

SignalRead path moveWrite path move
Hot dashboardMaterialized view, cache, or read replicaDefer refresh, async jobs, bounded staleness SLAs
Ingest stormIsolate analytics to replicas or warehouseBulk COPY, staging tables, temporarily fewer secondary indexes
SQL

Read: covering index avoids heap hits

SELECT order_id, total_usd FROM orders WHERE customer_id = 441;
✓Illustrative result set
order_idtotal_usd
900144
9002120.5
SQL

Write: defer secondary indexes during bulk load (pattern)

-- 1) DROP INDEX CONCURRENTLY …
-- 2) bulk insert
-- 3) CREATE INDEX CONCURRENTLY …
✓Illustrative result set
note
Reduces random index maintenance during load

Index everything on a churn table

  • Covering every dashboard variant while inserts crawl.
  • Massive transactions pinning WAL and delaying checkpoints.
  • Heavy BI on the primary without concurrency caps.

Measure write amplification

  • Pair each new index with expected selectivity and write regression notes.
  • Tune checkpoint and autovacuum (or equivalent) on high-churn tables.
  • Use poolers and role limits to cap concurrent writers vs readers.

Isolation

Read scaling via async replicas changes what users may see after a write, say it in the product contract, not only in infra docs.

TL;DR

Read optimization uses indexes, covering paths, denormalized read models, and replicas to cut latency and primary load. Write optimization batches inserts, avoids unnecessary secondary indexes, orders updates to reduce deadlock risk, and uses appropriate isolation. Async replicas improve read throughput but are eventually consistent: route only workloads that tolerate staleness off the primary.