Repository navigation
perf(database): index entity_relations.relation_id for inbound graph traversal - #194
Merged
Merged
Conversation
…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
brandPittCode
approved these changes
Nov 17, 2026
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



PR Description
What this PR Provides
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
JpaEntityRepository.findEntityIdsInGraph) resolves inbound edges withrelation_target_entities → entity_relations ON relation_id.entity_relationsonly has indexes that lead withentity_id(the primary key andidx_entity_relations_entity_id), so that join was a sequential scan for every node of the traversal frontier.Measurements
15,200 entities, 22,500 relation edges, local Docker (2 CPU, 1.5 GB),
abwith 16 concurrent clients. One run on a laptop: indicative, not a benchmark suite.GET /entities/service?size=20GET /entities/team?size=20GET /entities/service?q=property.status=productionBIDIRECTIONALDeployment note
CREATE INDEXtakes a lock that blocks writes onentity_relationswhile it builds. On the test data set (17k rows) it is instantaneous. For a large production table, consider creating the indexCONCURRENTLYby hand first; the migration then becomes a no-op thanks toIF NOT EXISTS.Not covered here
size=500list pages still take about 1.8 s, and a depth-6BIDIRECTIONALtraversal 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.Tests
EntityGraph*andEntityControllerTestagainst 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: