Problem
The site_state_parties pivot table has a composite primary key of (state_party_code, world_heritage_site_id).
When Eloquent eager-loads the countries relation, it runs:
SELECT countries.*, site_state_parties.world_heritage_site_id, site_state_parties.is_primary ...
FROM countries
JOIN site_state_parties ON countries.state_party_code = site_state_parties.state_party_code
WHERE site_state_parties.world_heritage_site_id IN (1, 2, 3, ... 30)
Because world_heritage_site_id is the second column in the composite PK, MySQL cannot use the PK index to satisfy the WHERE world_heritage_site_id IN (...) condition. This causes a full table scan of site_state_parties on every list page request.
Fix
Add a standalone index on site_state_parties.world_heritage_site_id via a new migration.
$table->index('world_heritage_site_id', 'idx_ssp_world_heritage_site_id');
Expected Impact
- Countries eager load goes from full table scan → index seek
- Directly reduces latency of
GET /api/world-heritages (list) and search results
Parent Issue
Part of #463
Problem
The
site_state_partiespivot table has a composite primary key of(state_party_code, world_heritage_site_id).When Eloquent eager-loads the
countriesrelation, it runs:Because
world_heritage_site_idis the second column in the composite PK, MySQL cannot use the PK index to satisfy theWHERE world_heritage_site_id IN (...)condition. This causes a full table scan ofsite_state_partieson every list page request.Fix
Add a standalone index on
site_state_parties.world_heritage_site_idvia a new migration.Expected Impact
GET /api/world-heritages(list) and search resultsParent Issue
Part of #463