sql|达梦sql优化实践5

Fgwzd项目sql语句优化
原sql如下:
–页脚统计
select /*+ ADAPTIVE_NPLN_FLAG(3) */
count(0) ,
nvl(sum(TOTAL_INVEST), 0),
nvl(sum(NUM), 0) ,
nvl(sum(INVEST_REQUIREMENTS), 0)
FROM
(
SELECT
LBFN.ID ,
LBFN.FILE_NO ,
LBFN.FILE_NAME ,
COUNT(LBPM.ID) AS NUM ,
SUM(NVL(LBPM.INVESTMENT_TOTAL, 0)) AS TOTAL_INVEST ,
LBFN.LOCKED ,
LBFN.STATUS ,
TO_CHAR(LBFN.SUBMIT_TIME, ‘YYYY-MM-DD HH24:MI:SS’) AS SUBMIT_TIME ,
LBFN.FILE_VERSION ,
LBFN.DEPARTMENT_GUID ,
SUM(case when LBPM.LOCAL_BOND =1 and to_char(LBPM.EXPECT_ENDYEAR, ‘yyyy-MM’) >= to_char(add_months(SYSDATE, 2), ‘yyyy-MM’) then 0 when LBPM.ID is null then 0 else CASE WHEN INSTR(SS.PARAM_VALUE, LBFN.FILE_NAME)>0 then 0 else 1 end end) AS BOND_CHECK,
SUM(case when regexp_like(LBPM.PRO_CODE_AREA, ‘[-]{4}-[-]{6}-[-]{2}-[-]{2}-[-]{6}$’)=1 or LBPM.ID IS NULL then 0 else 1 end) AS CODE_CHECK,
SUM(case when LBPM.LOCAL_BOND =1 and (LBPM.PROJECT_INCOME_SOURCE IS NULL OR LBPM.PROJECT_INCOME_SOURCE=’’
) then 1 else 0 end) AS SOURCE_CHECK ,
LBFN.YEAR ,
LBFN.SOURCE ,
LBFN.SYS_LOCK ,
LBFN.WRAPUP ,
sum(case when LBFN.year=2015 then nvl(LBPIEI.APPLY_CAPTIAL_2015, 0) when LBFN.year=2016 then nvl(LBPIEI.APPLY_CAPTIAL_2016, 0) when LBFN.year=2017 then nvl(LBPIEI.APPLY_CAPTIAL_2017, 0) when LBFN.year=2018 then nvl(LBPIEI.APPLY_CAPTIAL_2018, 0) when LBFN.year=2019 then nvl(LBPIEI.APPLY_CAPTIAL_2019, 0) when LBFN.year=2020 then nvl(LBPIEI.APPLY_CAPTIAL_2020, 0) when LBFN.year=2021 then nvl(LBPIEI.APPLY_CAPTIAL_2021, 0) when LBFN.year=2022 then nvl(LBPIEI.APPLY_CAPTIAL_2022, 0) when LBFN.year=2023 then nvl(LBPIEI.APPLY_CAPTIAL_2023, 0) when LBFN.year=2024 then nvl(LBPIEI.APPLY_CAPTIAL_2024, 0) when LBFN.year=2025 then nvl(LBPIEI.APPLY_CAPTIAL_2025, 0) when LBFN.year=2026 then nvl(LBPIEI.APPLY_CAPTIAL_2026, 0) when LBFN.year=2027 then nvl(LBPIEI.APPLY_CAPTIAL_2027, 0) when LBFN.year=2028 then nvl(LBPIEI.APPLY_CAPTIAL_2028, 0) when LBFN.year=2029 then nvl(LBPIEI.APPLY_CAPTIAL_2029, 0) when LBFN.year=2030 then nvl(LBPIEI.APPLY_CAPTIAL_2030, 0) end) as INVEST_REQUIREMENTS,
REPLACE(LBFN.DEPARTMENT_FULLNAME, ‘varchar1’, ‘’) AS DEPARTMENT_FULLNAME
FROM
table1 LBFN
LEFT JOIN
(
SELECT
LBP.ID ,
LBP.FILE_NO_ID ,
LBPIEM.LOCAL_BOND ,
LBPIEM.EXPECT_ENDYEAR ,
LBPIEM.PRO_CODE_AREA ,
LBPIEM.PROJECT_INCOME_SOURCE,
NVL(LBPIEM.INVESTMENT_TOTAL, 0) AS INVESTMENT_TOTAL
FROM
table2 LBP
JOIN table2_INFO_EXT_MASTER LBPIEM
ON
LBP.ID = LBPIEM.PROJECT_ID
WHERE
1=1
)
LBPM ON LBFN.ID = LBPM.FILE_NO_ID
LEFT JOIN
(
select
APPLY_CAPTIAL_2015,
APPLY_CAPTIAL_2016,
APPLY_CAPTIAL_2017,
APPLY_CAPTIAL_2018,
APPLY_CAPTIAL_2019,
APPLY_CAPTIAL_2020,
APPLY_CAPTIAL_2021,
APPLY_CAPTIAL_2022,
APPLY_CAPTIAL_2023,
APPLY_CAPTIAL_2024,
APPLY_CAPTIAL_2025,
PROJECT_ID ,
APPLY_CAPTIAL_2026,
APPLY_CAPTIAL_2027,
APPLY_CAPTIAL_2028,
APPLY_CAPTIAL_2029,
APPLY_CAPTIAL_2030
from
table2_INFO_EXT_INVEST
where
project_item_ext_id=‘A00069’
)
LBPIEI
ON
LBPM.ID = LBPIEI.PROJECT_ID
LEFT JOIN SYSTEM_SETTINGS SS
ON
SS.PARAM_NAME = ‘SYSTEM_LOCALBOND_ENDTIME_FLAG’
–WHERE
–LBFN.DEPARTMENT_GUID = ‘JIANGXI-1’
GROUP BY
LBFN.ID ,
LBFN.FILE_NO ,
LBFN.FILE_NAME ,
LBFN.LOCKED ,
LBFN.STATUS ,
TO_CHAR(LBFN.SUBMIT_TIME, ‘YYYY-MM-DD HH24:MI:SS’),
LBFN.FILE_VERSION ,
LBFN.DEPARTMENT_GUID ,
LBFN.YEAR ,
LBFN.SOURCE ,
LBFN.SYS_LOCK ,
REPLACE(LBFN.DEPARTMENT_FULLNAME, ‘varchar1’, ‘’) ,
LBFN.WRAPUP
);
其中加粗部分,表示与ss表进行笛卡尔积关联。
经过沟通,需要进行笛卡尔积关联的表是一个一对一配置的码表:
select ss.param_value from table4 SS where ss.PARAM_NAME = ‘SYSTEM_table2_ENDTIME_FLAG’;
优化方法如下:
消除对ss表的笛卡尔积关联,把语句拆开成两步用高级语言做,1、执行select ss.param_value from SYSTEM_SETTINGS SS where ss.PARAM_NAME = ‘SYSTEM_table2_ENDTIME_FLAG’; 2、把这个param_value作为常量传到sql的select字句里的加粗部分:
–页脚统计
select /*+ ADAPTIVE_NPLN_FLAG(3) /
count(0) ,
nvl(sum(TOTAL_INVEST), 0),
nvl(sum(NUM), 0) ,
nvl(sum(INVEST_REQUIREMENTS), 0)
FROM
(
SELECT
LBFN.ID ,
LBFN.FILE_NO ,
LBFN.FILE_NAME ,
COUNT(LBPM.ID) AS NUM ,
SUM(NVL(LBPM.INVESTMENT_TOTAL, 0)) AS TOTAL_INVEST ,
LBFN.LOCKED ,
LBFN.STATUS ,
TO_CHAR(LBFN.SUBMIT_TIME, ‘YYYY-MM-DD HH24:MI:SS’) AS SUBMIT_TIME ,
LBFN.FILE_VERSION ,
LBFN.DEPARTMENT_GUID ,
SUM(case when LBPM.LOCAL_BOND =1 and to_char(LBPM.EXPECT_ENDYEAR, ‘yyyy-MM’) >= to_char(add_months(SYSDATE, 2), ‘yyyy-MM’) then 0 when LBPM.ID is null then 0 else CASE WHEN INSTR(/SS.PARAM_VALUE/’PARAM_VALUE’, LBFN.FILE_NAME)>0 then 0 else 1 end end) AS BOND_CHECK,
SUM(case when regexp_like(LBPM.PRO_CODE_AREA, ‘[-]{4}-[-]{6}-[-]{2}-[-]{2}-[-]{6}$’)=1 or LBPM.ID IS NULL then 0 else 1 end) AS CODE_CHECK,
SUM(case when LBPM.LOCAL_BOND =1 and (LBPM.PROJECT_INCOME_SOURCE IS NULL OR LBPM.PROJECT_INCOME_SOURCE=’’
) then 1 else 0 end) AS SOURCE_CHECK ,
LBFN.YEAR ,
LBFN.SOURCE ,
LBFN.SYS_LOCK ,
LBFN.WRAPUP ,
sum(case when LBFN.year=2015 then nvl(LBPIEI.APPLY_CAPTIAL_2015, 0) when LBFN.year=2016 then nvl(LBPIEI.APPLY_CAPTIAL_2016, 0) when LBFN.year=2017 then nvl(LBPIEI.APPLY_CAPTIAL_2017, 0) when LBFN.year=2018 then nvl(LBPIEI.APPLY_CAPTIAL_2018, 0) when LBFN.year=2019 then nvl(LBPIEI.APPLY_CAPTIAL_2019, 0) when LBFN.year=2020 then nvl(LBPIEI.APPLY_CAPTIAL_2020, 0) when LBFN.year=2021 then nvl(LBPIEI.APPLY_CAPTIAL_2021, 0) when LBFN.year=2022 then nvl(LBPIEI.APPLY_CAPTIAL_2022, 0) when LBFN.year=2023 then nvl(LBPIEI.APPLY_CAPTIAL_2023, 0) when LBFN.year=2024 then nvl(LBPIEI.APPLY_CAPTIAL_2024, 0) when LBFN.year=2025 then nvl(LBPIEI.APPLY_CAPTIAL_2025, 0) when LBFN.year=2026 then nvl(LBPIEI.APPLY_CAPTIAL_2026, 0) when LBFN.year=2027 then nvl(LBPIEI.APPLY_CAPTIAL_2027, 0) when LBFN.year=2028 then nvl(LBPIEI.APPLY_CAPTIAL_2028, 0) when LBFN.year=2029 then nvl(LBPIEI.APPLY_CAPTIAL_2029, 0) when LBFN.year=2030 then nvl(LBPIEI.APPLY_CAPTIAL_2030, 0) end) as INVEST_REQUIREMENTS,
REPLACE(LBFN.DEPARTMENT_FULLNAME, ‘varchar1’, ‘’) AS DEPARTMENT_FULLNAME
FROM
table1 LBFN
LEFT JOIN
(
SELECT
LBP.ID ,
LBP.FILE_NO_ID ,
LBPIEM.LOCAL_BOND ,
LBPIEM.EXPECT_ENDYEAR ,
LBPIEM.PRO_CODE_AREA ,
LBPIEM.PROJECT_INCOME_SOURCE,
NVL(LBPIEM.INVESTMENT_TOTAL, 0) AS INVESTMENT_TOTAL
FROM
table2 LBP
JOIN table2_INFO_EXT_MASTER LBPIEM
ON
LBP.ID = LBPIEM.PROJECT_ID
WHERE
1=1
)
LBPM ON LBFN.ID = LBPM.FILE_NO_ID
LEFT JOIN
(
select
APPLY_CAPTIAL_2015,
APPLY_CAPTIAL_2016,
APPLY_CAPTIAL_2017,
APPLY_CAPTIAL_2018,
APPLY_CAPTIAL_2019,
APPLY_CAPTIAL_2020,
APPLY_CAPTIAL_2021,
APPLY_CAPTIAL_2022,
APPLY_CAPTIAL_2023,
APPLY_CAPTIAL_2024,
APPLY_CAPTIAL_2025,
PROJECT_ID ,
APPLY_CAPTIAL_2026,
APPLY_CAPTIAL_2027,
APPLY_CAPTIAL_2028,
APPLY_CAPTIAL_2029,
APPLY_CAPTIAL_2030
from
table2_INFO_EXT_INVEST
where
project_item_ext_id=‘A00069’
)
LBPIEI
ON
LBPM.ID = LBPIEI.PROJECT_ID
/
LEFT JOIN SYSTEM_SETTINGS SS
ON
SS.PARAM_NAME = ‘SYSTEM_LOCALBOND_ENDTIME_FLAG’ */
–WHERE
–LBFN.DEPARTMENT_GUID = ‘JIANGXI-1’
GROUP BY
LBFN.ID ,
LBFN.FILE_NO ,
LBFN.FILE_NAME ,
LBFN.LOCKED ,
LBFN.STATUS ,
TO_CHAR(LBFN.SUBMIT_TIME, ‘YYYY-MM-DD HH24:MI:SS’),
LBFN.FILE_VERSION ,
LBFN.DEPARTMENT_GUID ,
LBFN.YEAR ,
LBFN.SOURCE ,
LBFN.SYS_LOCK ,
REPLACE(LBFN.DEPARTMENT_FULLNAME, ‘varchar1’, ‘’) ,
LBFN.WRAPUP
);
优化后,执行时间从6秒,下降到2秒左右。
【sql|达梦sql优化实践5】达梦云适配技术社区的网址。https://eco.dameng.com

    推荐阅读