你有更好的办法吗? 讨论一下!

simonhehe 2008-07-11 02:20:37
declare @t table(id int,length int, AccValue int)
insert @t(id,length)
select 1,10
union all select 2,30
union all select 3,40
union all select 4,70
union all select 5,90

--1 初次操作得到如下结果(当前记录的AccValue=自身length + 前一记录的AccValue)
id length AccValue
----------- ----------- -----------
1 10 10
2 30 40
3 40 80
4 70 150
5 90 240
--2 当增加新记录时AccValue如果计算最合理有效
--3 当改动某条记录的length 后,受到影响的记录的AccValue如何计算最合理有效
...全文
205 23 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
zzuhui 2008-07-11
  • 打赏
  • 举报
回复
看不懂!
liangpei2008 2008-07-11
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 wgzaaa 的回复:]
declare @t table(id int,length int, AccValue int)
insert @t(id,length)
select 1,10
union all select 2,30
union all select 3,40
union all select 4,70
union all select 5,90
declare @c int,@c2 int
select @c=0
update @t set @c=@c+length,AccValue=@c
select * from @t
------------------
1 10 10
2 30 40
3 40 80
4 70 150
5 90 240
我认为这样更好
[/Quote]
这种做法的确不错
simonhehe 2008-07-11
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 liangpei2008 的回复:]
如果记录是用户输入,而用户每次都要看一下最新的更新结果,大家也要用什么相关子查询,或游标,或用TSQL模拟游标来做??
如果数据在上10万条呢?

SQL code
所以该问题的核心在于如何不重复累计已统计的信息。
下面是个人观点:
为已参与统计的数据加上处理标识。如果在10000中加上一条记录,那直接就以最后的值+新增记录。
如果在已有的记录中做修改,则直接在累计字段+增量。
如果不这样处理,再好的SQL语句,再…
[/Quote]

新增或修改记录时使用增量进行批量更新,我也是这么干

对问题1有什么好的解决办法

希望数据量在 <1W <10W <100W >100W 这几个区间时处理效率没有太明显差别
simonhehe 2008-07-11
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 wgzaaa 的回复:]
declare @t table(id int,length int, AccValue int)
insert @t(id,length)
select 1,10
union all select 2,30
union all select 3,40
union all select 4,70
union all select 5,90
declare @c int,@c2 int
select @c=0
update @t set @c=@c+length,AccValue=@c
select * from @t
------------------
1 10 10
2 30 40
3 40 80
4 70 150
5 90 240
我认为这样更好
[/Quote]

模拟游标比子查询或循环要好 :D
simonhehe 2008-07-11
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 chuifengde 的回复:]
SQL codecreate function getts(@id int)
returns bigint
as
begin
declare @ss bigint
select @ss=isnull(sum(length),0) from tsst where id<=@id
return @ss
end
……
[/Quote]

使用这样的子查询在大数据量的情况下,只会越来越糟
simonhehe 2008-07-11
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 hery2002 的回复:]
你想讨论什么?
一种是循环方式,
一种是子查询方式,
[/Quote]

^_^

针对这样的业务要求,在大数据量的情况下,用什么样的方法实现效率更高?
可以写下自己的实现方法,并说明优缺点
liangpei2008 2008-07-11
  • 打赏
  • 举报
回复
如果记录是用户输入,而用户每次都要看一下最新的更新结果,大家也要用什么相关子查询,或游标,或用TSQL模拟游标来做??
如果数据在上10万条呢?

所以该问题的核心在于如何不重复累计已统计的信息。
下面是个人观点:
为已参与统计的数据加上处理标识。如果在10000中加上一条记录,那直接就以最后的值+新增记录。
如果在已有的记录中做修改,则直接在累计字段+增量。
如果不这样处理,再好的SQL语句,再优秀的表设计也枉然.

lff642 2008-07-11
  • 打赏
  • 举报
回复
这个问题有趣.
wgzaaa 2008-07-11
  • 打赏
  • 举报
回复
对更新,我认为没有什么好说的,不过不使用触发器,要求前台直接使用触发器中的语句在效率方面有优势,但不便于数据管理
wwd252 2008-07-11
  • 打赏
  • 举报
回复
楼上的不错
wgzaaa 2008-07-11
  • 打赏
  • 举报
回复
第二种方法好点,但也和第一种方法一样重复扫描或反复执行
wgzaaa 2008-07-11
  • 打赏
  • 举报
回复
declare @t table(id int,length int, AccValue int)
insert @t(id,length)
select 1,10
union all select 2,30
union all select 3,40
union all select 4,70
union all select 5,90
declare @c int,@c2 int
select @c=0
update @t set @c=@c+length,AccValue=@c
select * from @t
------------------
1 10 10
2 30 40
3 40 80
4 70 150
5 90 240
我认为这样更好
arrow_gx 2008-07-11
  • 打赏
  • 举报
回复
declare @t table(id int IDENTITY(1,1),length int, AccValue int)
insert @t(id,length)
select 1,10
union all select 2,30
union all select 3,40
union all select 4,70
union all select 5,90
1,更新方法
......
2 当增加新记录时AccValue如果计算最合理有效 ,
先得到最后一个 AccValue ,然后插入数据
  declare @last_AccValue int
set @last_AccValue=(select top 1 AccValue from @t order by id desc)
insert into @t(length,AccValue) values(@length,@length+@last_AccValue)


3、当改动某条记录的length 后,受到影响的记录的AccValue如何计算最合理有效
个人认为,这个时候,只有先更新一条记录,然后再用方法一,对ID>=更新ID 的记录全部进行重新计算

chuifengde 2008-07-11
  • 打赏
  • 举报
回复
create  function getts(@id int)
returns bigint
as
begin
declare @ss bigint
select @ss=isnull(sum(length),0) from tsst where id<=@id
return @ss
end
go
create table tsst(id int,length int, AccValue as dbo.getts(id))
insert tsst(id,length)
select 1,10
union all select 2,30
union all select 3,40
union all select 4,70
union all select 5,90
chuifengde 2008-07-11
  • 打赏
  • 举报
回复
用计算列最方便了
hery2002 2008-07-11
  • 打赏
  • 举报
回复
你想讨论什么?
一种是循环方式,
一种是子查询方式,
viptiger 2008-07-11
  • 打赏
  • 举报
回复
=.=
hery2002 2008-07-11
  • 打赏
  • 举报
回复
declare @t table(id int,length int, AccValue int) 
insert @t(id,length)
select 1,10
union all select 2,30
union all select 3,40
union all select 4,70
union all select 5,90

select id,
length,
(select sum(length)
from @t b
where b.id <= a.id) AccValue
from @t a
/*
id length AccValue
----------- ----------- -----------
1 10 10
2 30 40
3 40 80
4 70 150
5 90 240
*/
utpcb 2008-07-11
  • 打赏
  • 举报
回复
declare @t table(id int,length int, AccValue int)
insert @t(id,length)
select 1,10
union all select 2,30
union all select 3,40
union all select 4,70
union all select 5,90

select id,
length,
(select sum(length)
from @t b
where b.id <= a.id) AccValue
from @t a


1 10 10
2 30 40
3 40 80
4 70 150
5 90 240
simonhehe 2008-07-11
  • 打赏
  • 举报
回复

不希望只是简单实现,要说出具体的优劣
加载更多回复(3)

22,302

社区成员

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

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