HomeRoadmapsDB Migration Day 5
Database Migrations & Zero-Downtime Schema Evolution Β· Day 5 Β· 20 min

Query-Plan & Index Regression Gate
Zero-Downtime Schema Evolution β€” Day 5

Evaluate EXPLAIN scan plans and cost budgets across schema versions to catch SEQ_SCAN regressions and broken composite index prefixes before release.

⚑ Runtime: Node.js 22 LTS Β· VitestπŸ›‘οΈ Database Reliability & Schema Evolution QE Architect
πŸ“¦Sample Codebase & Working Solution (Day 5)Available Now
View on GitHub

The official reference codebase for Day 5 is hosted in the repository. Clone the repository and navigate to tests/day5-query-plan-gate.spec.ts to run the working solution.

Git Clone β€” Working Solution
git clone https://github.com/777abhi/db-migration-qe-roadmap.git
cd db-migration-qe-roadmap/tests/day5-query-plan-gate.spec.ts
🏭Production Context & Enterprise Implementation
πŸ’‘ Why We Are Doing This: Dropping or reordering a composite index during a Contract phase can silently flip a 2ms Index Scan into a 4s Sequential Scan on a 10M-row table, exhausting DB CPU.
🏒 Real-World Production Usecase: Comparing before/after EXPLAIN plans for top-N production queries in CI before approving any index drop or column change.
βš™οΈ How Implemented in Production: Cost-based query-plan estimator checking leftmost index prefixes, covering Index-Only scans, and maxCostIncreaseRatio budgets.
Phase 1

Run Query-Plan Regression Gate Tests

Step 5.1: Run Day 5 Tests
Terminal β€” Day 5 Lab
npm run test:day5
Step 5.2: Sample Codebase & Working Solution (Day 5)
src/queryPlan/queryPlanGate.ts
if (before.scanType !== 'SEQ_SCAN' && after.scanType === 'SEQ_SCAN') {
  regressions.push({
    queryId: q.id,
    type: 'SEQ_SCAN_REGRESSION',
    beforeScan: before.scanType,
    afterScan: after.scanType,
    message: `Query ${q.id} degraded from ${before.scanType} to SEQ_SCAN.`,
  });
}
Day 5 Key Learnings
  • Evaluated leftmost-prefix matching for composite B-tree indexes.
  • Blocked migrations that degrade an INDEX_SCAN or INDEX_ONLY_SCAN to SEQ_SCAN.
  • Enforced quantitative cost-increase ratio budgets across critical application queries.

🧠 Knowledge Check

A query filters on (tenant_id, status). Which index still satisfies the leftmost-prefix rule?