<p>What would be the best way to design a threaded commenting system so that it doesn't hammer the database?</p>

[{'answer_id': 36487, 'author': 'TheSmurf', 'author_id': 1975282, 'author_profile': 'https://Stackoverflow.com/users/1975282', 'pm_score': 0, 'selected': False, 'text': "<p>What I normally do in this case is to have a single thread that is responsible for putting the data into the database, and have all auxiliary threads report to that thread, which then queues up the data, and writes it either serially, or in batches (depending on the requirements, and how much database activity I'm willing to put up with).</p>\n"}, {'answer_id': 36497, 'author': 'aryeh', 'author_id': 3288, 'author_profile': 'https://Stackoverflow.com/users/3288', 'pm_score': 1, 'selected': False, 'text': "<p>I'm guessing your question is about arranging the system so you don't have to work as:</p>\n\n<ol>\n<li>Select all the top level comments</li>\n<li>Select all comments whose parents were found in the step prior</li>\n<li>Select all comments whose parents were found in the step prior</li>\n<li>... repeat until no comments found</li>\n</ol>\n\n<p>I would suggest desiging the db table with a thread key which would be string of all the parents of that post.  You'd have to limit your discussion to a certain depth, but your sql statements would be straight selects and order by the thread key, giving you back threaded comments.  Less taxing on your DB and Webserver.</p>\n\n<p>A thread key would be something like it's current post id joined onto it's parent's thread key with a delimiter.</p>\n\n<p>How does that sound?</p>\n"}, {'answer_id': 36556, 'author': 'Christian Oudard', 'author_id': 3757, 'author_profile': 'https://Stackoverflow.com/users/3757', 'pm_score': 0, 'selected': False, 'text': '<p>I\'m guessing you have something resembling a "comments" table, with a foreign key to itself, pointing to the parent comment of each row. This makes the threaded comments into a tree structure with the thread starter as the tree root.</p>\n\n<p>So we can rephrase the question as "What is the best way to select a tree structure from a database?". Well I won\'t assume to know the best way, but my first inclination (probably wrong) is to use a stored procedure to walk the tree, and compile a list of rows to return. It still takes multiple select statements to get all the children, but it\'s only one database round trip.</p>\n\n<p>Aryeh\'s method with the accumulated parent list is probably better :)</p>\n'}, {'answer_id': 36565, 'author': 'Matt Rogish', 'author_id': 2590, 'author_profile': 'https://Stackoverflow.com/users/2590', 'pm_score': 1, 'selected': False, 'text': '<p>This website lists some common techniques:\n<a href="http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/" rel="nofollow noreferrer">http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/</a></p>\n\n<p>I\'d do the "nested set" model, but have multiple roots (e.g. each "topic" is a new tree). It\'s very fast, simple to query, but complicated to maintain...</p>\n'}, {'answer_id': 36629, 'author': 'Mark Harrison', 'author_id': 116, 'author_profile': 'https://Stackoverflow.com/users/116', 'pm_score': 2, 'selected': False, 'text': '<p><strong>SELECT ... START WITH ... CONNECT BY</strong></p>\n\n<p>Oracle has an extension to SELECT that allows easy tree-based retrieval.</p>\n\n<p>This query will traverse a table where the nesting relationship is stored in <strong>parent</strong> and <strong>child</strong> columns.</p>\n\n<pre><code>select * from my_table\n    start with parent = :TOP_ARTICLE\n    connect by prior child = parent;\n</code></pre>\n\n<p><a href="http://www.adp-gmbh.ch/ora/sql/connect_by.html" rel="nofollow noreferrer">http://www.adp-gmbh.ch/ora/sql/connect_by.html</a></p>\n'}, {'answer_id': 36633, 'author': 'Carl Meyer', 'author_id': 3207, 'author_profile': 'https://Stackoverflow.com/users/3207', 'pm_score': 2, 'selected': False, 'text': '<p><a href="http://www.sitepoint.com/print/hierarchical-data-database" rel="nofollow noreferrer">Modified pre-order tree traversal</a> (or what Matt refers to as "nested set") is the way to go.</p>\n\n<p>If you happen to be working in Django, there\'s a third-party app, <a href="http://code.google.com/p/django-mptt/" rel="nofollow noreferrer">django-mptt</a>, that makes implementing MPTT in your models a one-liner.</p>\n'}, {'answer_id': 488551, 'author': 'Gareth Farrington', 'author_id': 2021, 'author_profile': 'https://Stackoverflow.com/users/2021', 'pm_score': 0, 'selected': False, 'text': '<p>I have to second Carl Meyer\'s suggestions to use the <a href="http://www.sitepoint.com/print/hierarchical-data-database/" rel="nofollow noreferrer" title="Modified Preorder">link text</a> technique. I\'m working on a system like this right now but with some further optimizations for a forums. </p>\n\n<p>In forum systems that support replies you will frequently be doing inserts into the middle of the tree which yields poor performance. To reduce the pain I\'m working on allowing gaps in the number line. This works like pre-allocating memory in an array list. The cost for shifting the tree to the right is the same for 1 and for 100. So on successive replies (which are more likely) I can update fewer tree nodes and they will be much faster.</p>\n\n<p>The downsize is the counting of descendant nodes (number of replies below this post) by comparing the current nodes left and right values will break. This information can be cached in the data structure to make that fast. So on insert I will have to update all ancestor nodes with new counts. Still far fewer nodes will be updated and the result will be much faster average case insert times.</p>\n\n<p>Multiple trees are being stored in the same table. Each tree has a unique tree id, one per topic. Smaller trees update much faster.</p>\n\n<p>Hope That Helps</p>\n'}]
['https://Stackoverflow.com/questions/36475', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1745/']