perf(query): start selective list queries from an index
Why
When a user, a script or a background task looks up nodes by an exact attribute value (a device by its name) or by related nodes (the interfaces of one device), Infrahub read every node of the kind and checked the filter on each one. On the large performance dataset (434,000 interfaces), finding the 48 interfaces of one device read 13.7 M database hits and took 4.8 s.
Imagine asking: “Give me all interfaces belonging to router-1.”
There are two ways to answer:
- Look through every interface and check which device it belongs to.
- Find router-1 first, then follow its connections to its interfaces.
This PR lets Infrahub choose the second approach when it looks cheaper.
Measurements
Added cost: every lookup with such a filter first runs one extra query, which counts what the new start would read. It takes about 1 ms per call (1.45 s for 1,316 calls while generating 600 artifacts).
Database hits of the list query, per lookup (small dataset: 500 devices and 19,400 interfaces; large dataset: 10,000 devices and 434,000 interfaces):
| Lookup | Small, without the change | Small, with the change | Large, without the change | Large, with the change |
|---|---|---|---|---|
| Device by name | 27,165 | 115 (−99.6%) | 542,845 | 115 (−99.98%) |
| Interfaces of one device | 507,699 | 14,445 (−97.2%) | 13,714,010 | 14,505 (−99.9%) |
| Interface by device and name | 960,039 | 3,082 (−99.7%) | 21,433,731 | not known |
| Query group by name | 112,950 | not known | 1,259,199 | 100 (−99.99%) |
| Devices by device type | 110,173 | 28,989 (−73.7%) | 2,993,090 | 2,993,090 (0%) |
| Devices by status, most devices match | 92,645 | 92,645 (0%) | 1,852,873 | 1,852,873 (0%) |
"Not known": a background task ran a costlier list query at the same moment, so the measured query is not the lookup.
Time of the list queries on the large dataset:
| Lookup | Without the change | With the change |
|---|---|---|
| Device by name | 468 ms | 157 ms (−66.5%) |
| Interfaces of one device | 4,823 ms | 403 ms (−91.6%) |
| Query group by name | 635 ms | 115 ms (−81.9%) |
Generating 600 artifacts on the small dataset:
| Measure | Without the change | With the change |
|---|---|---|
| Page hits of the list and count queries | 8,513,092 | 889,510 (−89.6%) |
| Page hits of all queries | 32,033,432 | 24,472,755 (−23.6%) |
| Time of the list and count queries | 9.73 s | 8.16 s (−16.1%) |
| Same time plus the 1.45 s of the extra query | 9.73 s | 9.61 s (−1.2%) |
Same results: every lookup returns the same node IDs and counts without the change, with it, and with the new start forced on, on both datasets and on two other branches.
Not measured: the proposed change steps, artifact generation on the large dataset (the runs reached the 300-minute limit of the workflow first), and a database that does not fit in memory.
Runs: lookups on small without and with, on large without and with; artifact generation without and with; same results on small without, with and forced, on large without, with and forced.
🤖 Generated with Claude Code