Why this matters
N+1 patterns explode latency and DB load under scale.
Replace per-row lookups with JOINs or bulk queries to fetch related data in fewer round-trips.
N+1 patterns explode latency and DB load under scale.
Side-by-side examples engineers can pattern-match during review.
$orders = $pdo->query('SELECT * FROM orders')->fetchAll();
foreach ($orders as $o) { $c = $pdo->query('SELECT * FROM customers WHERE id='.(int)$o['customer_id'])->fetch(); }$stmt = $pdo->query('SELECT o.*, c.name AS customer_name FROM orders o JOIN customers c ON c.id = o.customer_id');
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);foreach($rows as $r){ query related }SELECT ... JOIN ...From the same buckets as this rule.
Review SQL/database migrations for operations that can lock large tables or cause downtime. Examples: creating indexes without CONCURRENTLY (Postgres), ALTER COLUMN TYPE on big tables, adding NOT NULL without backfill, long-running updates without batching. Require an online migration strategy (CONCURRENTLY, backfill in batches, dual-write/expand-contract) and a rollback plan.