views:

45

answers:

1

I have a SQL statement for merging edits from one table to another. I.e.

UPDATE f
SET f.AUDAPLCDE = m.AUDAPLCDE, f.AUDSTF_NO = m.AUDSTF_NO,
 f.AUDUPD_ID = m.AUDUPD_ID, f.AUDUPDDTE = m.AUDUPDDTE,
 f.UNTTYP = m.UNTTYP,  f.UNTSTM_NO = m.UNTSTM_NO,
 f.UNTIND = f.UNTIND,  f.UNQ = m.UNQ,    
 f.TRNCTL_NO = m.TRNCTL_NO, f.TRN_NO = m.TRN_NO,
 f.STAEVT = m.STAEVT,  f.SPSDTE = m.SPSDTE,
 f.RVRRSN = m.RVRRSN,  f.AUDUPDTME = m.AUDUPDTME, 
 f.RHTTYP = m.RHTTYP,  f.ALCADJ = m.ALCADJ,
 f.RESFAC = m.RESFAC,  f.PRTDTE = m.PRTDTE,
 f.POLREF = m.POLREF,  f.OUTFAC = m.OUTFAC,
 f.OTHBENQTY = m.OTHBENQTY, f.ORIIND = m.ORIIND,
 f.ORIDTE = m.ORIDTE,  f.INRTYP = m.INRTYP, 
 f.INRREF = m.INRREF,  f.FNDTRNTYP = m.FNDTRNTYP,
 f.FNDSUBTYP = m.FNDSUBTYP, f.FNDREF = m.FNDREF,
 f.FNDPRC = m.FNDPRC,  f.EVTUNQ = m.EVTUNQ,
 f.EVTTRN_NO = m.EVTTRN_NO, f.EFVDTE = m.EFVDTE,
 f.DUEDTE = m.DUEDTE,  f.CTBPCT = m.CTBPCT,
 f.CO_REF = m.CO_REF,  f.CAL_YR = m.CAL_YR,
 f.BONSUBTYP = f.BONSUBTYP, f.BONSTA = m.BONSTA,
 f.BONDCL_YR = m.BONDCL_YR, f.BON_YR = m.BON_YR, 
 f.BENTYP = m.BENTYP,  f.BENREF = m.BENREF,
 f.BENQTY2 = m.BENQTY2,  f.BENQTY1 = m.BENQTY1,
 f.AMT = m.AMT,    f.ALCRTE = m.ALCRTE
FROM FI700 f
INNER JOIN MERGEDATA_FI700 m ON m.FI700_UNIQUE_ID = f.FI700_UNIQUE_ID
                            AND m.SSIS_UPDATE_TYPE = 'U'
                            AND m.SSIS_TIMESTAMP  in (SELECT max(mm.SSIS_TIMESTAMP) 
                                                      FROM MERGEDATA_FI700 mm 
                                                      WHERE mm.FI700_UNIQUE_ID = m.FI700_UNIQUE_ID 
                                                      AND mm.SSIS_UPDATE_TYPE = 'U')

Really, I'm just trying to take the latest edit (by the TimeStamp) field, as this will contain all the cumulative edits. I'm pretty sure this can be made to perform better, i.e. somehow integrate the SELECT max(mm.SSIS_TIMESTAMP)... nested query into the join above it.

Any thoughts?

+2  A: 

Two small things - not sure if they make a big difference, though:

AND m.SSIS_TIMESTAMP  in (SELECT max(mm.SSIS_TIMESTAMP) 
                            FROM MERGEDATA_FI700 mm 
                            WHERE mm.FI700_UNIQUE_ID = m.FI700_UNIQUE_ID 
                            AND mm.SSIS_UPDATE_TYPE = 'U')

1) Is there an index on SSIS_TIMESTAMP that can be used??

2) Since you're selecting the MAX value, you could use an equal sign instead of "IN" operator:

AND m.SSIS_TIMESTAMP = (SELECT max(mm.SSIS_TIMESTAMP)

Marc

marc_s
Actually, there is no index on timestamp. I added it to the ID field and the UPDATE_TYPE field. Thanks for the '=' comment.
James Wiseman
Seeing your SELECT max() query, you might want to create an index on (FI700_UNIQUE_ID, SSIS_UPDATE_TYPE, SSIS_TIMESTAMP) since you use all three fields in your select there.
marc_s
@marc_s: Way ahead of you, but thanks any way :)
James Wiseman