update修改为merge(max+decode)优化

0    242    2

Tags:

👉 本文共约3360个字,系统预计阅读时间或需13分钟。

目录

    记录日期: 2014-07-30 14:25:27

    ------------- 优化方法: 减少大表扫描次数采用max+decode方式

    原sql语句:

    UPDATE RKO_ACCT_STATUS A SET RMB_PAYMENT = (SELECT NVL(SUM(POSTING_AMT), 0) FROM RKOT_ACCT_PMT_PRIOR B WHERE B.ACCT = A.ACCT AND ORG = '242' AND POSTING_DATE BETWEEN A.PRIOR_BILLING_DATE + 1 AND ADD_MONTHS(A.PRIOR_BILLING_DATE, 1) AND TXN_DATE <= (SELECT CASE WHEN USER_DATE_10=0 THEN NULL ELSE TO_DATE(USER_DATE_10, 'yyyyddd') END FROM RKOH_HAPS_AMBS_KD WHERE ACCT = A.ACCT AND ORG = 242 AND BATCH_DATE = TO_CHAR(LAST_DAY(A.PRIOR_BILLING_DATE),'yyyymmdd')) + 0.99999), USD_PAYMENT = (SELECT NVL(SUM(POSTING_AMT), 0) FROM RKOT_ACCT_PMT_PRIOR B WHERE B.ACCT = A.ACCT AND ORG = '241' AND POSTING_DATE BETWEEN A.PRIOR_BILLING_DATE + 1 AND ADD_MONTHS(A.PRIOR_BILLING_DATE, 1) AND TXN_DATE <= (SELECT CASE WHEN USER_DATE_10=0 THEN NULL ELSE TO_DATE(USER_DATE_10, 'yyyyddd') END FROM RKOH_HAPS_AMBS_KD WHERE ACCT = A.ACCT AND ORG = 241 AND BATCH_DATE = TO_CHAR(LAST_DAY(A.PRIOR_BILLING_DATE),'yyyymmdd')) + 0.99999) WHERE TO_CHAR(A.PRIOR_BILLING_DATE, 'yyyymm') = :B1;

    格式化一下:

    UPDATE RKO_ACCT_STATUS A

    SET RMB_PAYMENT =

    (SELECT NVL(SUM(POSTING_AMT),

    0)

    FROM RKOT_ACCT_PMT_PRIOR B

    WHERE B.ACCT = A.ACCT

    AND ORG = '242'

    AND POSTING_DATE BETWEEN A.PRIOR_BILLING_DATE + 1 AND

    ADD_MONTHS(A.PRIOR_BILLING_DATE,

    1)

    AND TXN_DATE <=

    (SELECT CASE

    WHEN USER_DATE_10 = 0 THEN

    NULL

    ELSE

    TO_DATE(USER_DATE_10,

    'yyyyddd')

    END

    FROM RKOH_HAPS_AMBS_KD

    WHERE ACCT = A.ACCT

    AND ORG = 242

    AND BATCH_DATE = TO_CHAR(LAST_DAY(A.PRIOR_BILLING_DATE),

    'yyyymmdd')) + 0.99999),

    USD_PAYMENT =

    (SELECT NVL(SUM(POSTING_AMT),

    0)

    FROM RKOT_ACCT_PMT_PRIOR B

    WHERE B.ACCT = A.ACCT

    AND ORG = '241'

    AND POSTING_DATE BETWEEN A.PRIOR_BILLING_DATE + 1 AND

    ADD_MONTHS(A.PRIOR_BILLING_DATE,

    1)

    AND TXN_DATE <=

    (SELECT CASE

    WHEN USER_DATE_10 = 0 THEN

    NULL

    ELSE

    TO_DATE(USER_DATE_10,

    'yyyyddd')

    END

    FROM RKOH_HAPS_AMBS_KD

    WHERE ACCT = A.ACCT

    AND ORG = 241

    AND BATCH_DATE = TO_CHAR(LAST_DAY(A.PRIOR_BILLING_DATE),

    本人提供Oracle(OCP、OCM)、MySQL(OCP)、PostgreSQL(PGCA、PGCE、PGCM)等数据库的培训和考证业务,私聊QQ646634621或微信dbaup66,谢谢!
    AiDBA后续精彩内容已被站长无情隐藏,请输入验证码解锁本文!
    验证码:
    获取验证码: 请先关注本站微信公众号,然后回复“验证码”,获取验证码。在微信里搜索“AiDBA”或者“dbaup6”或者微信扫描右侧二维码都可以关注本站微信公众号。

    标签:

    Avatar photo

    小麦苗

    学习或考证,均可联系麦老师,请加微信db_bao或QQ646634621

    您可能还喜欢...

    发表回复