4 ms·
> Nobody has yet convinced me that recursion has any sustained advantage over looping. most sql systems support recursive queries. I believe recursive code ca
by dangwhy 4y ago
> Nobody has yet convinced me that recursion has any sustained advantage over looping.
most sql systems support recursive queries. I believe recursive code can be analyzed by the system and executed in a the most efficient manner.
Loops have sideffects closely linking them to actual execution, which makes them blackboxes to the system.
- twic 4y agoIf you're talking about recursive common table expressions, those are called recursive, but are really iterative. The PostgreSQL documentation describes the iterative evaluation: https://www.postgresql.org/docs/current/queries-with.html#id-1.5.6.12.6.3 https://www.postgresql.org/docs/current/queries-with.html#id... Note how there is no way to remove a row from the result set once it has been added. That would not be the case with a truly recursive query, because you would be constructing a new result set at every step. As a concrete example, try to write a graph query which finds all nodes exactly three edges from some starting node. That would be trivial with true recursion, but is impossible with a recursive CTE alone.