blcknd.forum

Materialised paths vs. recursive CTEs for comment trees

Tree started by avery 1 month ago · 103 views
A avery Contributor

We store a zero-padded path on each post so a whole thread sorts in tree order with one indexed query. The alternative is a recursive CTE at read time.

Where does each approach break down?

0
M mod Moderator

Paths win on read: one index scan, no recursion. The cost is that moving a subtree means rewriting every descendant.

0
S sam New User

How often do you actually move a subtree though?

0
S sam New User

CTEs keep writes trivial and handle unbounded depth without a cap.

0
A avery Contributor

True, but the query plan degrades badly on hot threads.

0

Log in to join this discussion.