Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The reason why your query is slow is that you have <em>sort whole table</em> on every request. To speed it up significantly you need to avoid sorting big chuck of data, at cost of CPU, HDD/Memory or limitations on pagination logic.</p> <p>As there is not much information about how you table is sorted and if you insert in the middle / delete entries very often, I'll narrow down you question by making these assumptions:</p> <ol> <li><p>I would imagine you have a table storing an archive of articles. New entries are mostly at the bottom of the table, entries from the middle of the table deleted rarely.</p></li> <li><p>You sort always by the same column <code>somecolumn</code> and in the same order, e.g. descending.</p></li> <li><p>You do not have any user entered filters (like article title or author).</p></li> </ol> <p>This makes the table static in terms of the output: each article will be on the same place, unless a new one inserted. New one come to the top of your output. Then you can store <code>ROW_NUMBER() OVER ()</code> as a column. A more convenient solution will be an <code>IDENTITY</code> column. It will speed up things if you create a clustered index on this column</p> <pre><code>alter table add [Record_Number] int null IDENTITY </code></pre> <p>This new column is added as <code>null</code> so you can populate values first time. Then you can make it <code>not null</code>.</p> <p>On the other hand you can last row number very quickly by</p> <pre><code>select @Max_Row = SELECT MAX(row_number) from MyTable </code></pre> <p>Now when you have total number of rows, page size and page number you can select rows you need in one statement without sorting the whole lot.</p> <pre><code>Select * From MyTable Where row_number between (@Max_Row - @Page * @Page_Size) + 1 AND @Max_Row -(@Page - 1) * @Page_Size </code></pre> <p>If you do have a filter in your CTE, then give some more information about how your data is structured, so we can think of a way to limit the scope of CTE.</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.
 

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