Skip to content

perf(database): index entity_relations.relation_id for inbound graph traversal - #194

Merged
brandPittCode merged 2 commits into
mainfrom
perf/index-entity-relations-relation-id
Nov 17, 2026
Merged

brandPittCode merged 2 commits into
mainfrom
perf/index-entity-relations-relation-id

Conversation

@mmornati

Copy link
Copy Markdown
Contributor

PR Description

What this PR Provides

  • Adds Flyway migration V7_2__add_entity_relations_relation_id_index.sql: CREATE INDEX IF NOT EXISTS idx_entity_relations_relation_id ON entity_relations (relation_id).

Why

  • The recursive graph query (JpaEntityRepository.findEntityIdsInGraph) resolves inbound edges with relation_target_entities → entity_relations ON relation_id.
  • entity_relations only has indexes that lead with entity_id (the primary key and idx_entity_relations_entity_id), so that join was a sequential scan for every node of the traversal frontier.
  • The query runs on every entity list page, not only on the graph endpoint. In a captured list request it took 156 ms of a 166 ms total database time.

Measurements

15,200 entities, 22,500 relation edges, local Docker (2 CPU, 1.5 GB), ab with 16 concurrent clients. One run on a laptop: indicative, not a benchmark suite.

Endpoint p50 before p50 after
GET /entities/service?size=20 263–378 ms 87 ms
GET /entities/team?size=20 188 ms 68 ms
GET /entities/service?q=property.status=production 460 ms 100 ms
graph, depth 3, BIDIRECTIONAL 64 ms 28 ms

Deployment note

  • CREATE INDEX takes a lock that blocks writes on entity_relations while it builds. On the test data set (17k rows) it is instantaneous. For a large production table, consider creating the index CONCURRENTLY by hand first; the migration then becomes a no-op thanks to IF NOT EXISTS.

Not covered here

  • size=500 list pages still take about 1.8 s, and a depth-6 BIDIRECTIONAL traversal from a hub node took 9 s for 401 nodes: the CTE deduplicates per (node, depth) and revisits nodes at each level. Both need a query change, not an index.
  • Whether list pages should run the graph query at all.

Tests

  • EntityGraph* and EntityControllerTest against Testcontainers Postgres with the migration applied: 178 tests, 0 failures.

Fixes

Found during the idp-core audit of 2026-11-17.

Review

The reviewer must double-check these points:

  • The reviewer has tested the feature
  • The reviewer has reviewed the implementation of the feature
  • The documentation has been updated

…traversal

The recursive graph query resolves inbound edges by joining
relation_target_entities to entity_relations on relation_id. entity_relations
only had indexes leading with entity_id, so that join was a sequential scan
per frontier node. The query runs on every entity list page.

Measured on 15k entities / 22k edges (local Docker, 2 CPU):
- list 20 services: p50 263-378 ms -> 87 ms
- property filter:  p50 460 ms -> 100 ms
- graph depth 3 bidirectional: p50 64 ms -> 28 ms
@github-code-quality

github-code-quality Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Code coverage overview

Languages: Java

Java / code-coverage/jacoco

main 6bbce97 perf/index-entity-re... 79e8cff +/-
91% 91% 0%

ℹ️ Calculations represent line coverage. Results may change as calculations are updated.


Updated November 17, 2026 14:47 UTC

@mmornati
mmornati marked this pull request as ready for review November 17, 2026 06:53
@mmornati mmornati self-assigned this Nov 17, 2026
@sonarqubecloud

Copy link
Copy Markdown

@brandPittCode
brandPittCode merged commit 38620a2 into main Nov 17, 2026
18 checks passed
@brandPittCode
brandPittCode deleted the perf/index-entity-relations-relation-id branch November 17, 2026 14:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants