473,321 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

Need to improve my stored procedure , your comments are welcome!!

The following SP removes records from table A if they exist in table
B.

Table A - TEST.TABLE_X has 30 million records
Table B - TEST.TABLE_X_DELTA has 5 million records.

Appx 4 million records shoud be deleted in Table A. However my query
moves the cursor one record at a time. So this is extremely slow. The
reason I have to use a SP is bec I need to avoid fulling the log
space. Thus I need to commit frequently.

I will appreciate if you can guide me in the right direction.
Thanks a bunch!
DECLARE VAR_CIF_ID DECIMAL(9, 0);

DECLARE TABLE_X_DELTA_CRSR CURSOR WITH HOLD FOR
SELECT B.TABLE_X_CIF_ID
FROM TEST.TABLE_X_DELTA B, TEST.TABLE_X A
WHERE A.TABLE_X_AR_ID=B.TABLE_X_AR_ID
AND A.TABLE_X_CIF_ID=B.TABLE_X_CIF_ID;

OPEN TABLE_X_DELTA_CRSR;

LOOP_TABLE_X_DELTA : LOOP

SET VAR_CIF_ID = NULL;
FETCH TABLE_X_DELTA_CRSR INTO VAR_CIF_ID;

IF VAR_CIF_ID IS NULL THEN LEAVE LOOP_TABLE_X_DELTA; END IF;

DELETE FROM TEST.TABLE_X WHERE TABLE_X_CIF_ID = VAR_CIF_ID;
COMMIT;

END LOOP LOOP_TABLE_X_DELTA;

CLOSE TABLE_X_DELTA_CRSR;

END;
Nov 12 '05 #1
3 1828
"Metin Esat" <me****@hotmail.com> wrote in message
news:d7**************************@posting.google.c om...
The following SP removes records from table A if they exist in table
B.

Table A - TEST.TABLE_X has 30 million records
Table B - TEST.TABLE_X_DELTA has 5 million records.

Appx 4 million records shoud be deleted in Table A. However my query
moves the cursor one record at a time. So this is extremely slow. The
reason I have to use a SP is bec I need to avoid fulling the log
space. Thus I need to commit frequently.

I will appreciate if you can guide me in the right direction.
Thanks a bunch!
DECLARE VAR_CIF_ID DECIMAL(9, 0);

DECLARE TABLE_X_DELTA_CRSR CURSOR WITH HOLD FOR
SELECT B.TABLE_X_CIF_ID
FROM TEST.TABLE_X_DELTA B, TEST.TABLE_X A
WHERE A.TABLE_X_AR_ID=B.TABLE_X_AR_ID
AND A.TABLE_X_CIF_ID=B.TABLE_X_CIF_ID;

OPEN TABLE_X_DELTA_CRSR;

LOOP_TABLE_X_DELTA : LOOP

SET VAR_CIF_ID = NULL;
FETCH TABLE_X_DELTA_CRSR INTO VAR_CIF_ID;

IF VAR_CIF_ID IS NULL THEN LEAVE LOOP_TABLE_X_DELTA; END IF;

DELETE FROM TEST.TABLE_X WHERE TABLE_X_CIF_ID = VAR_CIF_ID;
COMMIT;

END LOOP LOOP_TABLE_X_DELTA;

CLOSE TABLE_X_DELTA_CRSR;

END;


One thing that will help is only commit every 100 - 1000 deletes. You can
set up a counter in your loop and then reset the counter with the commit.
Each commit takes longer than the delete.

I assume you have the proper indexes set up?
Nov 12 '05 #2
Try this:
DECLARE SQLCODE INTEGER;

LOOP_TABLE_X_DELTA : LOOP
DELETE FROM
(SELECT 1 FROM TEST.TABLE_X AS A
WHERE EXISTS(SELECT 1
FROM TEST.TABLE_X_DELTA B
WHERE A.TABLE_X_AR_ID=B.TABLE_X_AR_ID
AND A.TABLE_X_CIF_ID=B.TABLE_X_CIF_ID)
FETCH FIRST 1000 ROWS ONLY) AS D
IF SQLCODE = 100 THEN LEAVE LOOP_TABLE_X_DELTA; END
COMMIT;
END LOOP;
COMMIT;

Of course you can play with the value in FETCH FIRST.

Cheers
Serge
Nov 12 '05 #3
Try this:
DECLARE SQLCODE INTEGER;

LOOP_TABLE_X_DELTA : LOOP
DELETE FROM
(SELECT 1 FROM TEST.TABLE_X AS A
WHERE EXISTS(SELECT 1
FROM TEST.TABLE_X_DELTA B
WHERE A.TABLE_X_AR_ID=B.TABLE_X_AR_ID
AND A.TABLE_X_CIF_ID=B.TABLE_X_CIF_ID)
FETCH FIRST 1000 ROWS ONLY) AS D
IF SQLCODE = 100 THEN LEAVE LOOP_TABLE_X_DELTA; END IF;
COMMIT;
END LOOP;
COMMIT;

Of course you can play with the value in FETCH FIRST.

Cheers
Serge
Nov 12 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dan Caron | last post by:
I have to create a stored procedure to purge "x" # of records from a table. I have two tables (script below): Schedule ScheduleHistory I need to purge records out of ScheduleHistory. The...
1
by: tedd_n_alex | last post by:
I have database on SQL Server 2000 set up with a merge publication. This publication is configured with a number of dynamic filters to reduce the amount of data sent to each client. Each client has...
1
by: Christian Büttner | last post by:
Hi i need a stored procedure in an sql server, that inserts a query and returns a int value. the int value is an id made of a auto increment (identity(1,1) field. i have no clue how to do...
7
by: Jeff Wang | last post by:
Hi all, Can someone help me out? I've been struggling with this for almost a week and still have no clue what's wrong. Basically I want to write a DB2 stored procedure for OS/390 in REXX. In...
6
by: segis bata | last post by:
Hello everyone, I'm writing you because I need help in something that's taking too long. I want to build a Stored Procedure to be accessed by an ASP page and I need that Stored Procedure to do a...
2
by: vinod | last post by:
Dear Experts, I'm vinod, a junior DBA. working in HYD, india. i need one stored procedure. Using one procedure i want to know the columns that are associated with the primary key and the name of...
4
by: eighthman11 | last post by:
I'm calling a stored procedure on a sql server from an access application. I just need the stored procedure to run I do not need any data returned from the stored procedure to my Access...
4
by: Simon Gare | last post by:
Hi need a stored procedure to replace the 4 commands listed below UPDATE dbo.booking_form SET total_cost = mileage_charge + waiting_charge + CarParkToDriver UPDATE dbo.booking_form SET VAT =...
2
by: prashanth023 | last post by:
Hi , Can any body please solve the problem I need a stored procedure that should take a number as input for number of records to insert. And it should take the row by row data and insert n...
2
by: Deepamathi | last post by:
Thanks in advance for your help. I need a Stored Procedure that will do the following steps: 1. delete a single record from table GROUP 2. delete all records from table SUBGROUP with a matching...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.