很急,高人帮忙(插入数据后更新)
有一数据库 (SQL SERVER 2000)
非常频繁的插入数据
字段:序号 时间 (其它不列举)
插入的时候,需要按时间来排序,因为时间在我的系统里是个变量,说不准是个什么时间,但必须按升序来排列,并且,前面的序号要根据实际位置也要跟着变,数据量不小,我用我的办法效率实在是太低下,我想问的是有没有一个高效的办法来解决这个问题?
[解决办法]
好像思路有点问题吧...
[解决办法]
--参考以下
create table 表 (序号 int,时间 datetime)
----建个触发器
create trigger tr_updateID
on 表
for insert
as
if object_id( '表bak ') is not null
drop table 表bak
select 序号=identity(int,1,1) , 时间 into 表bak from 表 order by 时间
insert into 表 (时间)
select getdate()
select * from 表bak
/*
序号 时间
----------------------------------------------------
12007-07-27 12:01:35.497
22007-07-27 12:01:41.000
32007-07-27 12:05:22.840
42007-07-27 12:06:19.700
52007-07-27 12:06:31.200
62007-07-27 12:06:45.140
72007-07-27 12:07:07.170
82007-07-27 12:07:16.950
92007-07-27 12:07:17.937
102007-07-27 12:07:18.810
112007-07-27 12:07:19.890
122007-07-27 12:07:20.700
132007-07-27 12:08:22.340
142007-07-27 12:08:23.500
152007-07-27 12:08:26.750
*/
[解决办法]
--你这么干就可以了,在要插入的表中设序号col为自增就可用来,干嘛还要现处理?!
create table t1(col int identity,col2 datetime)
create table t2(col int,col2 datetime)
insert into t2 select 2, '2007-07-07 '
insert into t2 select 1, '2007-07-17 '
insert into t2 select 3, '2007-07-27 '
insert into T1
select col2 from t2
select * from t1
[解决办法]
插入的时候,需要按时间来排序?
有这个必要吗?插入进数据库按主键位置放就好了
你查询的时候按时间order by不就够了,或者在时间这个字段建一个升序的索引
[解决办法]
在时间 上建立聚集索引
create clustered index IN_时间 on table1 (时间)
序非主
insert table1 values (1,@date)
在table1 上建trigger
create trigger tr_test on table1 after insert
as
update table1 set 序 = (select top 1 a.序 from table1 a where a.时间 > inserted.时间 ) where table1.key = inserted.key
update table1 set 序 = 序 + 1 where 时间 > inserted.
如果序号主
建另外 加一字段 num 行位置
也用上面的trigger 更新num字段即可
[解决办法]
a是表名,inserted是你刚刚插入的纪录