Home » SQL & PL/SQL » SQL & PL/SQL » Intermittent commit while inserting
Intermittent commit while inserting [message #232699] |
Mon, 23 April 2007 01:19  |
abcindiaxyz
Messages: 13 Registered: March 2005 Location: Indonesia
|
Junior Member |
|
|
Dear All,
Hi.
My requirement is to commit the data intermittently while inserting.
Say for example there are 50,000 rows in a table "abc", and now i want to insert these 50,000 rows into another table "xyz", but the commit should happen every 5,000 records.
My requirement is urgent, so please help me in this regard as soon as possible.
|
|
|
|
|
|
|
Re: Intermittent commit while inserting [message #232751 is a reply to message #232699] |
Mon, 23 April 2007 03:21   |
kc1982
Messages: 13 Registered: October 2006
|
Junior Member |
|
|
sorry, didn't read the message well.
DECLARE
CURSOR TEST IS
SELECT COLUMN_1 FROM ABC;
COMMIT_NUMBER NUMBER := 0;
BEGIN
FOR CUR_REC IN TEST LOOP
INSERT INTO XYZ (COLUMN1) VALUES (CUR_REC.COLUMN_1);
COMMIT_NUMBER:=COMMIT_NUMBER+1;
IF MOD(COMMIT_NUMBER, 5000) = 0 THEN
COMMIT;
END IF;
END LOOP;
END;
|
|
|
|
Re: Intermittent commit while inserting [message #232757 is a reply to message #232755] |
Mon, 23 April 2007 03:47   |
JRowbottom
Messages: 5933 Registered: June 2006 Location: Sunny North Yorkshire, ho...
|
Senior Member |
|
|
Here's a thought - instead of doing a commit, why not set a savepoint, and then add an exception block that will do a ROLLBACK TO SAVEPOINT, and then continue inserting. That way if you do get errors in the insert you won't loose everything (which is I guess why you want to do this) and it will still be better than Commiting.
It'll still be slower than just doing one single SQL insert statement though.
|
|
|
|
Re: Intermittent commit while inserting [message #242498 is a reply to message #232699] |
Sat, 02 June 2007 20:08   |
abcindiaxyz
Messages: 13 Registered: March 2005 Location: Indonesia
|
Junior Member |
|
|
Here is the final query i used (modified a bit to hide my tables).
DECLARE
INSERT_COUNT NUMBER := 1;
CURSOR c1 IS SELECT /*+ PARALLEL ( SEL_TBL 2) FULL (SEL_TBL) */ * from schema_name1.table_name1 SEL_TBL;
BEGIN
FOR r1 IN c1 LOOP
INSERT /*+ PARALLEL ( INS_TBL 2) APPEND */ INTO schema_name1.table_name1 INS_TBL (columns) VALUES (values);
-- commit after every 100 inserts to avoid large rollback segments
IF INSERT_COUNT = 100 THEN
COMMIT;
INSERT_COUNT := 1;
ELSE
INSERT_COUNT := INSERT_COUNT + 1;
END IF;
END LOOP ;
COMMIT;
END
;
/
Commit ;
THANKS all for your replies
|
|
|
|
Re: Intermittent commit while inserting [message #242507 is a reply to message #242498] |
Sun, 03 June 2007 01:05   |
 |
Michel Cadot
Messages: 68757 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
Quote: | INSERT /*+ PARALLEL ( INS_TBL 2) APPEND */ INTO schema_name1.table_name1 INS_TBL (columns) VALUES (values);
|
This is the same thing as:
INSERT /*+ Oracle don't care about what you put there */ INTO schema_name1.table_name1 INS_TBL (columns) VALUES (values);
"SELECT * FROM tab;" with a FULL hint a non-sense. What can Oracle do if not a FULL SCAN?
If you don't know what are and when to use hints, DO NOT USE THEM.
I still don't understand why you're not using something like:
INSERT INTO ... SELECT ... FROM ...;
Regards
Michel
[Updated on: Sun, 03 June 2007 01:05] Report message to a moderator
|
|
|
|
Re: Intermittent commit while inserting [message #242523 is a reply to message #242507] |
Sun, 03 June 2007 09:45   |
William Robertson
Messages: 1643 Registered: August 2003 Location: London, UK
|
Senior Member |
|
|
Good point about the hints - they are doing nothing here. Neither parallel nor direct-path make any sense in a single-row INSERT, and so both hints will be ignored.
Also agree regarding the inadvisability of committing in loops. If your database can't handle 2.5 million rows in one INSERT statement it's probably time to get a new laptop. And even if for some bizarre reason you absolutely HAVE to do the processing in a loop, you could still use bulk collect and FORALL to retain some chance of reasonable performance.
|
|
|
|
|
Re: Intermittent commit while inserting [message #242551 is a reply to message #242546] |
Mon, 04 June 2007 00:56  |
 |
Michel Cadot
Messages: 68757 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
Frank,
I didn't answer to William's post, I wanted to strenghten it showing that it is more than a sarcarsm, it is reality, even with not the latest laptop model.
Regards
Michel
|
|
|
Goto Forum:
Current Time: Tue May 20 09:05:19 CDT 2025
|