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 > update 10 million records

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-26-09, 13:30
assafp76 assafp76 is offline
Registered User
 
Join Date: Jul 2009
Posts: 1
update 10 million records

Hi,
I need to update 10 million records in Stored Procedure:

BEGIN
UPDATE table
SET feild = feild*(CONSTANT_VALUE);
END

The probles is performance.
Someone told me it's becuse of the spool file (or log) that get bigger and bigger .He said that only at the end of the update process commit is done.
So, my questions is:
1. Is there a way to prevent writting to the log?
(i tried to write "with none" at the end of the Stored Procedure- it's not working)
2. Is there a way to do a commit every n record? (without a cursor)

thanks,
Assaf
Reply With Quote
  #2 (permalink)  
Old 07-26-09, 14:09
db2girl db2girl is offline
∞∞∞∞∞∞
 
Join Date: Aug 2008
Location: Toronto, Canada
Posts: 1,816
The following link has an example of how to delete with commitcount:
How to DELETE with COMMIT COUNT - bytes

This may give you some idea on how to update with commitcount.


Also, in DB2 LUW you temporary turn off logging for a UOW. Please check the following technote: IBM - How to temporarily turn logging off for operations modifying the table?

Not exactly sure whether this option works with stored proc, but I don't see why it wouldn't as long as the alter table and stored proc with update are executed as one UOW.

May be people who are more familiar with stored proc can offer other suggestions.
Reply With Quote
  #3 (permalink)  
Old 07-26-09, 14:22
db2girl db2girl is offline
∞∞∞∞∞∞
 
Join Date: Aug 2008
Location: Toronto, Canada
Posts: 1,816
Just re-read the technote again and the notes section does mention about stored proc so this option should work for you. Please note that using this option can leave your table inaccessible (if some error occurs during execution or during rollforward). Please check the notes section in the technote and can also read here: http://publib.boulder.ibm.com/infoce.../c0006079.html

So, consider taking a backup before and after using the not logged initially option if this is an important table.

Last edited by db2girl; 07-26-09 at 14:26.
Reply With Quote
  #4 (permalink)  
Old 07-26-09, 17:10
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
The proper way would be to open a cursor in the SP, and the fetch each row to be updated, and then update or delete the row using the PK of the row. You can do a commit every x number of updates (I would recommend no more than 1000). This allows maximum concurrency with other applications (depending on how often you commit) and logs all the data, although it will not fill up the active log files since you are doing commits (but make sure you have plenty of archive log space or you are using circular logging).

Using this method, you must define the cursor WITH HOLD, so it will not be closed when a commit is issued.

If you are only doing this once, and don't mind taking a backup before and after, then the NOT LOGGED INITIALLY method is acceptable. Unlike earlier versions of DB2, it is no longer required that that table be defined with NOT LOGGED INITIALLY in order to temporarily turn logging off (alter the table with NOT LOGGED INITIALLY). Don't forget to run the script auto-commit turned off, and then do an explicit commit when you are done, and then do a backup when you are finished.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #5 (permalink)  
Old 07-27-09, 08:59
DBFinder DBFinder is offline
Registered User
 
Join Date: Sep 2008
Location: Toronto,Canada
Posts: 606
Assaf,

If you are not comfortable with such an SP, I may be able to provide you a template of this SP ?( as Marcus said).
Let me know,

Quote:
Originally Posted by assafp76
2. Is there a way to do a commit every n record? (without a cursor)

Assaf
Well, I used a technique ( called chunking ) which is not for all situations. It was with additional sub-query having FETCH FIRST 20000 ROWS ONLY -- and of course repeating it in a for loop in batch file till all rows are done. This woks in some (few) situations. I used it once for update and once in delete. Few other situations it was not feasible because it was creating recursiveness.

I would say SP approach using cursor with hold is most reliable method , especially in production database.


DBFinder

Last edited by DBFinder; 07-27-09 at 09:11.
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