造轮子:论Top与ROW_NUMBER读取第一页的效率问题

http://www.cnblogs.com/Leo_wl/p/4921799.html

SELECT TOP 30 * FROM users WHERE nID>2000 And nID<50000 ORDER BY nID DESC;

SELECT * FROM (SELECT ROW_NUMBER() OVER(ORDER BY nID asc) AS rownum,* FROM users Where nID>2000 And nID<50000 ) AS D
        WHERE rownum>0 AND rownum<31;
SELECT * FROM (SELECT ROW_NUMBER() OVER(ORDER BY nID asc) AS rownum,* FROM users Where nID>2000 And nID<50000 ) AS D
        WHERE rownum BETWEEN 1 AND 30;

他们的不同之处在于“提取N条记录”的这个操作,Top N和 WHERE rownum>0 AND rownum<31。

它们具体是如何运行我讲不出理论,但是我可以做个比方:

体育老师让我们跑30步的距离,Top N的做法就是跑30步,自己边跑边数;WHERE rownum>0 AND rownum<31 相当于老师在30步的位置花了个标记,你只管死跑,到了那个标记就相当于跑了30步。我想做了标记死跑这个要快点吧,:)



你可能感兴趣的:(造轮子:论Top与ROW_NUMBER读取第一页的效率问题)