当前位置:首页 » 存储配置 » mssql分页存储过程

mssql分页存储过程

发布时间: 2025-07-09 13:17:00

1. 如果在数据库中有大数据量,而我们用分页存储过程,怎么样才能效率高

--------------------------------
--关于分页储存的效率问题
--5个存储过程都是采用不同的方式
--------------------------------
------------------------------------------
--利用select top 和select not in进行分页--
------------------------------------------
create procere proc_paged_with_notin --利用select top and select not in
(
@pageIndex int, --页索引
@pageSize int --每页记录数
)
as
begin
set nocount on;
declare @timediff datetime --耗时
declare @sql nvarchar(500)
select @timediff=Getdate()
set @sql='select top '+str(@pageSize)+' * from tb_TestTable where(ID not in(select top '+str(@pageSize*@pageIndex)+' id from tb_TestTable order by ID ASC)) order by ID'
execute(@sql) --因select top后不支技直接接参数,所以写成了字符串@sql
select datediff(ms,@timediff,GetDate()) as 耗时
set nocount off;
endexec proc_paged_with_notin 10000,10
--------------------------------------
--利用select top 和 select max(列键)--
--------------------------------------
create procere proc_paged_with_selectMax --利用select top and select max(列)
(
@pageIndex int, --页索引
@pageSize int --页记录数
)
as
begin
set nocount on;
declare @timediff datetime
declare @sql nvarchar(500)
select @timediff=Getdate()
set @sql='select top '+str(@pageSize)+' * From tb_TestTable where(ID>(select max(id) From (select top '+str(@pageSize*@pageIndex)+' id From tb_TestTable order by ID) as TempTable)) order by ID'
execute(@sql)
select datediff(ms,@timediff,GetDate()) as 耗时
set nocount off;
end--------------------------------------------------------
--利用select top和中间变量--此方法因网上有人说效果最佳--
--------------------------------------------------------
create procere proc_paged_with_Midvar --利用ID>最大ID值和中间变量
(
@pageIndex int,
@pageSize int
)
as
declare @count int
declare @ID int
declare @timediff datetime
declare @sql nvarchar(500)
begin
set nocount on;
select @count=0,@ID=0,@timediff=getdate()
select @count=@count+1,@ID=case when @count<=@pageSize*@pageIndex then ID else @ID end from tb_testTable order by id
set @sql='select top '+str(@pageSize)+' * from tb_testTable where ID>'+str(@ID)
execute(@sql)
select datediff(ms,@timediff,getdate()) as 耗时
set nocount off;
end
---------------------------------------------------------------------------------------
--利用Row_number() 此方法为SQL server 2005中新的方法,利用Row_number()给数据行加上索引--
---------------------------------------------------------------------------------------
create procere proc_paged_with_Rownumber --利用SQL 2005中的Row_number()
(
@pageIndex int,
@pageSize int
)
as
declare @timediff datetime
begin
set nocount on;
select @timediff=getdate()
select * from (select *,Row_number() over(order by ID asc) as IDRank from tb_testTable) as IDWithRowNumber where IDRank>@pageSize*@pageIndex and IDRank<@pageSize*(@pageIndex+1)
select datediff(ms,@timediff,getdate()) as 耗时
set nocount off;
end
--------------------------
--利用临时表及Row_number--
--------------------------
create procere proc_CTE --利用临时表及Row_number
(
@pageIndex int, --页索引
@pageSize int --页记录数
)
as
set nocount on;
declare @ctestr nvarchar(400)
declare @strSql nvarchar(400)
declare @datediff datetime
begin
select @datediff=GetDate()
set @ctestr='with Table_CTE as
(select ceiling((Row_number() over(order by ID ASC))/'+str(@pageSize)+') as page_num,* from tb_TestTable)';
set @strSql=@ctestr+' select * From Table_CTE where page_num='+str(@pageIndex)
end
begin
execute sp_executesql @strSql
select datediff(ms,@datediff,GetDate())
set nocount off;
end
我们分别在每页10条数据的情况下在第2页,第1000页,第10000页,第100000页,第199999页进行测试,耗时单位:ms 每页测试5次取其平均值 存过第2页耗时第1000页耗时第10000页耗时第100000页耗时第199999页耗时效率排行1用not in0ms16ms47ms475ms953ms32用select max5ms16ms35ms325ms623ms13中间变量_number0ms0ms34ms365ms710ms24临时表780ms796ms798ms780ms805ms4正好我正在研究这个问题 给大家分享

2. SQLSERVER如何实现分页查询

写存储过程 ..
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

ALTER PROCEDURE usp_Province_pagination

@PageSize INT, --每页的显示的行数
@AbsolutePage INT, -- 当前页的页数
@PageCount INT OUTPUT --总页数
AS
DECLARE @BeginRecord INT --记录每此从哪一行开始读取
DECLARE @RecordCount INT --表中数据的总条数
DECLARE @sql NVARCHAR(1000)
SET @RecordCount = (SELECT count(*) FROM Province)

--表中没有数据的情况
IF @RecordCount = 0
BEGIN
SET @PageCount = 0
RETURN(0)
END
-- 表中的总条数大于定义的每页的行数的情况
IF @RecordCount > @PageSize
BEGIN
-- 计算总能分多少页
SET @PageCount = (@RecordCount + @PageSize - 1)/@PageSize
--当前应该从哪一行开始读取
SET @BeginRecord = ((@AbsolutePage-1) * @PageSize)
SET @sql = N'SELECT TOP ' + cast(@PageSize AS NVARCHAR(100)) +' ProvinceID, provinceCode, ProvinceName
FROM Province
WHERE ProvinceID NOT IN
(SELECT TOP '+ CAST(@BeginRecord AS NVARCHAR(100)) + ' ProvinceID
FROM Province)'

EXECUTE sp_executesql @sql
END
ELSE -- -- 表中的总条数大于定义的每页的行数情况
BEGIN
SET @PageCount = 1
SET @SQL = 'SELECT ProvinceID, provinceCode, ProvinceName FROM Province '
EXECUTE sp_executesql @sql
END

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

3. Sql server2008怎样分页

你可以用存储过程分页,如下存储过程支持分页和排序:

CREATE PROCEDURE GetSortedMovies
(
@SortExpression NVarChar(100),
@StartRowIndex INT,
@MaximumRows INT
)
AS

-- 创建一个临时表存储查询结果
CREATE TABLE #PageIndex
(
IndexId INT IDENTITY (1,1) NOT NULL,
RecordId INT
)
-- 插入临时表
INSERT INTO #PageIndex (RecordId)
SELECT Id FROM Movies
ORDER BY
CASE WHEN @SortExpression='Id' THEN Id END ASC,
CASE WHEN @SortExpression='Id DESC' THEN Id END DESC,
CASE WHEN @SortExpression='Title' THEN Title END ASC,
CASE WHEN @SortExpression='Title DESC' THEN Title END DESC,
CASE WHEN @SortExpression='Description' THEN Description END ASC,
CASE WHEN @SortExpression='Description DESC' THen Description END DESC

-- 得到页数
SELECT Id,Title,Description FROM Movies
INNER JOIN #PageIndex WITH (nolock)
ON Movies.Id = #PageIndex.RecordId
WHERE #PageIndex.IndexId > @StartRowIndex
AND #PageIndex.IndexId < (@StartRowIndex + @MaximunRows + 1)
ORDER BY #PageIndex.IndexId

4. SQL如何实现数据分页,要具体语句,谢谢

可以的,用存储过程

分页存储过程如下

CREATE PROCEDURE GetRecordFromPage
@tblName varchar(255), -- 表名
@RetColumns varchar(1000) = '*', -- 需要返回的列,默认为全部
@Orderfld varchar(255), -- 排序字段名
@PageSize int = 10, -- 页尺寸
@PageIndex int = 1, -- 页码
@IsCount bit = 0, -- 返回记录总数, 非 0 值则返回
@OrderType varchar(50) = 'asc', -- 设置排序类型, 非 asc 值则降序
@strWhere varchar(1000) = '' -- 查询条件 (注意: 不要加 where)
AS

declare @strSQL varchar(1000) -- 主语句
declare @strTmp varchar(300) -- 临时变量
declare @strOrder varchar(400) -- 排序类型

if @IsCount != 0 --执行总数统计

