Feb 7, 2011

Delete Duplicate rows and perftunning while doing delete for huge data


Here's how you remove the duplicate rows before the primary key or unique indexes can be created:

DELETE FROM our_table
WHERE rowid not in
(SELECT MIN(rowid)
FROM our_table
GROUP BY column1, column2, column3... ;
Here column1, column2, column3 constitute the identifying key for each record.
Be sure to replace our_table with the table name from which you want to remove the duplicate rows. The GROUP BY is used on the columns that make the primary key for the table. This script deletes each row in the group after the first row.

OR

insert into EMP values ('1','20');
insert into EMP values ('2','30');
insert into EMP values ('2','30');
insert into EMP values ('3','40');
insert into EMP values ('4','50');
commit;

delete from EMP t1
where t1.rowid >
             ( select min(t2.rowID) from EMP t2
               where t1.col_A = t2.col_A
               and t1.col_B = t2.col_b)


OR

delete cea
where rowid in (
  select rid
  from (
    SELECT ROWID as rid,
           row_number() over (partition by academic_period, load_week, sub_academic_period, person_uid, course_number, course_reference_number) rn
    FROM cea
  )
  where rn > 1
)

OR

delete from emp
where rowid not in
(select max(rowid) from emp group by empno);

If you want to delete huge data then we need to use below syntax for better performance


ALTER SESSION ENABLE PARALLEL DML;

DELETE /*+ PARALLEL(12) */
FROM TABLE_NAME
WHERE COLUMN_NAME='CONDITION'

COMMIT;

ALTER SESSION DISABLE PARALLEL DML;

No comments:

Post a Comment