---
phase: 07-pre-expansion-fixes
plan: 01
subsystem: database
tags: [performance, n+1, sql-optimization, indexes, static-cache]

requires:
  - phase: 06-sonarqube-quality
    provides: OrdersRepository refactored (god class split)
provides:
  - Aggregating LEFT JOINs replacing N+1 correlated subqueries in orders list
  - Static cache for information_schema check
  - Performance indexes on orders table
affects: [07-02, 07-03, 07-04]

tech-stack:
  added: []
  patterns: [aggregating-left-join, static-property-cache]

key-files:
  created:
    - database/migrations/20260314_000048_add_orders_performance_indexes.sql
  modified:
    - src/Modules/Orders/OrdersRepository.php

key-decisions:
  - "allegro_order_status_mappings index skipped — already has UNIQUE KEY on allegro_status_code"
  - "Migration date 20260314 (execution date, not plan date)"

patterns-established:
  - "Aggregating LEFT JOIN zamiast correlated subqueries dla countów/sum w listach"
  - "Static property cache dla information_schema lookups"

duration: ~8min
started: 2026-03-14
completed: 2026-03-14
---

# Phase 7 Plan 01: Performance Fixes Summary

**Eliminacja N+1 subqueries w liście zamówień, static cache dla information_schema, indeksy wydajnościowe na tabeli orders**

## Performance

| Metric | Value |
|--------|-------|
| Duration | ~8min |
| Started | 2026-03-14 |
| Completed | 2026-03-14 |
| Tasks | 3 completed |
| Files modified | 2 (1 modified, 1 created) |

## Acceptance Criteria Results

| Criterion | Status | Notes |
|-----------|--------|-------|
| AC-1: Brak correlated subqueries w liście zamówień | Pass | 4 subqueries zastąpione aggregating LEFT JOINs; grep potwierdza zero `SELECT COUNT(*) FROM order_items WHERE oi.order_id` |
| AC-2: information_schema nie odpytywany per-request | Pass | `private static ?bool $supportsMappedMedia` + `self::$supportsMappedMedia` — max 1× na cykl PHP |
| AC-3: Brakujące indeksy dodane migracją | Pass | Migracja 000048: source, external_status_id, ordered_at, composite (source, external_status_id); IF NOT EXISTS |

## Accomplishments

- 4 correlated subqueries (items_count, items_qty, shipments_count, documents_count) → 3 aggregating LEFT JOINs — eliminacja ~200 dodatkowych zapytań przy 50 wierszach na stronę
- `canResolveMappedMedia()` z instance na static property — information_schema odpytywany max 1× na cykl PHP
- 4 brakujące indeksy na tabeli orders dla typowych filtrów/sortowań

## Files Created/Modified

| File | Change | Purpose |
|------|--------|---------|
| `src/Modules/Orders/OrdersRepository.php` | Modified | LEFT JOINs zamiast subqueries w buildListSql(); static cache w canResolveMappedMedia() |
| `database/migrations/20260314_000048_add_orders_performance_indexes.sql` | Created | Indeksy: source, external_status_id, ordered_at, composite (source, external_status_id) |
| `DOCS/DB_SCHEMA.md` | Modified | Wpis o migracji 000048 |
| `DOCS/TECH_CHANGELOG.md` | Modified | Wpis 2026-03-14 o optymalizacjach |

## Decisions Made

| Decision | Rationale | Impact |
|----------|-----------|--------|
| Pominięto indeks na allegro_order_status_mappings.allegro_status_code | Tabela już ma UNIQUE KEY na tej kolumnie (migracja 000025) | Brak duplikatu indeksu |
| Data migracji 20260314 zamiast 20260313 | Wykonanie w dniu 2026-03-14 | Numeracja zgodna z datą faktycznego utworzenia |

## Deviations from Plan

### Summary

| Type | Count | Impact |
|------|-------|--------|
| Auto-fixed | 0 | - |
| Scope additions | 0 | - |
| Deferred | 0 | - |

**Total impact:** Plan wykonany zgodnie ze specyfikacją. Jedyna różnica: pominięcie zbędnego indeksu na allegro_order_status_mappings (UNIQUE KEY już istniał).

## Issues Encountered

None

## Verification Results

```
✓ php -l src/Modules/Orders/OrdersRepository.php — No syntax errors
✓ grep correlated subqueries — 0 matches (eliminated)
✓ grep supportsMappedMedia — static + self:: confirmed
✓ Migration 000048 — exists, non-empty, idempotent (IF NOT EXISTS)
```

## Next Phase Readiness

**Ready:**
- OrdersRepository zoptymalizowany — lista zamówień gotowa na wzrost danych
- Plan 07-02 (SSL + cron + migration) niezależny, może być wykonany natychmiast

**Concerns:**
- Migracja 000048 wymaga uruchomienia na środowisku docelowym

**Blockers:**
- None

---
*Phase: 07-pre-expansion-fixes, Plan: 01*
*Completed: 2026-03-14*
