博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Tuning SQL via case when statement
阅读量:5994 次
发布时间:2019-06-20

本文共 2808 字,大约阅读时间需要 9 分钟。

原SQL如下:SQL的主要问题是红色部分居然通过标量查询,反复的查找与SQL相同的基表,很显然这个可以用case when来简化。 select a.TRAN_ID, a.AMOUNT, a.BALANCE, a.INVAMT, a.PROMISED, a.INVNO, a.RCLNUM, b.PROBLEM_ID,a.TRANTYPE, a.TYPE, a.DUEDATE, a.INVDATE, a.RCLDATE, a.LASTTYPE, a.LOCBAL, a.CUSTNO, b.STATUS, a.PAYMENTS_PENDING, isnull((  SELECT 'Y'  FROM GPCOMP1.GPRECL aa  with (NOLOCK) LEFT OUTER JOIN GPCOMP1.GPPROB b  with (NOLOCK) ON (aa.tran_id = b.open_invoice_tran_id), GPCOMP1.GPTRCTRL c  with (NOLOCK)        WHERE  aa.tran_id = a.tran_id        AND aa.trantype = c.trantype           AND           (            (               c.CAPPLSTRAT = 'Y'             AND (b.PROBLEM_ID IS NULL OR (b.PROBLEM_ID IS NOT NULL AND b.STATUS = 'C' ))   AND ((c.DAPPLSTRAT = 'Y' AND c.CAPPLSTRAT = 'Y') OR C.SAPPLYSTRAT='Y' OR (C.SAPPLYSTRAT='N' AND     aa.INVNO not in (select distinct (invno) from GPCOMP1.GPRECL r  with (NOLOCK)        where tran_id = (select open_invoice_tran_id from GPCOMP1.GPPROB p  with (NOLOCK)  where p.open_invoice_tran_id = r.tran_id))))            )            OR            ( c.DAPPLSTRAT = 'Y' AND (b.PROBLEM_ID IS NOT NULL AND b.STATUS <> 'C' ) )            )            AND          (             ( c.PPAPPLSTRAT = 'Y' AND aa.PAYMENTS_PENDING = 'Y' )             OR             ( c.NPPAPPLSTRAT = 'Y' AND aa.PAYMENTS_PENDING != 'Y' )          )       ),'N') as STRATEGIC  from GPCOMP1.GPRECL a  with (NOLOCK) LEFT OUTER JOIN GPCOMP1.GPPROB b  with (NOLOCK)  on (b.OPEN_INVOICE_TRAN_ID = a.TRAN_ID)where a.CUSTNO= @P0  order by a.INVNO

 

changed to

selecta.TRAN_ID,a.AMOUNT,a.BALANCE,a.INVAMT,a.PROMISED,a.INVNO,a.RCLNUM,b.PROBLEM_ID,a.TRANTYPE,a.TYPE,a.DUEDATE,a.INVDATE,a.RCLDATE,a.LASTTYPE,a.LOCBAL,a.CUSTNO,b.STATUS,a.PAYMENTS_PENDING,CASEWHEN c.PPAPPLSTRAT = 'Y'     AND a.PAYMENTS_PENDING = 'Y'     AND c.DAPPLSTRAT = 'Y'     AND (b.PROBLEM_ID IS NOT NULL AND b.STATUS <> 'C' )THEN 'Y'WHEN c.PPAPPLSTRAT = 'Y'     AND a.PAYMENTS_PENDING = 'Y'     AND c.CAPPLSTRAT = 'Y'     AND (b.PROBLEM_ID IS NULL OR (b.PROBLEM_ID IS NOT NULL AND b.STATUS = 'C' ))     AND (c.DAPPLSTRAT = 'Y' or C.SAPPLYSTRAT='Y' or (C.SAPPLYSTRAT='N' AND b.open_invoice_tran_id is null))THEN 'Y'WHEN c.NPPAPPLSTRAT = 'Y'     AND a.PAYMENTS_PENDING != 'Y'     AND c.DAPPLSTRAT = 'Y'     AND (b.PROBLEM_ID IS NOT NULL AND b.STATUS <> 'C' )THEN 'Y'WHEN c.NPPAPPLSTRAT = 'Y'     AND a.PAYMENTS_PENDING != 'Y'     AND c.CAPPLSTRAT = 'Y'     AND (b.PROBLEM_ID IS NULL OR (b.PROBLEM_ID IS NOT NULL AND b.STATUS = 'C' ))     AND (c.DAPPLSTRAT = 'Y' or C.SAPPLYSTRAT='Y' or (C.SAPPLYSTRAT='N' and b.open_invoice_tran_id is null))THEN 'Y'ELSE 'N'END  as  STRATEGICFROM GPCOMP1.GPRECL a LEFT OUTER JOIN GPCOMP1.GPPROB b      on (b.OPEN_INVOICE_TRAN_ID = a.TRAN_ID)LEFT OUTER JOIN GPCOMP1.GPTRCTRL c    on (c.trantype = a.trantype)where a.CUSTNO= '12345'  order by a.INVNO

转载地址:http://urmlx.baihongyu.com/

你可能感兴趣的文章
Mysql 不同条件进行修改存储
查看>>
百度地图给map添加右键菜单(判断是否为marker)
查看>>
线程的状态
查看>>
CentOS 6.4上通过yum安装Nagios-3.5.0 [服务端/server端]
查看>>
ODL Beryllium (Be) - The Fourth Release of OpenDaylight PRODUCTION-READY OPEN SDN PLATFORM
查看>>
Cortex-M0 ARM-GCC 编译链接选项
查看>>
JFinal中EhCache利用RMI方式分布式缓存配置
查看>>
谈传奇世界3D版
查看>>
我的友情链接
查看>>
【阿里云新品发布·周刊】第8期:数字化风暴已经来临!云+区块链,如何颠覆未来科技?...
查看>>
python开发编译器
查看>>
演示:使用IPsec+PKI来完成IP通信的安全
查看>>
Maven和Gradle对比
查看>>
C语言extern关键字用法
查看>>
我的LINUX之路----安装LINUX及远程连接
查看>>
如何提高Java并行程序性能
查看>>
数据加密到底管不管用
查看>>
面向对象程序与类
查看>>
安装vsftpd
查看>>
Linux性能分析的前60000毫秒
查看>>