Postgresql's new recursive queries at Disqus: 500% speedup
davidcramer.net
davidcramer.net
1 Initial comment
1/1 Re: Initial comment
1/2 Re: Initial comment
1/2/1 Re: Re: Initial comment
1/2/2 ...
These sorts of IDs have the wonderful property that you can efficiently select any subtree using a LIKE query, e.g. "SELECT * FROM comments WHERE path LIKE '1/%'". These queries are efficient because they use string btree indexes. And the results may even be sorted the right way, provided you have monotonically increasing IDs within each level.
I think I read about this technique being used on Slashdot a few years ago, but I can't remember the name or find any reference. Still, it seems like a way to achieve the same benefits that the OP cites for comments with PostgreSQL's ltree support on any database with reasonable string indexing.
Anyone know the name of that technique?
http://www.mjcblog.net/2009/03/full-tree-selects-via-materia...
1/2/3
11/2/3
2/2/3
I wonder if there're ltrees for other DBs.
In other words, recursion still works, and in our tests has proven faster than doing it at the application layer (no matter how you sugar coat it).
Turns out that the average discussion thread has few enough comments that they can be fetched from the db in one call (e.g. by the thread_id). This makes even deeply nested comments fairly simple to query for, and then it is quite easy to rebuild the tree in memory.
I think reddit uses this method too.
I believe the author's point is that using these recursive SQL queries is more efficient, as the DB engine is inherently faster than your (presumably) scripting language. So this solution is for when the in-memory method is failing to scale.
http://highscalability.com/blog/2010/3/23/digg-4000-performa...
Not saying necessarily that the DB engine is less efficient, but rather that it is serves as a limiting factor and anything you can offload onto other servers is a good solution (for highly-visited websites).
As for anything, the answer is "it depends". It depends on what you want to do. In this case that is grab all rows in a specific dataset.
If you in any case want to grab the dataset X, consisting of a given set of rows, asking the database to figure how to fetch all rows in one operation as efficiently as possible will (or at least should on any respectable database engine) be more efficient than having the scripting language sending multiple requests which can only be optimized one by one.
For reference, I've only tried recursive queries in Microsoft SQL Server, but they are pretty fucking neat from a geeky perspective.
The best part of it, is that it is MUCH simpler than either trying to roll your own through cursors or dealing with it in whatever language outside the database.
http://www.reddit.com/r/programming/comments/8q2pz/ask_reddi...
In my case, Appengine does not support recursive SQL queries, so another method had to be used.
http://ss64.com/ora/connectby.html
I used it back in 2002 to build some messageboards and it was pretty effing magical. Glad to see equivalent functionality finally make it to OSS.
If you're interseted in doing something like this, I'd highly recommend this book: http://www.amazon.com/Hierarchies-Smarties-Kaufmann-Manageme...
He has two really good chapters that specifically building a comment system. It takes a bit of work, and you end up pre-computing the comment system on INSERT's instead of SELECT's. But, you can build a comment system that pulls your entire comment tree in order using a single SELECT ... FROM COMMENTS ... ORDER BY comment.node_left;
I'm not really sure how to do that in SQL.