I have to compare a date column available in table and view, any missing date in table will add record from the view. Both table and view has 100 millions of data. I have written below code but to iterate over 400K rows it takes 2hr each for table and view. Would like to know if I can improve my code to compare. Please don't ask why I add record from View to table, this is bit complicated setup I cannot help on this.
Note -
- I have to run this for around 100+ tables.
- There are different date formats for different tables/ views. But it remains same for the table and corresponding view with which we will do the comparison.
- In some tables there are invalid dates, those records should be ignored. Am achieving this by using TO_DATE function in the code, if exception am continuing.
- Date format for each table/ view is maintained in a MASTER table.
Did not include initial Code to read MASTER table with BEGIN and DECLARE
query_str := 'select DISTINCT ' || date_column_name || ' as DATM FROM ' || tablename;
OPEN c_query FOR query_str;
LOOP
BEGIN
FETCH c_query INTO dateval;
EXIT WHEN c_query%notfound;
tmp := TO_DATE(
dateval,
dateformat
);
tabledatelist(dateval) := 'Date Val';
EXCEPTION
WHEN OTHERS THEN
continue;
END;
END LOOP;
CLOSE c_query;
query_str := 'select DISTINCT ' || date_column_name || ' as DATM FROM ' || viewname;
OPEN c_query FOR query_str;
LOOP
BEGIN
FETCH c_query INTO dateval;
EXIT WHEN c_query%notfound;
tmp := TO_DATE(
dateval,
dateformat
);
IF
NOT tabledatelist.EXISTS(dateval)
THEN
-- Code to add missing records. Using it as Insert into select col_name from table.
END IF;
EXCEPTION
WHEN OTHERS THEN
continue;
END;
END LOOP;
CLOSE c_query;
varcharcolumn to begin with. Do you have a chance to fix that broken design before you proceed.