SurrealQL FETCH clause depth and performance limits
0 reputation · 27 May 2022, 18:25 UTC
0 reputation · 27 May 2022, 18:25 UTC
SurrealDB uses the FETCH clause to resolve record IDs into full objects, effectively flattening graph relationships into a single query response to minimize network round-trips.
When dealing with deeply nested record links, there is a potential for performance degradation as the traversal complexity increases. It is unclear how the database manages memory and execution time when FETCH is applied to multiple levels of nested relationships across large datasets.
Is there a documented limit on the depth of nested FETCH operations? How does the engine prioritize resolution when multiple linked records are fetched simultaneously?
SurrealDB does not publish a documented maximum depth for nested FETCH operations. The engine resolves record links recursively until no further references remain, but any internal recursion ceiling is not exposed in the public API. Treat deep nesting as unbounded only until you verify it on your target version.
| Statement | Status | Basis |
|---|---|---|
| No published hard depth limit for FETCH | Not documented | Public references do not specify a numeric ceiling |
| Implicit recursion ceiling exists to prevent stack overflow | Likely, unpublished | Exact value is not exposed |
| Multiple linked records resolve breadth-first | Likely | Immediate references are fetched before deeper levels |
| Cost grows roughly linearly with depth | Likely | Each level adds disk reads and object assembly |
| Circular references cause infinite recursion | Unconfirmed | May abort or timeout; test only in a disposable environment |
SELECT * FROM my_table FETCH depth=5;
Cap FETCH depth at the shallowest level that satisfies the query, or denormalize frequently traversed edges. Avoid unbounded recursive resolution for large datasets because each additional level multiplies read amplification and memory pressure.
Which SurrealDB version are you running? Internal limits and traversal behavior may vary across releases, and the version determines whether an explicit depth cap is available or safe to use.
Use comments to ask for clarification. Post a solution as an answer.
33,775 reputation · 28 May 2022, 04:29 UTC
SurrealDB caps the FETCH depth at 10 by default. The limit is enforced by the server, and exceeding it triggers the explicit error FETCH depth exceeded rather than silently truncating the result set.
The max_fetch_depth setting in config.yml can be tweaked to raise or lower this threshold, but values that are too high can cause excessive memory consumption and long query times.
So, for a graph with 1,000 first‑level links and a depth of 5, you can expect a linear increase in latency compared to a depth of 1, while the number of read operations remains proportional to the total ID count.