Question : Changing Primary key on a big table

Hi experts, I've been using mostly SQL Server in the last 5 years, so my Oracle skills are a little outdated. I need your advice for the following :

On a datawarehouse Oracle 9i DB, a table has been wrongly designed and I'm asked to change the primary key to avoid dupes.

Let's call it STAR12, and assume the current primary key is on
(ID_MONTH int, ID_DWH int) where ID_DWH is a sequence number that resets for each new ID_MONTH

I had to alter the table to add a new field ID_GRP_RISK
And I need to
- populate the new field (I've done this part)
- delete the records where the new field is null (done it too)
- set a not null constraint on the new field
- drop the current primary key
- delete the duplicate records
- create the new primary key on (ID_MONTH, ID_GRP_RISK)

The requirements :
I've been told that applying the changes to the historical data is a requirement. Dropping the table and running the loading script to populate a brand new table for each and every month is not an option.

My current concerns :
I was wondering if it would not be better to keep the current index and just create a new unique index on (ID_MONTH, ID_GRP_RISK), and if you experts would have better ideas or advices to handle this situation.

Also I was wondering if it would not be more effective to
- create a non-unique index first, to speed up the deleting of the dupes (records that have the same ID_MONTH, ID_GRP_RISK)
- delete the dupes
- make it the primary key afterwards (is it even possible ?)

The table has about 1 million records per ID_MONTH, currently 20 months loaded.

To avoid filling the temp tablespaces and / or swapping forever, I loop on ID_Month with a cursor to put intermediary COMMITs after each month is processed.

Any ideas/sample code welcome (more efficient syntax), in particular

I will create another topic for the "delete dupes" part if need be.

Thanks for reading

Hilaire

Answer : Changing Primary key on a big table

To delete duplicates you could just use
create or replace procedure del_proc
is
TYPE T is table of rowid;
t1 t;
cursor c is
select rowid from table a where rowid > (select min(rowid)  from table b where a.col1 = b.col1 and a.col2 = b.col2 );
begin
open c;
loop
fetch c bulk collect into T;
for all i in t1.first..t1.last
loop
delete from table where rowid = t1(i);
end loop;
exit when c%notfound;
end loop;
end;

Instead an easier way would be create the primary key with the duplicates using the INTO EXCEPTIONS clause.

Step1. Create the exceptions table using utlexcpt.sql which would be under your ORACLE_HOME\rdbms folder.
Step2. Alter table add primary key tab_pk(col1,col2) exceptions into exception
Step 3.delete from table where rowid <> (select minrid from (SELECT col1,col2,MIN(e.row_id) minrid
FROM EXCEPTIONS e,table t WHERE t.ROWID=e.row_id GROUP BY col1,col2))
Random Solutions  
 
programming4us programming4us