If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Conditional Delete with commit issue

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-10-12, 09:18
mdx34 mdx34 is offline
Registered User
 
Join Date: Oct 2004
Posts: 268
Conditional Delete with commit issue

DB2 V9.5 WSE FP8 - RHEL6.2

I have researched but could not find a delete statement that looks like mine to create a delete process with commit every N rows.

I have ran the below statement but it did not delete any records.

Here is the original statement that deletes the rows but blows up the logs even I set the logs to max (250). I also increased the log size to 4096. It deletes about 7 million rows and leaves the table with about 150,000 rows

db2 "DELETE FROM schema1.TABLE1 where (id NOT in(select id from schema1.TABLE2 ))"

Here is the one that does not delete anything and gives a message that ".....result of a query is an empty table."

db2 "DELETE FROM schema1.TABLE1 where (id NOT in(select id from schema1.TABLE2 FETCH FIRST 250000 ROWS ONLY))" ;

commit;

What is the best way to delete the table and how can I put this into a while statament (or any other method but not a stored procedure as this is part of many other deletes) ?

Thanks.
Reply With Quote
  #2 (permalink)  
Old 07-10-12, 11:17
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,649
You can use a unix shell script to execute your "first 250000 rows only" delete in an infinite loop. The exit criteria from the loop will be a unix return code of 1. When you exit the loop, recheck the data in the table to confirm the records are all deleted ..

select 1 from schema1.table1 where exists (select * from schema1.table1 where id not in (select id from schema1.table2)

or similar...
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #3 (permalink)  
Old 07-10-12, 11:33
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,766
I have two ideas now.

(1) EXPORT ---> TRUNCATE ---> INPORT
This way might be better, because the number of rows to be kept looks relatively much smaller(10% or less) than the number of rows in the table.
(1-1) EXPORT rows to be kept.
(1-2) Tuncate table. For example Truncate Table or TRUNCATE statement on DB2 9.7.
(1-3) INPORT the rows kept in (1-1).

(2) delete with commit every N rows.
See my posts(#8 - #11) in this thread
developerWorks : Information Management : IBM DB2 for Linux, Unix, and Windows Forum : Delete Script 250 rows at time ...

Last edited by tonkuma; 07-10-12 at 22:40. Reason: Added "much" ... "(10% or less)"
Reply With Quote
  #4 (permalink)  
Old 07-10-12, 11:41
mdx34 mdx34 is offline
Registered User
 
Join Date: Oct 2004
Posts: 268
Thank you for your replies. Please ignore this post. I found out that the logs are blown up before it comes to this statement. Previous statements are also conditional delete statements with no commit in between and some of the delete statements like "Delete from schema.table" are the casue of the log problem instead of using truncate.

These are multiple scripts given to me by our developers to clean up the data to initialize the database. They have a very small development databases where this is not an issue but when I run it in client test/production it becomes a problem because the volume of the data.

Thanks again
Reply With Quote
  #5 (permalink)  
Old 07-10-12, 16:05
papadi papadi is offline
Registered User
 
Join Date: Oct 2009
Location: 221B Baker St.
Posts: 487
Before promoting the offering to "client testing", some full-volume testing would be advisable. . .
Reply With Quote
  #6 (permalink)  
Old 07-10-12, 22:11
fengsun2 fengsun2 is offline
Registered User
 
Join Date: Nov 2011
Posts: 279
Quote:
Originally Posted by tonkuma View Post
I have two ideas now.

(1) EXPORT ---> TRUNCATE ---> INPORT
This way might be better, because the number of rows to be kept looks relatively smaller than the number of rows in the table.
(1-1) EXPORT rows to be kept.
(1-2) Tuncate table. For example Truncate Table or TRUNCATE statement on DB2 9.7.
(1-3) INPORT the rows kept in (1-1).

(2) delete with commit every N rows.
See my posts(#8 - #11) in this thread
developerWorks : Information Management : IBM DB2 for Linux, Unix, and Windows Forum : Delete Script 250 rows at time ...
Maybe we can change a little for performance consideration.

Code:
BEGIN
DECLARE delete_count INT DEFAULT 0;

delete_loop:
REPEAT
   SELECT COUNT(*)
    INTO  delete_count
    FROM  OLD TABLE
         (DELETE FROM (SELECT *
                        FROM  reporter_status
                        WHERE lastoccurence < current date - 6 MONTHs
                        FETCH FIRST 250 ROWS ONLY
                      )
         );
 
   INSERT INTO log_reporter_status(text)
   VALUES 'delete_count = ' || delete_count || '.' ;
 
   COMMIT;
   if delete_count < 250 then
      leave delete_loop;
   end if;
UNTIL
   delete_count <= 0
END REPEAT;
Reply With Quote
  #7 (permalink)  
Old 07-10-12, 22:36
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,766
If additional code is to avoid a final(extra) loop,
this may be enough.

Note 1: Reformatted(adjusted leading blanks and new-line)
Note 2: Adding a meaningful name to a loop(if not necessary) may be a good habit.
Code:
BEGIN
DECLARE delete_count INT DEFAULT 0;

delete_loop:
REPEAT
   SELECT COUNT(*)
    INTO  delete_count
    FROM  OLD TABLE
         (DELETE
           FROM (SELECT *
                  FROM  reporter_status
                  WHERE lastoccurence < current date - 6 MONTHs
                  FETCH FIRST 250 ROWS ONLY
                )
         );
 
   INSERT INTO log_reporter_status(text)
   VALUES 'delete_count = ' || delete_count || '.' ;
 
   COMMIT;
UNTIL
   delete_count < 250
END REPEAT;
Reply With Quote
  #8 (permalink)  
Old 07-10-12, 23:17
fengsun2 fengsun2 is offline
Registered User
 
Join Date: Nov 2011
Posts: 279
yeah, “delete_count < 250” is better。
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On