可以让这个 SQL 语句执行得更好吗?

发布于 2024-08-04 07:50:18 字数 1780 浏览 9 评论 0原文

我有一个 SQL 语句用于将编辑从一个表合并到另一个表。也就是说

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')

,真的,我只是想获取最新的编辑(通过时间戳)字段,因为这将包含所有累积的编辑。我很确定这可以做得更好,即以某种方式将 SELECT max(mm.SSIS_TIMESTAMP)... 嵌套查询集成到其上方的联接中。

有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柒七 2024-08-11 07:50:18

有两件小事 - 但不确定它们是否有很大区别:

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)SSIS_TIMESTAMP 上是否有可以使用的索引?

2)由于您选择了 MAX 值,因此可以使用等号而不是“IN”运算符:

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

Marc

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文