Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>EDIT</strong>: Your one-column indices are not enough. You would need to, at least, cover the three involved columns.</p> <p>More advanced solution: replace <code>replycount &gt; 1</code> with <code>hasreplies = 1</code> by creating a new <code>hasreplies</code> field that equals 1 when <code>replycount &gt; 1</code>. Once this is done, create an index on the three columns, in that order: <code>INDEX(forumid, hasreplies, dateline)</code>. Make sure it's a BTREE index to support ordering.</p> <p>You're selecting based on: </p> <ul> <li>a given <code>forumid</code></li> <li>a given <code>hasreplies</code></li> <li>ordered by <code>dateline</code></li> </ul> <p>Once you do this, your query execution will involve: </p> <ul> <li>moving down the BTREE to find the subtree that matches <code>forumid = X</code>. This is a logarithmic operation (duration : log(number of forums)). </li> <li>moving further down the BTREE to find the subtree that matches <code>hasreplies = 1</code> (while still matching <code>forumid = X</code>). This is a constant-time operation, because <code>hasreplies</code> is only 0 or 1. </li> <li>moving through the dateline-sorted subtree in order to get the required results, without having to read and re-sort the entire list of items in the forum.</li> </ul> <p>My earlier suggestion to index on <code>replycount</code> was incorrect, because it would have been a range query and thus prevented the use of a <code>dateline</code> to sort the results (so you would have selected the threads with replies very fast, but the resulting million-line list would have had to be sorted completely before looking for the 100 elements you needed).</p> <p><strong>IMPORTANT</strong>: while this improves performance in all cases, your huge OFFSET value (10000!) is going to decrease performance, because MySQL does not seem to be able to skip ahead despite reading straight through a BTREE. So, the larger your OFFSET is, the slower the request will become. </p> <p>I'm afraid the OFFSET problem is not automagically solved by spreading the computation over several computations (how do you skip an offset in parallel, anyway?) or moving to NoSQL. All solutions (including NoSQL ones) will boil down to simulating OFFSET based on <code>dateline</code> (basically saying <code>dateline &gt; Y LIMIT 100</code> instead of <code>LIMIT Z, 100</code> where <code>Y</code> is the date of the item at offset <code>Z</code>). This works, and eliminates any performance issues related to the offset, but prevents going directly to page 100 out of 200.</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