Skip to content

Add index on site_state_parties.world_heritage_site_id #464

@zigzagdev

Description

@zigzagdev

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions