如何高效使用存储过程按需要读取数据?请各位高手讲解,小弟诚谢!在线等。。

cnlamar 2003-09-15 01:22:27
大家好,我是.net版那边的,最近做了一个asp.net的东西,由于采用了数据分页,所以希望每次读取的数据刚好是分页的就够了,全部读取太过浪费!
于是写了一存储过程,由于本人对此并不了解,还请各位高手帮忙看看,斧正一下!

我写的存储过程是按照要读取的数据分页的索引和每页的大小提交过来,然后将符合我的条件的数据全部放到一个临时表里,然后在临时表里用游标来获取我要读取的数据段,最后返回,但我发现,这样我不是每次都要把所有符合条件的数据读取,然后建临时表?那样的话,只是减少了数据输出的量,但数据库的运算没有得到降低,于是每次对程序做重力测试的时候,SQLSERVER都占用了很多的资源……CPU很BT的达到了60%多,汗,我该怎么做呢?
...全文
26 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
cnlamar 2003-09-15
  • 打赏
  • 举报
回复
比如说,我要取出按最后更新时间排列的第30-40条,该怎么做?
最后更新时间的字段是UpdataTime而我的唯一字段是ThreadID
cnlamar 2003-09-15
  • 打赏
  • 举报
回复
PS:我排序的字段和唯一的ID是不一样的
cnlamar 2003-09-15
  • 打赏
  • 举报
回复
那我还得排序,这样的话,是不是就是说要建临时表?
CrazyFor 2003-09-15
  • 打赏
  • 举报
回复
select top M-N * from yourTable where id not in(select top N-1 id from table)


SQL就可以这样写,
比如:
select top 10 from tb where id not in (select top 30 id from tb)
就是取31-40条记录.


cnlamar 2003-09-15
  • 打赏
  • 举报
回复
小弟工夫太牵,几乎是看不懂。。。大哥,解释一下。。。
cnlamar 2003-09-15
  • 打赏
  • 举报
回复
select top M-N * from yourTable where id not in(select top N-1 id from table)


这个是一个SQL语法?还是一个意思?

能否写个例子?感谢~~
CrazyFor 2003-09-15
  • 打赏
  • 举报
回复


查询N-M条记录。
select IDENTITY(int,1,1) as iid,* into #temptable from yourtable
select top M-N * from #temptable where iid>=N

OR:

select top M-N * from yourTable where id not in(select top N-1 id from table)
ID为表具有唯一值的任何字段
cnlamar 2003-09-15
  • 打赏
  • 举报
回复
哥们,帮我顶顶,急疯了我。。。
liuyun2003 2003-09-15
  • 打赏
  • 举报
回复
学习
cnlamar 2003-09-15
  • 打赏
  • 举报
回复
贴上此存储过程

CREATE procedure ReadThreadData
(@TableName VarChar(10),
@OraderKey VarChar(20),
@FatherID VarChar(20),
@ReadAll bit,
@pagesize int,
@pageindex int,
@docount bit)
as
set nocount on
if(@docount=1)

exec('select count(ThreadID) from '+@TableName+' where fatherid='+@FatherID)
else
begin

create table #indextable (id int identity(1,1),nid int)
declare @PageLowerBound int
declare @PageUpperBound int
set @PageLowerBound=(@pageindex-1)*@pagesize
set @PageUpperBound=@PageLowerBound+@pagesize
set rowcount @PageUpperBound

exec('insert into #indextable(nid) select ThreadID from '+@TableName+' where fatherid='+@FatherID+' order by UpTop DESC,'+@OraderKey)

declare @a varchar(300)

if(@ReadAll=1)
set @a='select O.* from '+@TableName+' O,#indextable t where O.ThreadID=t.nid and t.id>'+cast(@PageLowerBound as varchar(10))+' and t.id<='+cast(@PageUpperBound as varchar(10))+' order by t.id'
else
set @a='select O.ThreadID,O.Title,O.Icon,O.State,O.CreateUser,O.UpdataUser,O.UpdataTime,O.ViewNum,O.ReplyNum,O.UpTop,O.IsBest,O.TitlePic,O.Buffer01,O.Buffer02,O.Buffer03,O.Buffer04,O.Buffer05 from '+@TableName+' O,#indextable t where O.ThreadID=t.nid and t.id>'+cast(@PageLowerBound as varchar(10))+' and t.id<='+cast(@PageUpperBound as varchar(10))+' order by t.id'

exec(@a)

end
set nocount off
GO

22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