准备测试数据: SELECT TOP 100000 * INTO tbicxx_100000 FROM tbicxx
页数靠后测试
分页查询每页30条,查询第1500页(即第45001-45030条数据)
第一种方案、最简单、普通的方法:
1 2 3 4 5 6 7 8
SELECT TOP 30 * FROM tbicxx_100000 WHERE CICID NOTIN ( SELECT TOP 45000 CICID FROM tbicxx_100000 ORDERBY DFKRQ DESC, CICID DESC ) ORDERBY DFKRQ DESC, CICID DESC
平均查询100次所需时间:45s/19s(45s秒表示原文所载数据,19s为实测数据)
第二种方案:
1 2 3 4 5 6 7 8 9 10 11 12
SELECT* FROM ( SELECT TOP 30 * FROM (SELECT TOP 45030*FROM tbicxx_100000 ORDERBY DFKRQ DESC, CICID DESC) f ORDERBY f.DFKRQ ASC, f.CICID DESC ) s ORDERBY s.DFKRQ DESC, s.CICID DESC
SELECT* FROM tbicxx_100000 w1 WHERE CICID IN ( SELECT TOP 30 CICID FROM ( SELECT TOP 45030 CICID, DFKRQ FROM tbicxx_100000 ORDERBY DFKRQ DESC, CICID DESC ) w ORDERBY w.DFKRQ ASC, w.CICID ASC ) ORDERBY w1.DFKRQ DESC, w1.CICID DESC
平均查询100次所需时间:20S/10s
第五种方案:
1 2 3 4 5 6 7 8 9 10 11 12
SELECT w2.n, w1.* FROM tbicxx_100000 w1, ( SELECT TOP 50030 ROW_NUMBER() OVER (ORDERBY DFKRQ DESC, CICID DESC) n, CICID FROM tbicxx_100000 ) w2 WHERE w1.CICID = w2.CICID AND w2.n >50000 ORDERBY w2.n ASC
平均查询100次所需时间:15S/10s
页数靠前测试
查询第1000-1030条记录
第一种方案:
1 2 3 4 5 6 7 8
SELECT TOP 30 * FROM tbicxx_100000 WHERE CICID NOTIN ( SELECT TOP 1000 CICID FROM tbicxx_100000 ORDERBY DFKRQ DESC, CICID DESC ) ORDERBY DFKRQ DESC, CICID DESC
平均查询100次所需时间:80s/15s
第二种方案:
1 2 3 4 5 6 7 8 9 10 11 12
SELECT* FROM ( SELECT TOP 30 * FROM (SELECT TOP 1030*FROM tbicxx_100000 ORDERBY DFKRQ DESC, CICID DESC) f ORDERBY f.DFKRQ ASC, f.CICID DESC ) s ORDERBY s.DFKRQ DESC, s.CICID DESC
SELECT* FROM tbicxx_100000 w1, ( SELECT TOP 30 CICID FROM ( SELECT TOP 1030 CICID, DFKRQ FROM tbicxx_100000 ORDERBY DFKRQ DESC, CICID DESC ) w ORDERBY w.DFKRQ ASC, w.CICID ASC ) w2 WHERE w1.CICID = w2.CICID ORDERBY w1.DFKRQ DESC, w1.CICID DESC
平均查询100次所需时间:12S/3s
第四种方案:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
SELECT* FROM tbicxx_100000 w1 WHERE CICID IN ( SELECT TOP 30 CICID FROM ( SELECT TOP 1030 CICID, DFKRQ FROM tbicxx_100000 ORDERBY DFKRQ DESC, CICID DESC ) w ORDERBY w.DFKRQ ASC, w.CICID ASC ) ORDERBY w1.DFKRQ DESC, w1.CICID DESC
平均查询100次所需时间:13S/5s
第五种方案:
1 2 3 4 5 6 7 8 9 10 11 12
SELECT w2.n, w1.* FROM tbicxx_100000 w1, ( SELECT TOP 1030 ROW_NUMBER() OVER (ORDERBY DFKRQ DESC, CICID DESC) n, CICID FROM tbicxx_100000 ) w2 WHERE w1.CICID = w2.CICID AND w2.n >1000 ORDERBY w2.n ASC
IF ISNULL(@sys_Where,'') ='' SET@new_where1 =' ' ELSE SET@new_where1 =' WHERE '+@sys_Where
IF ISNULL(@sys_Order,'') <>'' BEGIN SET@new_order1 =' ORDER BY '+ Replace(@sys_Order,'desc','') SET@new_order1 = Replace(@new_order1,'asc','desc')
SET@new_order2 =' ORDER BY '+@sys_Order END ELSE BEGIN SET@new_order1 =' ORDER BY '+@sys_Key+' DESC' SET@new_order2 =' ORDER BY '+@sys_Key+' ASC' END
SET@SqlCount='SELECT @RCount=COUNT(1),@PCount=CEILING((COUNT(1)+0.0)/'---CEILING 向上取整 floor向下取整 +CAST(@sys_PageSize AS NVARCHAR)+') FROM '+@sys_Table +@new_where1
print @SqlCount EXEC SP_EXECUTESQL @SqlCount,N'@RCount INT OUTPUT,@PCount INT OUTPUT', @RCount OUTPUT,@PCount OUTPUT
IF @sys_PageIndex >CEILING((@RCount+0.0)/@sys_PageSize) --如果输入的当前页数大于实际总页数,则把实际总页数赋值给当前页数 BEGIN SET@sys_PageIndex =CEILING((@RCount+0.0)/@sys_PageSize) END
set@sql='select '+@sys_fields +' from '+@sys_Table +' w1 ' +' where '+@sys_Key +' in (' +'select top '+ ltrim(str(@sys_PageSize)) +' '+@sys_Key +' from ' +'(' +'select top '+ ltrim(STR(@sys_PageSize *@sys_PageIndex +@sys_Begin)) +' '+@sys_Key +' FROM ' +@sys_Table +@new_where1 +@new_order2 +') w '+@new_order1 +') '+@new_order2