perf(merge): read the edges of the merged branch through the branch index
Merge queries that read the whole graph
This pull request is stacked on #11709 and targets its branch.
On the synthetic dataset described in #11709 (10,000 devices with 48 interfaces each, 490,794 nodes and 13,684,466 relationships), the merge of a proposed change with one changed interface description still took about 13 s with #11709. Four bulk merge queries took 11.1 s of it in the 4-interface run, and their plans started from a scan of a whole label:
BulkMergeNodeExistenceQuery: everyNodevertex, then everyAttributevertex.BulkMergeRelationshipEdgesQuery: everyRelationshipvertex.BulkMergeAttributePropertyEdgesQuery: everyAttributevertex. Its pattern matches 4 relationship types at once, so no index can serve it.BulkMergeRelationshipPropertyEdgesQuery: everyRelationshipvertex, with the same 4-type pattern.
This addresses the part of #11706 that #11709 leaves open.
Changes to the four bulk merge queries
All in backend/infrahub/core/diff/query/bulk_merge.py:
BulkMergeNodeExistenceQueryhints thebranchindex ofIS_PART_OFand ofHAS_ATTRIBUTE. These reads no longer name the labels their edge type implies (NodeandRootforIS_PART_OF,NodeforHAS_ATTRIBUTE): with theNodelabel onHAS_ATTRIBUTE, the planner joined the index seek with a scan of everyNodevertex to check it. The onlyIS_PART_OFedges that do not link aNodeto theRootlink aBranch, and they have nobranchproperty.BulkMergeRelationshipEdgesQueryhints thebranchindex ofIS_RELATED.BulkMergeAttributePropertyEdgesQueryreads each ofHAS_VALUE,IS_PROTECTED,HAS_OWNERandHAS_SOURCE(VERSIONED_VALUE_EDGE_TYPES) through itsbranchindex, joined withUNION ALL, then finds the node of each attribute. TheHAS_VALUEread does not name theAttributelabel, which that edge type implies.BulkMergeRelationshipPropertyEdgesQuerydoes the same forIS_PROTECTED,HAS_OWNERandHAS_SOURCE. It no longer readsHAS_VALUE, which never starts at aRelationshipvertex.- The writes that follow these reads are unchanged.
BulkMergeCardinalityOneResolutionQueryis unchanged: it starts from the resolutions it receives and finds their nodes by uuid.
This pull request adds no index, no migration and no schema change. It uses the branch indexes that #11709 relies on.
Times on the synthetic dataset
Same setup as in #11709. The values are the second of two runs.
| Step | 1 interface, #11709 | 1 interface, this branch | 4 interfaces on 2 devices, #11709 | 4 interfaces on 2 devices, this branch |
|---|---|---|---|---|
DiffUpdate | 0.14 s | 0.11 s | 0.13 s | 0.13 s |
BranchRebase | 2.09 s | 2.07 s | 2.09 s | 2.08 s |
CoreProposedChangeMerge | 14.3 s | 2.14 s | 13.2 s | 3.13 s |
The #11709 values come from e3c172e for 1 interface and from 51ac5a0 for 4 interfaces; the commits in between do not change these queries.
Database time of the four queries in the 4-interface run, from the traces:
| Query | #11709 | This branch |
|---|---|---|
BulkMergeAttributePropertyEdgesQuery | 4,820 ms | 3 ms |
BulkMergeNodeExistenceQuery | 4,685 ms | 1 ms |
BulkMergeRelationshipEdgesQuery | 1,022 ms | 1 ms |
BulkMergeRelationshipPropertyEdgesQuery | 609 ms | 1 ms |
With this branch at 35a3b5a, the merge spends 0.15 s in 121 database queries; the rest of its 3.1 s is outside the database. At 7a3a52d, the second 1-interface run took 2.10 s for the merge, and the four queries 1 to 2 ms each. EXPLAIN of the four queries on the synthetic dataset at 7a3a52d shows a relationship index seek on branch for each read, and no scan of a label.
The merge was not measured on the large dataset of the performance tests: as described in #11709, the proposed change merge test of opsmill/infrahub-private-tests#84 fails before the merge.
Tests and checks
backend/tests/component/core/diff/query/test_bulk_merge_query_plans.pyloads 300 people with a car each onmainand refreshes the planner statistics. For each of the four queries, it asserts that the plan has no scan of everyNode,AttributeorRelationshipvertex, of every relationship of a type, or of a whole index, and that it seeks thebranchindex of the source branch for exactly the expected edge types. On a graph of a few nodes, the planner already uses thebranchindex without a hint, so the test needs that data; it takes about 25 s.- The 4 cases fail with the queries of #11709. Removing only the
IS_PART_OFhint does not fail the test, because on this data the planner still seeks that index without the hint. - Results at 7a3a52d:
backend/tests/component/core/diff/,backend/tests/component/core/merge/,test_branch_merge.py,backend/tests/component/core/agnostic_retirement/andbackend/tests/component/core/changelog/: 422 passed.mypyonbackend: no issues.
uv run pytest backend/tests/component/core/diff/ backend/tests/component/core/merge/ backend/tests/component/core/test_branch_merge.py
Documentation and changelog updates
dev/knowledge/backend/database-indexes.md: a rule to leave out a label that the edge type already implies on a hinted read, and the bulk merge queries that read the source branch named as examples of reads of one branch.- No changelog fragment: the fragment of #11709 states that merging a branch or a proposed change is faster on large databases, which covers this change.
Checklist
- Tests added/updated
- Changelog entry added (
uv run towncrier create ...): covered by the fragment of #11709 - External docs updated (if user-facing or ops-facing change): no user-facing documentation describes these queries
- Internal .md docs updated (internal knowledge and AI code tools knowledge)
- I have reviewed AI generated content