一次去重 80w 数据时夯死临时处理实例

数据库 Oracle
近日,博主在对一张百万数据的业务表进行去重时,去重操作竟然夯住了。下面就让我们跟随博主来简单回忆一下。

近日,在对一张百万数据的业务表进行去重时,去重操作竟然夯住了。下面就来简单回忆一下。

1、查询业务表数据量,查看到总共有200多w条

SQL> select count(*) from tb_bj_banker_etl;

2552381

2、查询表内应该去掉的重复数据量,共80多w条

SQL> select count(*) from tb_bj_banker_etl where (id) in (select id from tb_bj_banker_etl group by id having count(*)>1) and rowid not in(select max(rowid) from tb_bj_banker_etl group by id having count(*)>1);

830099

3、于是,在晚上下班前,执行了下面的语句脚本,为了去重

SQL> delete from tb_bj_banker_etl where(id) in (select id from tb_bj_banker_etl group by id having count(*)>1) and rowid not in(select max(rowid) from tb_bj_banker_etl group by id having count(*)>1);

SQL> commit;

4、第二天,到达现场时,发现PL/SQL Developer工具中昨天晚上执行的语句仍在执行中

首先察觉,80多w的去重数据跑了一个晚上也没跑完?这肯定是哪里出了问题?

怀疑有锁表。

于是查询是否有锁表的用户。

 

  1. SELECT 
  2.   A.OWNER,                        --OBJECT所属用户 
  3.   A.OBJECT_NAME,                  --OBJECT名称 
  4.   B.XIDUSN, 
  5.   B.XIDSLOT, 
  6.   B.XIDSQN, 
  7.   B.SESSION_ID,                   --锁表用户的session 
  8.   B.ORACLE_USERNAME,              --锁表用户的Oracle用户名 
  9.   B.OS_USER_NAME,                 --锁表用户的操作系统登陆用户名 
  10.   B.PROCESS, 
  11.   B.LOCKED_MODE,  
  12.   C.MACHINE,                      --锁表用户的计算机名称 
  13.   C.STATUS,                       --锁表状态 
  14.   C.SERVER, 
  15.   C.SID, 
  16.   C.SERIAL#, 
  17.   C.PROGRAM                       --锁表用户所用的数据库管理工具 
  18. FROM 
  19.   ALL_OBJECTS A, 
  20.   V$LOCKED_OBJECT B, 
  21.   SYS.GV_$SESSION C  
  22. WHERE 
  23.   A.OBJECT_ID = B.OBJECT_ID 
  24.   AND B.PROCESS = C.PROCESS 
  25. ORDER BY 1,2 

 

在下面结果中可以看到,锁表的只是去重语句的发起会话,并没有其它用户造成锁表,这说明语句仍然在执行嘛?带着疑问,开始尝试解决。

1 BJHYL tb_bj_banker_ETL 15 18 9000 913 BJHYL Administrator 4036:972 3 WORKGROUP\BACKDB ACTIVE DEDICATED 913 3381 plsqldev.exe

2 BJHYL tb_bj_banker_ETL 15 18 9000 913 BJHYL Administrator 4036:972 3 WORKGROUP\BACKDB INACTIVE DEDICATED 649 41791 plsqldev.exe

3 BJHYL tb_bj_banker_ETL 15 18 9000 913 BJHYL Administrator 4036:972 3 WORKGROUP\BACKDB INACTIVE DEDICATED 817 27777 plsqldev.exe

4 BJHYL tb_bj_banker_ETL 15 18 9000 913 BJHYL Administrator 4036:972 3 WORKGROUP\BACKDB INACTIVE DEDICATED 841 1981 plsqldev.exe

5、采用分批次,解决去重夯住问题

由于直接去重无法顺利进行,于是想到了分批次去重的方法,试一下。

 

  1. ***次: 
  2. delete from tb_bj_banker_etl where(id) in (select id from tb_bj_banker_etl  group by id  having count(*)>1) and rowid not in(select max(rowid) from tb_bj_banker_etl group by id having count(*)>1) and rownum<=100000; 
  3. commit
  4.  
  5. 第二次: 
  6. delete from tb_bj_banker_etl where(id) in (select id from tb_bj_banker_etl  group by id  having count(*)>1) and rowid not in(select max(rowid) from tb_bj_banker_etl group by id having count(*)>1) and rownum<=100000; 
  7. commit
  8.  
  9. 。。。。。。。 
  10. 。。。。。。。 
  11. 。。。。。。。 
  12.  
  13. 第八次: 
  14. delete from tb_bj_banker_etl where(id) in (select id from tb_bj_banker_etl  group by id  having count(*)>1) and rowid not in(select max(rowid) from tb_bj_banker_etl group by id having count(*)>1); 
  15. commit

 

结果:通过将80多万数据划分成以10w数据为单次进行去重操作,总共用时140多秒,完成了去重80万数据的目的。但为何直接处理出现夯死情况,有待后续跟踪分析。

博文出处:http://blog.csdn.net/huangyanlong/article/details/46041735
 

责任编辑:Ophira 来源: 个人博客
相关推荐

2011-08-18 17:36:04

爱普生喷墨打印机

2020-11-16 07:19:17

线上函数性能

2012-06-06 14:56:29

爱普生喷墨打印机

2018-09-26 17:12:16

Chrome浏览器cookie

2020-08-19 11:02:39

系统ssh登录

2009-05-22 17:05:52

2023-11-06 07:45:42

单据图片处理

2021-03-18 23:47:18

MySQLselect索引

2020-10-27 10:35:38

优化代码项目

2021-02-01 09:00:34

Ceph octopu集群运维

2023-09-07 13:32:00

MySQL数据库

2011-06-28 10:41:50

DBA

2022-09-03 18:29:49

开发技术

2010-09-07 15:31:20

SQL语句事务

2020-10-24 13:50:59

Python编程语言

2021-12-27 10:08:16

Python编程语言

2009-11-06 10:49:53

2022-01-10 09:31:17

Jetty异步处理seriesbaid

2022-07-27 10:30:49

后端前端

2018-07-18 15:37:24

数据库DB2故障处理
点赞
收藏

51CTO技术栈公众号