Once we made it a true parent of the other TLD_ tables, we had a problem and had to create a script to create the TLD_HEADER records in order for the delta that creates the FK to run successfully. (Create_TLD_Header.sql)
This query was used to identify missing TLD_HEADER records from TLD_SALES_DATA:
select distinct site_no, tran_dt, reg_no, tran_noHowever in a customer's environment it was taking 7 hours and 53 minutes.
from tld_sales_data tsd
where NOT EXISTS (select *
from TLD_HEADER
where site_no = tsd.site_no
and tran_dt = tsd.tran_dt
and reg_no = tsd.reg_no
and tran_no = tsd.tran_no);
A sql guru looked at the script and suggested this query instead:
Very interesting. Do an outer join, but where the th.rowid is null? That's kind of weird. But believe it or not this query picked up the same number of records, but did so in 26 minutes. That's 18 times faster! How embarrasing. So if you need to locate records that DON'T exist, use this method where an outer join is used instead of an inner join with a NOT EXISTS!SELECT /*+ index_ffs(tsd TLD_SALES_DATA_PK) index_ffs(th TLD_HEADER_PK) */
distinct tsd.site_no, tsd.tran_dt, tsd.reg_no, tsd.tran_no
FROM tld_sales_data tsd, tld_header th
WHERE th.site_no(+) = tsd.site_no
AND th.tran_dt(+) = tsd.tran_dt
AND th.reg_no(+) = tsd.reg_no
AND th.tran_no(+) = tsd.tran_no
AND th.rowid is null;
No comments:
Post a Comment