begin
if @strWhere != ''
set @strSQL = "select count(*) as Total from [" + @tblName + "] where " + @strWhere
else
set @strSQL = "select count(*) as Total from [" + @tblName + "]"
end

else --执行查询操作

begin

if @OrderType != 'asc'
begin
set @strTmp = "<(select min"
set @strOrder = " order by [" + @Orderfld +"] desc"
end
else
begin
set @strTmp = ">(select max"
set @strOrder = " order by [" + @Orderfld +"] asc"
end

set @strSQL = "select top " + str(@PageSize) + " " + @RetColumns + " from ["
+ @tblName + "] where [" + @Orderfld + "]" + @strTmp + "(["
+ @Orderfld + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["
+ @Orderfld + "] from [" + @tblName + "]" + @strOrder + ") as tblTmp)"
+ @strOrder

if @strWhere != ''
set @strSQL = "select top " + str(@PageSize) + " " + @RetColumns + " from ["
+ @tblName + "] where [" + @Orderfld + "]" + @strTmp + "(["
+ @Orderfld + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["
+ @Orderfld + "] from [" + @tblName + "] where (" + @strWhere + ") "
+ @strOrder + ") as tblTmp) and (" + @strWhere + ") " + @strOrder

if @PageIndex = 1
begin
set @strTmp = ""
if @strWhere != ''
set @strTmp = " where (" + @strWhere + ")"

set @strSQL = "select top " + str(@PageSize) + " " + @RetColumns + " from ["
+ @tblName + "]" + @strTmp + " " + @strOrder
end
end

exec (@strSQL)

5. 在SQL中存储过程的一般语法是什么

1、 创建语法

createproc|procerepro_name

[{@参数数据类型}[=默认值][output],

{@参数数据类型}[=默认值][output],

....

]

as

SQL_statements

2、 创建不带参数存储过程

--创建存储过程

if(exists(select*fromsys.objectswherename='proc_get_student'))

dropprocproc_get_student

go

createprocproc_get_student

as

select*fromstudent;

--调用、执行存储过程

execproc_get_student;

3、 修改存储过程

--修改存储过程

alterprocproc_get_student

as

select*fromstudent;

4、 带参存储过程

--带参存储过程

if(object_id('proc_find_stu','P')isnotnull)

dropprocproc_find_stu

go

createprocproc_find_stu(@startIdint,@endIdint)

as

select*fromstudentwhereidbetween@startIdand@endId

go

execproc_find_stu2,4;

5、 带通配符参数存储过程

--带通配符参数存储过程

if(object_id('proc_findStudentByName','P')isnotnull)

dropprocproc_findStudentByName

go

createprocproc_findStudentByName(@namevarchar(20)='%j%',@nextNamevarchar(20)='%')

as

select*fromstudentwherenamelike@nameandnamelike@nextName;

go

execproc_findStudentByName;execproc_findStudentByName'%o%','t%';

(5)mssql分页存储过程扩展阅读:

SQL存储过程优点:

1、重复使用。存储过程可以重复使用,从而可以减少数据库开发人员的工作量。

2、减少网络流量。存储过程位于服务器上,调用的时候只需要传递存储过程的名称以及参数就可以了,因此降低了网络传输的数据量。

3、安全性。参数化的存储过程可以防止SQL注入式攻击,而且可以将Grant、Deny以及Revoke权限应用于存储过程。

热点内容
安卓手机怎么投屏到gl8 发布:2025-07-10 10:54:47 浏览:276
区县数据库 发布:2025-07-10 10:51:59 浏览:918
服务器遭到破坏我的世界 发布:2025-07-10 10:51:53 浏览:647
ftp服务器攻击 发布:2025-07-10 10:28:46 浏览:140
提高studio编译速度 发布:2025-07-10 10:28:46 浏览:415
Char在sql 发布:2025-07-10 10:19:19 浏览:783
请密码不叫什么说话 发布:2025-07-10 10:06:22 浏览:115
苹果应用怎么设置密码 发布:2025-07-10 10:04:00 浏览:838
雪国脚本 发布:2025-07-10 10:04:00 浏览:940
编程让 发布:2025-07-10 09:48:13 浏览:360