我的表一直在用,不能停。表中数据库有三千多万条记录,现在每天以三万条左右的记录增加. 我想把表中2011年以前的记录都清空。 我处理的办法是 : 1.先备份表 .
2.delete from 表名 where 时间 < 2011-01-01 但是表中记录太多,我觉得这方法怕破坏表结构.
请问还有其它好办法清理表数据吗?
2.delete from 表名 where 时间 < 2011-01-01 但是表中记录太多,我觉得这方法怕破坏表结构.
请问还有其它好办法清理表数据吗?
delete from 表明 where SUBSTRING('2011-01-01',0,5)<2011
truncate 表名 --------表一直在用. 不能动表。。
1 建一张同样结构的表。
2 修改程序,把新数据插入到这张新表里
3 delete原表数据。
4 把新数据插入回原表。
delete操作时,建议一次少删除一些,比如只删除1000条,或只删除一天的数据。不然插入操作容易长时间被锁而失败。
我也有个表,要定时手工delete操作,我是这样做的。
select * into #t from 表名 where 时间>='2011-01-01'delete from 表名insert into 表名 select * from #t如楼上建议 你可以分次删除
--> 测试数据:[表1]
if object_id('[表1]') is not null
drop table [表1]
create table [表1](
[字段1] varchar(1),
[字段2] varchar(1)
)
go
insert [表1]
select 'a','a' union all
select 'b','a' union all
select 'c','a' union all
select 'd','b' union all
select 'e','a' union all
select 'f','c' union all
select 'g','c'
go
select * from [表1]
/*
字段1 字段2
a a
b a
c a
d b
e a
f c
g c
*/
--truncate用于清空表的记录
truncate table [表1]select * from [表1]
--表结构存在,记录全部清空
当掉了,先统计下2011-1-1之前有多少数据,再分批删除,一次删除5w行,分批进行,如果删除的数据占用了表
50%的数据,建议你还是先把不删除的数据转移到一个表里面,然后 truncate表,再把数据倒过来,delete操作
会给表留下很多的碎片。删完建议收缩
go;
delete from 表名 where 时间 = 2010-01-02;
go;
... ...
一天一天的删