Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Mailing Lists -> Oracle-L -> faster way of doing this?
Hi,
The code needs to work on oracle 8i and 9i. The current code occasionally gave "Ora-01555: Snapshot too old" error (becuase there is a commit within the loop for each 1000 rows, which I took out in my test code below). Assuming we can not change the RBS size, and I found Tom suggested to do the bulk delete
http://asktom.oracle.com/pls/ask/f?p=4950:8:17490901823893610313::NO::F4950_ P8_DISPLAYID,F4950_P8_CRITERIA:1046580359875 However my test showed the new method ran slower. Is there a better/faster way of writing the same delete sql? The total number of "to-be-deleted" rows could be huge in the table, so I need to use rownum<=1000. TIA.
Guang
set timing on
set serveroutput on
declare
counter number;
cursor DelAudit_cur1 is
SELECT AuditTrail.RID FROM AuditTrail, Customers, Banks, auditedUsers WHERE AuditTrail.UserRID = AuditedUsers.userRID AND auditedUsers.Customerrid = Customers.RID AND Customers.Bank = Banks.RID AND TRUNC(AuditTrail.DateTime) < (TRUNC(SYSDATE) -NVL(Customers.AUDITRETENTIONPERIOD, Banks.AUDITRETENTIONPERIOD)); begin
counter := 0;
FOR Cur_DelAudit IN DelAudit_cur1 LOOP
DELETE FROM AuditTrail a WHERE a.rid = Cur_DelAudit.RID; counter:=counter+1;
counter=12154
PL/SQL procedure successfully completed.
Elapsed: 00:00:07.09
SQL> rollback;
Rollback complete.
declare
counter number;
begin
counter := 0;
loop
delete from AuditTrail where RID in (SELECT AuditTrail.RID FROM AuditTrail, Customers, Banks, auditedUsers WHERE AuditTrail.UserRID = AuditedUsers.userRID AND auditedUsers.Customerrid = Customers.RID AND Customers.Bank = Banks.RID AND TRUNC(AuditTrail.DateTime) < (TRUNC(SYSDATE) - NVL(Customers.AUDITRETENTIONPERIOD, Banks.AUDITRETENTIONPERIOD))) and rownum <= 1000; counter := counter + sql%rowcount; exit when sql%rowcount = 0;
counter=12154
PL/SQL procedure successfully completed.
Elapsed: 00:00:16.01
SQL> rollback;
Rollback complete.
declare
counter number;
begin
counter := 0;
loop
delete from AuditTrail A where UserRID in (SELECT AuditedUsers.userRID FROM Customers, Banks, auditedUsers WHERE auditedUsers.Customerrid = Customers.RID AND Customers.Bank = Banks.RID) AND (TRUNC(SYSDATE) - TRUNC(A.DateTime)) > (select NVL(Customers.AUDITRETENTIONPERIOD, Banks.AUDITRETENTIONPERIOD) from Customers, Banks, auditedUsers WHERE auditedUsers.Customerrid = Customers.RID AND Customers.Bank = Banks.RID AND AuditedUsers.userRID = A.UserRID) and rownum <= 1000; counter := counter + sql%rowcount; exit when sql%rowcount = 0;
counter=12154
PL/SQL procedure successfully completed.
Elapsed: 00:00:22.03
SQL> rollback;
Rollback complete.
-- http://www.freelists.org/webpage/oracle-lReceived on Wed Mar 23 2005 - 15:41:06 CST
![]() |
![]() |