关于SQL效率的问题?

kile 2004-11-24 09:21:32
现在我有这么一个情况

有表 SUMTAB 一个,里面保存13万条数据汇总

我现在要做的操作就是把sumtab表中的记录分派到4个表中去t1,t2,t3,t4

就是说,假设sumtab表中有4个字段a,b,c,d,我要把a插入到t1,b插入到t2,c插入到t3,d插入到t4中

t1,t2,t3,t4之间的关系是这样的
假如我把a插入t1,那么t1的出发器将自动生成t2,t3的空列,
接下来的就是把b和c更新到t2和t3
d的数据还是要插入到t4中去的,t2,t3,t3中都有字段id与t1中的sid关联,就是说t1与t2t3t3中的每个表都是一对多的关系

问题出现在处理sumtab上了
我是有游标处理这个事情的,但是很可怜,处理到3万条数据后,速度超慢!无敌慢,
我想是效率有问题

就是把13万条数据打开,然后从头游到尾。。

哪位大侠有办法解决这个问题,小弟不胜感激。
...全文
103 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
chop_chop 2004-11-25
  • 打赏
  • 举报
回复
盡量少用corsor,
特別是數量大時,
zjcxc 2004-11-24
  • 打赏
  • 举报
回复
看得不明白,最好能举数据说明一下.
kile 2004-11-24
  • 打赏
  • 举报
回复
不是的,我好象没说明白,,,,,t2,t3必须是update t2 where id=....
这样的才行
kile 2004-11-24
  • 打赏
  • 举报
回复
t2,t3,t4之间没有本质上的联系,但是他们都有一个字段id是相同的,而且对应t1的sid
zengzhengliang 2004-11-24
  • 打赏
  • 举报
回复
同意楼上的说法
游标太慢了哈。。
Andy__Huang 2004-11-24
  • 打赏
  • 举报
回复
t1,t2,t3,t4之間的關系你說明白一點好嗎﹖上面你好象寫錯了吧﹖
yjs_lh 2004-11-24
  • 打赏
  • 举报
回复
如果要用存储过程,这样写:
create procedure p_test
as
begin
-- 上面的代码...
end
go

即可。其中的go可能需要处理一下
yjs_lh 2004-11-24
  • 打赏
  • 举报
回复
--1、为了创建一个sid,可以建立t1的表结构使之有个自增长的列。并创建相应的t2,t3,t4
create table t1(
sid int not null identity primary key,
a varchar(40) null
)
go

create table t2(
id int not null
b varchar(40) null
)
go

create table t3(
id int not null
c varchar(40) null
)
go

create table t4(
id int not null
d varchar(40) null
)
go


--2、求t1的数据
insert into t1 (a)
select distinct a from sumtab where a is not null
go
--3、求t2的数据
insert into t2
select distinct sid, b from subtab inner join t1 on subtab.a = t1.a
go
--4、同3的方法,求t3和t4的数据
insert into t3
select distinct sid, c from subtab inner join t1 on subtab.a = t1.a
go
insert into t4
select distinct sid, d from subtab inner join t1 on subtab.a = t1.a
go
kile 2004-11-24
  • 打赏
  • 举报
回复
是存储过程,具体怎么做?
Andy__Huang 2004-11-24
  • 打赏
  • 举报
回复
用存儲過程處理。

不要用到游標﹐用游標速度會非常的慢﹐你應該用臨時表﹐也可以不用臨時 表也可以解決

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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