perf(query): start selective list queries from an index

Merged
SA
10d ago
+1534-250

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.

Jira: IFC-3502 and IFC-3519.

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):

LookupSmall, without the changeSmall, with the changeLarge, without the changeLarge, with the change
Device by name27,165115 (−99.6%)542,845115 (−99.98%)
Interfaces of one device507,69914,445 (−97.2%)13,714,01014,505 (−99.9%)
Interface by device and name960,0393,082 (−99.7%)21,433,731not known
Query group by name112,950not known1,259,199100 (−99.99%)
Devices by device type110,17328,989 (−73.7%)2,993,0902,993,090 (0%)
Devices by status, most devices match92,64592,645 (0%)1,852,8731,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:

LookupWithout the changeWith the change
Device by name468 ms157 ms (−66.5%)
Interfaces of one device4,823 ms403 ms (−91.6%)
Query group by name635 ms115 ms (−81.9%)

Generating 600 artifacts on the small dataset:

MeasureWithout the changeWith the change
Page hits of the list and count queries8,513,092889,510 (−89.6%)
Page hits of all queries32,033,43224,472,755 (−23.6%)
Time of the list and count queries9.73 s8.16 s (−16.1%)
Same time plus the 1.45 s of the extra query9.73 s9.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