Guys,
I am trying to accomplish follow task. We are using Oracle 10g.
I have a table called TABLE_A consist of 5 columns:
HGSN, YEAR, KEY, VERT, YEAR
HGSN, YEAR, KEY, VERT - make up the primary key.
Here is how records are in the table:
HGSN YEAR KEY VERT TOTAL
XX1234 2006 P 1 400
XX1234 2006 D 1 300
XX1234 2006 P 2 150
XX1234 2006 D 2 100
XX1234 2007 D 1 188
Total number of records in the table approx: 5 million
Here is what I am trying to accomplish:
Delete every records with key='D' if there is no corresponding record with key='P'
so, if you look at above dataset - last record should be deleted since there no cooresponding 'P' record.
This is what I wrote:
DELETE from TABLE_A
WHERE KEY='D'
AND HGSN||YEAR||KEY||VERT NOT IN
(SELECT HGSN||YEAR||KEY||VERT FROM TABLE_A WHERE KEY='P');
This SQL is taking a very long time...I mean hrs. I also created a unique index on all four primary key columns,
but result remain the same. How can I reduce the execution time. Any suggestions will be highly appreciated.
Thx,
Al
I am trying to accomplish follow task. We are using Oracle 10g.
I have a table called TABLE_A consist of 5 columns:
HGSN, YEAR, KEY, VERT, YEAR
HGSN, YEAR, KEY, VERT - make up the primary key.
Here is how records are in the table:
HGSN YEAR KEY VERT TOTAL
XX1234 2006 P 1 400
XX1234 2006 D 1 300
XX1234 2006 P 2 150
XX1234 2006 D 2 100
XX1234 2007 D 1 188
Total number of records in the table approx: 5 million
Here is what I am trying to accomplish:
Delete every records with key='D' if there is no corresponding record with key='P'
so, if you look at above dataset - last record should be deleted since there no cooresponding 'P' record.
This is what I wrote:
DELETE from TABLE_A
WHERE KEY='D'
AND HGSN||YEAR||KEY||VERT NOT IN
(SELECT HGSN||YEAR||KEY||VERT FROM TABLE_A WHERE KEY='P');
This SQL is taking a very long time...I mean hrs. I also created a unique index on all four primary key columns,
but result remain the same. How can I reduce the execution time. Any suggestions will be highly appreciated.
Thx,
Al