Note that there are some explanatory texts on larger screens.

plurals
  1. POHow does SO calculates number of votes, answers and views in database?
    primarykey
    data
    text
    <p>I think SO and other question/answer systems are perfect example for advanced TSQL techniques. I am wondering how SO and other questions/answers systems work with DB tables. I am particulary interested what is the TSQL select for list of questions where you also want to show number of votes and answers. </p> <p>Table <code>question</code> would be specified as this</p> <pre><code>id int title varchar id_user int </code></pre> <p>table <code>votes</code> so you can monitor users votes for particular question</p> <pre><code>id_user int id_question int vote int </code></pre> <p>table 'answer' with replies to question</p> <pre><code>id int id_parent int </code></pre> <p>What would select look like to get result from <code>question</code> like this:</p> <pre><code>id int title varchar votes_count int answers_count int </code></pre> <p>I was thinking about two methods</p> <ol> <li>Table <code>question</code> would recalculate with every vote or answer and update table fields <code>votes_count</code> and <code>answers_count</code> directly in <code>question</code> table. That means table <code>question</code> would have also <code>votes_count int, answers_count int</code></li> <li>Create select which would join, calculate <code>count</code> directly in <code>votes</code> or <code>answers</code> tables and just show results in <code>question</code> table. How much speed would be lost by counting with every select all the records for votes and answers?</li> </ol> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    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. 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