Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A relational database should handle this "adjacency list" model just fine.</p> <p>First of all, don't use 0 in the <code>parent_comment_id</code> of the "root" comment, use NULL. Then you can build a FOREIGN KEY from <code>parent_comment_id</code> to <code>comment_id</code> that would prevent you from mistakenly attaching a reply to a non-existent comment.</p> <blockquote> <p>it will require me to go through the whole comments table just to find whether a comment has replies or not</p> </blockquote> <p>Assuming you have indexed the <code>parent_comment_id</code> (which <a href="https://stackoverflow.com/a/1721893/533120">InnoDB did automatically</a> if you created the FK above), finding the first level of replies to the given comment will require an index <strong>range scan</strong>. To understand index range scans and why they are efficient, you first need to understand the <a href="http://use-the-index-luke.com/sql/anatomy" rel="nofollow noreferrer">Anatomy of an SQL Index</a>.</p> <p>Finding the second level would require another range scan etc. Unfortunately, MySQL doesn't support the recursive query that would allow you to do all that in a single database round-trip, but it should still be fairly efficient.</p> <p>If you have preformed <strong>measurements</strong> and concluded that's a problem, there are other strategies for representing hierarchies (with different trade-offs), such as "nested sets" and "closures". Take a look at <a href="http://www.slideshare.net/billkarwin/models-for-hierarchical-data" rel="nofollow noreferrer">this presentation by Bill Karwin</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload