3 ms·
For trees that have few inserts/deletes and a large number of reads (like most threaded comments), I'd probably go with a modified preorder tree traversal (MPTT
by jperras 12y ago
For trees that have few inserts/deletes and a large number of reads (like most threaded comments), I'd probably go with a modified preorder tree traversal (MPTT) implementation.
With an MPTT structure you can query the entire tree (or subsets of said tree) with a single non-recursive SQL query, at the expense of having to use multiple statements to perform inserts and/or deletions. If you're really concerned about insert performance (which can be an issue for large trees), there are always hybrid approaches where you use adjacency lists for maintaining newly inserted nodes and then run an out-of-band operation to rewrite the relevant MPTT tree values.
*note: MPTTs used to be all the rage maybe 6-8 years ago, and it's like everyone forgot about them. What's up with that?