473,799 Members | 3,026 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_DE LTA 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_C RSR CURSOR WITH HOLD FOR
SELECT B.TABLE_X_CIF_I D
FROM TEST.TABLE_X_DE LTA B, TEST.TABLE_X A
WHERE A.TABLE_X_AR_ID =B.TABLE_X_AR_I D
AND A.TABLE_X_CIF_I D=B.TABLE_X_CIF _ID;

OPEN TABLE_X_DELTA_C RSR;

LOOP_TABLE_X_DE LTA : LOOP

SET VAR_CIF_ID = NULL;
FETCH TABLE_X_DELTA_C RSR INTO VAR_CIF_ID;

IF VAR_CIF_ID IS NULL THEN LEAVE LOOP_TABLE_X_DE LTA; END IF;

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

END LOOP LOOP_TABLE_X_DE LTA;

CLOSE TABLE_X_DELTA_C RSR;

END;
Nov 12 '05 #1
3 1847
"Metin Esat" <me****@hotmail .com> wrote in message
news:d7******** *************** ***@posting.goo gle.com...
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_DE LTA 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_C RSR CURSOR WITH HOLD FOR
SELECT B.TABLE_X_CIF_I D
FROM TEST.TABLE_X_DE LTA B, TEST.TABLE_X A
WHERE A.TABLE_X_AR_ID =B.TABLE_X_AR_I D
AND A.TABLE_X_CIF_I D=B.TABLE_X_CIF _ID;

OPEN TABLE_X_DELTA_C RSR;

LOOP_TABLE_X_DE LTA : LOOP

SET VAR_CIF_ID = NULL;
FETCH TABLE_X_DELTA_C RSR INTO VAR_CIF_ID;

IF VAR_CIF_ID IS NULL THEN LEAVE LOOP_TABLE_X_DE LTA; END IF;

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

END LOOP LOOP_TABLE_X_DE LTA;

CLOSE TABLE_X_DELTA_C RSR;

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_DE LTA : LOOP
DELETE FROM
(SELECT 1 FROM TEST.TABLE_X AS A
WHERE EXISTS(SELECT 1
FROM TEST.TABLE_X_DE LTA B
WHERE A.TABLE_X_AR_ID =B.TABLE_X_AR_I D
AND A.TABLE_X_CIF_I D=B.TABLE_X_CIF _ID)
FETCH FIRST 1000 ROWS ONLY) AS D
IF SQLCODE = 100 THEN LEAVE LOOP_TABLE_X_DE LTA; 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_DE LTA : LOOP
DELETE FROM
(SELECT 1 FROM TEST.TABLE_X AS A
WHERE EXISTS(SELECT 1
FROM TEST.TABLE_X_DE LTA B
WHERE A.TABLE_X_AR_ID =B.TABLE_X_AR_I D
AND A.TABLE_X_CIF_I D=B.TABLE_X_CIF _ID)
FETCH FIRST 1000 ROWS ONLY) AS D
IF SQLCODE = 100 THEN LEAVE LOOP_TABLE_X_DE LTA; 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
4417
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 problem is that the # of records that needs to be "kept" is dynamic, and stored in the Schedule table. So all records in ScheduleHistory should be purged, except for the most recent "x" number of records.
1
1760
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 an anonymous pull subscription. The merge process can be triggered by the windows sync manager and my application. To improve performance I have created some helper tables to hold the mapping between user login and primary keys of selected...
1
391
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 that.
7
15597
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 this procedure it reads a dataset and return the first line of the dataset. I met two problems: Problem 1:
6
1316
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 couple of things: 1.. Depending on parameters sent by the ASP page, do a SELECT statement to a table (tblX01) and if it returns ZERO records, the SP should insert some info into other table (tblX02) and if returns ONE record, it won't insert into...
2
1354
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 the primary key. The output should be as follows Table Name PK_Constraint Name Columns
4
17408
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 application but I do pass parameters from my Access application. What I have works fine and is attached below. But I have one question. Why do I need this line of code to execute the stored procedure. Set rst = qdf.OpenRecordset(dbOpenSnapshot) Here...
4
1492
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 = total_cost * 17.5/100 UPDATE dbo.booking_form SET GrandTotal = total_cost + VAT UPDATE dbo.booking_form SET TotalToDriver = MileageToDriver + WaitingToDriver + CarParkToDriver all of them have the same WHERE clause which I need a little...
2
3265
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 number of records. in command prompt Please colve my problem.
2
9467
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 'groupID' from the deleted GROUP 2. as each record from SUBGROUP is deleted there are multiple associated records in the table PROFILE with a matching 'subGroupID' that also need to be deleted
0
9688
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9546
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10247
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10031
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9079
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7571
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5593
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.