想要做一个数据库表清理的测试,清理A表里面不在B表且不C表的数据。这三个表有一个相同的字段ID做主键。我写了个存储过程,大概思路是先把要删除的数据查询出来放在游标里,然后打开游标循环delete A where A.ID =游标变量.ID,每5000条提交一次。这三个表的数据都在3亿条左右。想问大家有没有别的思路?或者用什么方式查询能最大效率执行查询这一步?
还是要看b和c表的数据量,如果只是a表量大,b和c较小的话,可用把切割a表进行处理 通过不同条件查询,生成建临时表,再汇聚 例如 select * from a where not exists (select 1 from b where a.id=b.id) and not exists (select 1 from c where a.id=c.id) and mod(a.id,10)=0;
通过不同条件查询,生成建临时表,再汇聚
例如
select * from a where not exists (select 1 from b where a.id=b.id) and not exists (select 1 from c where a.id=c.id) and mod(a.id,10)=0;
如果保留的数据量少,建议将结果数据写入一张新表再迁移回来。原则上不建议对大量数据使用游标,效率会比sql的方式低很多。