去重复时
1)要考虑数据的量和重复的量???
2)确认删除方式怎么删除???
3)删除的比例及记录数???
4)删除之后要验证是否还有重复的!!!
5)怎么复制表
少量 :delete
方法1)通过重复数据的rowid去除重复数据
delete test where rowid not in(select max(rowid) from test t group by t.object_id);
方法2)分析函数去除数据
delete test
where rowid in (select rowid
from (select row_number() over(partition by t.object_id order by rowid) rn,
t.*
from test t)
where rn > 1);
方法3)通过唯一主键及对应的rowid去除
delete test t
where exists (select null
from test t2
where t.object_id = t2.object_id
and t.rowid > t2.rowid);
方法4)
exists 与in 进行改写
delete test t
where t.object_id in (select t2.object_id
from test t2
where t.object_id = t2.object_id
and t.rowid > t2.rowid);
大量 :重建表
create table xx select distinct * from test;
alter table test rename to test_bkp;
alter table xx rename to test;
注:当要删除的数据远远大于保留的数据时用此法,重命名的时间段这张表不可以用服务终止系统不可用,生产环境不建议用
怎么复制表:
1.复制表结构和数据
create table 新表名
as
select * from 旧表名;
2.只复制表结构
create table 新表名
as
select * from 旧表名 where 1<>1;
3.复制其他数据库中的表结构和数据,建立一个dblink.
create table 新表名
as
select * from 旧表名@dblink的名称;
4.复制数据到已经建立的表
insert into 新表名
select * from 旧表名;