Sqlserver paging query the database has been a Sqlserver short board, nothing else, come up with several ways, assuming table ARTICLE, field ID, YEAR ... (other omitted), data 53 210 (real customer data, not quantity ), 30 per paging query, the query section 1500 (ie 45001-45030 pieces of data), field ID clustered index, YEAR no index, Sqlserver version: 2008R2
The first scenario, the most simple and common method:
SELECT TOP 30 * FROM ARTICLE WHERE ID NOT IN (SELECT TOP 45000 ID FROM ARTICLE ORDER BY YEAR DESC, ID DESC) ORDER BY YEAR DESC,ID DESC
The second option:
code show as below:
SELECT * FROM (SELECT TOP 30 * FROM (SELECT TOP 45030 * FROM ARTICLE ORDER BY YEAR DESC, ID DESC) f ORDER BY f.YEAR ASC, f.ID DESC) s ORDER BY s.YEAR DESC,s.ID DESC
The average time required to query 100: 138S
Third option:
code show as below:
SELECT * FROM ARTICLE w1, ( SELECT TOP 30 ID FROM ( SELECT TOP 50030 ID, YEAR FROM ARTICLE ORDER BY YEAR DESC, ID DESC ) w ORDER BY w.YEAR ASC, w.ID ASC ) w2 WHERE w1.ID = w2.ID ORDER BY w1.YEAR DESC, w1.ID DESC
100 times the average query time required: 21S
The fourth program:
code show as below:
SELECT * FROM ARTICLE w1 WHERE ID in ( SELECT top 30 ID FROM ( SELECT top 45030 ID, YEAR FROM ARTICLE ORDER BY YEAR DESC, ID DESC ) w ORDER BY w.YEAR ASC, w.ID ASC ) ORDER BY w1.YEAR DESC, w1.ID DESC
100 times the average query time required: 20S
The fifth program:
code show as below:
SELECT w2.n, w1.* FROM ARTICLE w1, (SELECT TOP 50030 row_number() OVER (ORDER BY YEAR DESC, ID DESC) n, ID FROM ARTICLE ) w2 WHERE w1.ID = w2.ID AND w2.n > 50000 ORDER BY w2.n ASC
100 times the average query time required: 15S
The first query 1000-1030 Records
The first scenario:
code show as below:
SELECT TOP 30 * FROM ARTICLE WHERE ID NOT IN(SELECT TOP 1000 ID FROM ARTICLE ORDER BY YEAR DESC, ID DESC) ORDER BY YEAR DESC,ID DESC
100 times the average query time required: 80s
The second option:
code show as below:
SELECT * FROM ( SELECT TOP 30 * FROM (SELECT TOP 1030 * FROM ARTICLE ORDER BY YEAR DESC, ID DESC) f ORDER BY f.YEAR ASC, f.ID DESC) s ORDER BY s.YEAR DESC,s.ID DESC
100 times the average query time required: 30S
Third option:
code show as below
SELECT * FROM ARTICLE w1, ( SELECT TOP 30 ID FROM ( SELECT TOP 1030 ID, YEAR FROM ARTICLE ORDER BY YEAR DESC, ID DESC ) w ORDER BY w.YEAR ASC, w.ID ASC ) w2 WHERE w1.ID = w2.ID ORDER BY w1.YEAR DESC, w1.ID DESC
100 times the average query time required: 12S
The fourth program:
code show as below:
SELECT * FROM ARTICLE w1 WHERE ID in ( SELECT top 30 ID FROM ( SELECT top 1030 ID, YEAR FROM ARTICLE ORDER BY YEAR DESC, ID DESC ) w ORDER BY w.YEAR ASC, w.ID ASC ) ORDER BY w1.YEAR DESC, w1.ID DESC
100 times the average query time required: 13S
The fifth program:
code show as below:
SELECT w2.n, w1.* FROM ARTICLE w1,( SELECT TOP 1030 row_number() OVER (ORDER BY YEAR DESC, ID DESC) n, ID FROM ARTICLE) w2 WHERE w1.ID = w2.ID AND w2.n > 1000 ORDER BY w2.n ASC
100 times the average query time required: 14S
Thus when the front pages of the query, the efficiency of 3> 4> 5> 2> 1, on page After 5> 4> 3> 1> 2, then according to the user habits, general users look at a search first few page, so choose the 345 program can, if considering Scenario 5 is the best choice, but to pay attention to SQL2000 is not supported row_number () function, due to time constraints and conditions do not deeper, broader testing, Interested in