更新语句继续提问。别嫌我烦。答对者每人可得100分。重复不算。(可用分快没了)

ShapeRock 2003-03-24 11:25:47
表theA
id the1 th2 (注意id可以重复)
1 4 10
1 6 15
2 3 5
3 1 3
表theB
id sum1 sum2 (sum1为表theA的id<=theB.id的所有the1的累加)
1 0 0 (sum2为表theA的id<=theB.id的所有the2的累加)
2 0 0
3 0 0
想能过一条Update语句
得到
id sumNum sumMemory
1 10 25
2 13 30
3 14 33
我知道
Update theB set sum1=(select sum(the1) from theA where theA.id<=theB.id),sum2=(select sum(the2) from theA where theA.id=theB.id)
这样对每一个theB表更新字段都要对theA 表进行一次汇总查询。请问,有没有效率更高的方法?别吐我。

...全文
87 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
ShapeRock 2003-03-25
  • 打赏
  • 举报
回复
declare @a bigint,@b bigint
set @a=0
set @b=0
Update theB set sum1=cc.aa,sum2=cc.bb from (select id,sum(the1) aa,sum(the2) bb from theA group by id)cc where cc.id=theB.id
Update theB set @a=@a+sum1,sum1=@a,@b=@b+sum2,sum2=@b
当theB表的主键是id或有一个索引是id的升序时可以实现。
这里的关键问题是怎么能控制Update的更新顺序。
ShapeRock 2003-03-25
  • 打赏
  • 举报
回复
theB的id是主键。此问题有个巧妙的方法。是小黑给我解答的问题包括这个方法。用时两秒,我再做最后的鉴定。提问此问题也是为了集思广议。看看有没有别的思路或能确定此方法。如果准确无误,我就公布此方法。
流星尔 2003-03-25
  • 打赏
  • 举报
回复
Update theB set sum1=(select sum(the1) from theA where theA.id<=theB.id),sum2=(select sum(the2) from theA where theA.id<=theB.id) 已经最快了,没办法了。
Rewiah 2003-03-25
  • 打赏
  • 举报
回复
有没有索引?
10000条记录太少,加多两个0试试。
语句还是:
Update theB set sum1=(select sum(the1) from theA where theA.id<=theB.id),sum2=(select sum(the2) from theA where theA.id=theB.id)

happydreamer 2003-03-25
  • 打赏
  • 举报
回复
哈哈 是这个啊 我来试试看
CrazyFor 2003-03-24
  • 打赏
  • 举报
回复
update theB set sum1=bbb.sum1,sum2=bbb.sum2 from TheB left join
(select id,sum(the1) as sum1,sum(the2) as sum2 from (select b.id,a.the1,a.the2 from theb b left join thea a on b.id>=a.id)aa
group by id)bbb on tehB.id=bbb.id

给theA,theB表的ID字段加索引。
ShapeRock 2003-03-24
  • 打赏
  • 举报
回复
蚂蚁大哥,我要的是更新语句,不是查找语句。而且我那条语句在我机器上要56秒,而你那条查询要3分多。
pengdali 2003-03-24
  • 打赏
  • 举报
回复
从update语句上我想不出了!看看有没有其他解最优了!
CrazyFor 2003-03-24
  • 打赏
  • 举报
回复

select id,sum(the1),sum(the2) from (select b.id,a.the1,a.the2 from theb b left join thea a on b.id>=a.id)aa
group by id
ShapeRock 2003-03-24
  • 打赏
  • 举报
回复
两个表都带索引的。
yonghengdizhen 2003-03-24
  • 打赏
  • 举报
回复
同意楼上的GG..
除了建索引没有更有效的方法了..

除非修改数据库结构并加触发器.(那样你的语句就不需要了.)
yoki 2003-03-24
  • 打赏
  • 举报
回复
对A.ID和B.ID建索引,语句是不能再简了
ShapeRock 2003-03-24
  • 打赏
  • 举报
回复
声明,我会用10000条记录进行测试,如果能提高1秒,就给分。
pengdali 2003-03-24
  • 打赏
  • 举报
回复
花点钱升级一下硬件,可以得到良好的效果!!!
pengdali 2003-03-24
  • 打赏
  • 举报
回复
Update theB set sum1=(select sum(the1) from theA where theA.id<=theB.id),sum2=(select sum(the2) from theA where theA.id=theB.id)

是最快的写法了!子查询本身就比联结快!
happydreamer 2003-03-24
  • 打赏
  • 举报
回复
偶想到的好像就这两个方法 和蚂蚁大力的相同

update theB set sum1=bbb.sum1,sum2=bbb.sum2 from TheB left join
(select id,sum(the1) as sum1,sum(the2) as sum2 from (select b.id,a.the1,a.the2 from theb b left join thea a on b.id>=a.id)aa
group by id)bbb on tehB.id=bbb.id

or

update theb
set sum1=(select sum(the1) from thea where id<=theb.id),
sum2=(select sum(the2) from thea where id<=theb.id)

liuyunfeidu 2003-03-24
  • 打赏
  • 举报
回复
建立索引,然后编写存储过程执行这个操作。
ShapeRock 2003-03-24
  • 打赏
  • 举报
回复
sorry我重新测试
to pengdali(大力) 你的最后那句用时2分50秒。
to CrazyFor(蚂蚁) 你的最后那句用时2分43秒。
但我提问中的那句用时1分51秒。
ShapeRock 2003-03-24
  • 打赏
  • 举报
回复
to :pengdali(大力)
你那个用时2分43秒。
ShapeRock 2003-03-24
  • 打赏
  • 举报
回复
to :pengdali(大力)
try:

select id,sum(the1) the1,sum(th2) th2 into # from thea group by id
select b.id,sum(a.the1) th1 ,sum(a.th2) th2 into #1 from # a join # b on a.id<=b.id group by b.id

update theb set sum1=a.th1,sum2=a.th2 from #1 a where a.id=theb.id

select * from theb
drop table #,#1
你这个用时太长,我上了趟厕所。还没更新完。
加载更多回复(9)

22,206

社区成员

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

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