sql 游标问题 更新大批量数据 在线等 ~

xuxubaby 2011-03-22 10:54:22
如下,写了一游标代码,其中表tb_photo1中数据有5万多条,表tb_toupiao中数据有300万多条,下面游标实现的功能是更新
tb_photo1表中ptrq的值,发现效率不是一般的慢,现在sql已运行2个多小时,还没有运行完,查影响行数的时候,看数据在sql运行的情况下不断的变化,现在的问题是有没有更高效的方法来解决这样批量更新的问题呢。因表中字段ptrq的值是根据其他表中数据相减的差值得出的,所以肯定不能直接update ptrq,不知道我表达的意思各位仁兄是否明白 ,望指教 ~

declare @ptid int
declare @ptrq int
declare @i int
declare cursor1 cursor for --定义游标cursor1
select top 10 ptid,ptrq from tb_photo1 order by ptrq desc --使用游标的对象(跟据需要填入select文)
open cursor1 --打开游标

fetch next from cursor1 into @ptid,@ptrq --将游标向下移1行,获取的数据放入之前定义的变量@id,@name中

while @@fetch_status=0 --判断是否成功获取数据
begin
update tb_photo1 set ptrq=ptrq-(
select
(select count(id) from tb_toupiao where ptid=@ptid)-
(select count(*)
from(
select count(id) as id
from tb_toupiao
where ptid = @ptid
group by convert(varchar(16),addtime,120),usid) a)) where ptid=@ptid
--进行相应处理(跟据需要填入SQL文)

fetch next from cursor1 into @ptid,@ptrq --将游标向下移1行
end

close cursor1 --关闭游标
deallocate cursor1


...全文
502 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
XiangZhiLiu 2011-03-22
  • 打赏
  • 举报
回复
添加索引~~~优化sql~~
快溜 2011-03-22
  • 打赏
  • 举报
回复
update a set ptrq=ptrq-(select count(id) from tb_toupiao where ptid=a.ptid)- 
(select count(*)from( select count(id) as idfrom tb_toupiao
where ptid = a.ptid group by convert(varchar(16),addtime,120),usid)a)
from tb_photo1 a
xuxubaby 2011-03-22
  • 打赏
  • 举报
回复

declare @ptid int
declare @ptrq int
declare @i int
declare cursor1 cursor for --定义游标cursor1
select ptid,ptrq from tb_photo1 order by ptrq desc --使用游标的对象(跟据需要填入select文)
open cursor1 --打开游标

fetch next from cursor1 into @ptid,@ptrq --将游标向下移1行,获取的数据放入之前定义的变量@id,@name中

while @@fetch_status=0 --判断是否成功获取数据
begin
update tb_photo1 set ptrq=ptrq-(
select
(select count(id) from tb_toupiao where ptid=@ptid)-
(select count(*)
from(
select count(id) as id
from tb_toupiao
where ptid = @ptid
group by convert(varchar(16),addtime,120),usid) a)) where ptid=@ptid
--进行相应处理(跟据需要填入SQL文)

fetch next from cursor1 into @ptid,@ptrq --将游标向下移1行
end

close cursor1 --关闭游标
deallocate cursor1




这样子,应该是没有top10的 我复制的时候忘记去掉啦 ~
xuxubaby 2011-03-22
  • 打赏
  • 举报
回复
来人啊 没人回答了吗 ?
xuxubaby 2011-03-22
  • 打赏
  • 举报
回复
那就麻烦各位仁兄给出点sql吧~ 谢啦
快溜 2011-03-22
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 y_h_t 的回复:]
何时用游标呢,能给点解释吗,不要从网上复制过来啊 ~
[/Quote]一切不用游标能解决的问题,都不用游标。比如你这个
xuxubaby 2011-03-22
  • 打赏
  • 举报
回复
何时用游标呢,能给点解释吗,不要从网上复制过来啊 ~
快溜 2011-03-22
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 y_h_t 的回复:]
用 或者 不用 在效率上有多大的区别吗 ~ 
[/Quote]游标开启耗时耗资源
xuxubaby 2011-03-22
  • 打赏
  • 举报
回复
用 或者 不用 在效率上有多大的区别吗 ~ 
xuam 2011-03-22
  • 打赏
  • 举报
回复
不需要用到游标吧?
gw6328 2011-03-22
  • 打赏
  • 举报
回复

update tb_photo1 set ptrq=ptrq-(
select
(select count(id) from tb_toupiao where ptid=@ptid)-
(select count(*)
from(
select count(id) as id
from tb_toupiao
where ptid = @ptid
group by convert(varchar(16),addtime,120),usid) a))

这里是不是多写了。这么我么多。你游标只选了10条不可能运行两个小时吧。
快溜 2011-03-22
  • 打赏
  • 举报
回复
貌似没必要用游标
SQL77 2011-03-22
  • 打赏
  • 举报
回复
update a set ptrq=ptrq-
(select count(id)-count(distinct convert(char(16),addtime,120)+usid)
from tb_toupiao where ptid=a.ptid
from tb_photo1 a


把两表的PTID加上索引,以利于子查询查找统计,要不然速度更慢,还有需要考虑一下时间或者USID相加为空的情形,或者没有投票的情形

22,209

社区成员

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

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