优化笔记:jsyhjkzqxx_D_20140916.gz

2019年11月12日 阅读数:350
这篇文章主要向大家介绍优化笔记:jsyhjkzqxx_D_20140916.gz,主要内容包括基础应用、实用技巧、原理机制等方面,希望对大家有所帮助。

有几张表没有权限,因此跑不起来。算法

 

目测黄色部分比较坑爹,死了n多脑细胞才看懂,又死了n多脑细胞才改出来。对5034进行了2次扫描,并屡次分组排序求和。(分组和排序算法相对来讲比较耗性能)sql

 

改成只扫描一次,一次编号操做。oracle

 

 

 

这个没有什么既定的规则,就是使劲想各类奇葩办法简化。(这个改写就是利用了oracle在排序的时候,把null当成无穷大。而在sqlserver上就不适用了,由于sqlserver在排序的时候把null当无穷小)ide

 

 

 

黄色部分可改成下面的代码。sqlserver

 

 

 

select *性能

 

  FROM (select f1_5034,spa

 

               decode(min(rn_a) - 1, 1, 0, min(rn_a) - 1) ylqk,code

 

               min(rn_b) - 1 bbqkorm

 

          from (select caseserver

 

                         when nvl(f25_5034, 0) > 0 then null

 

                         else rn end rn_a,

 

                       case

 

                         when f25_5034 Is not Null then null

 

                         else rn end rn_b,

 

                       f1_5034

 

                  from (Select f1_5034,

 

                               f3_5034,

 

                               f25_5034,

 

                               row_number() Over(partition By f1_5034 Order By f3_5034 Desc) rn

 

                          From wind.tb_object_5034

 

                         Where f3_5034 Like '%1231'))

 

         group by f1_5034)

 

 where bbqk is not null

 

 

 

--------------------原代码-------------------------

 

 

 

 

 

Select fdate,zqdm,zqmc,jysc,xzjq,dqsyl,fsynx,ffxjg,fpmje,fsqpmlv,ffxcs,ffxfs,fscfxr,

 

fssdd,fjxqsr,jxjzr,fxgm,cqxypj,dqxypj,'NULL' gjxypj,jhht,fldzrdb,ffxgsdm,fgsjc,zqlx,nvl(bbqk,0) bbqk,fxrq,

 

Case When nvl(zzc,0)<>0  Then 1 Else 0 End As xxplqk,ylqk ylqk,

 

hxzbczl,zzc,jzc,DbrXyPj,rzzt,shzt,'1' lsyjs,Jjbz, cxsmc, cxqsr,cxjzr,bjyy,lvlx,syyhdb,FCqXypj,FDqXypj,FGjxypj,FGnxypjJgd,FGjxypjJgd,DCqXypj,

 

'NULL' DGjxypj,flvjz,'NULL' FYsSyr, '0' fsdq ,'NULL' fsdqsr ,'NULL' fsdjzr ,'NULL' Fsdjzr2 ,fjcjq,flljq, '1' ffxzt,flvjzlb,'1' Ffxlx, fdbrdm,fdbr,Fdbrxz,FdbrSnmJzc,fsndgncqxy,Fdbrgnpjjg,

 

Ffxrlb,fzgr,fzgjzr,x.f13_5015 fzgbl,x.f12_5015 fzgjg, 'NULL' fzgzqdm,fsfsh,'NULL' fjkyy,fdbrzt,fggxx,

 

(Select F68_1854

 

  From wind.tb_object_1854 T

 

 Where nvl(BbQk, 0) >= 1

 

   And F1_1854 = F25_4516

 

   And F4_1854 = '合并报表'

 

   And substr(F2_1854, 5, 8) = '1231'

 

   And f2_1854 = (Select Max(f2_1854)

 

                    From Wind.tb_object_1854

 

                   Where T.f1_1854 = f1_1854

 

                     And substr(F2_1854, 5, 8)='1231'

 

                     And f4_1854 = '合并报表')) fsnlr,

 

  (Select F68_1854

 

     From wind.tb_object_1854 T

 

    Where nvl(BbQk,0) >= 2

 

    And F1_1854 =F25_4516

 

    And F4_1854 ='合并报表'

 

    And substr(F2_1854, 5, 8) = '1231'

 

    And f2_1854 = (Select substr(Max(f2_1854),1,4)-1||'1231'

 

                    From Wind.tb_object_1854

 

                   Where T.f1_1854 = f1_1854

 

                     And substr(F2_1854, 5, 8)='1231'

 

                     And f4_1854 = '合并报表')) fqnlr,

 

(Select F68_1854

 

     From wind.tb_object_1854 T

 

    Where nvl(BbQk,0) >= 3

 

    And F1_1854 =F25_4516

 

    And F4_1854 ='合并报表'

 

    And substr(F2_1854, 5, 8) = '1231'

 

    And f2_1854 = (Select substr(Max(f2_1854),1,4)-2||'1231'

 

                    From Wind.tb_object_1854

 

                   Where T.f1_1854 = f1_1854

 

                     And substr(F2_1854, 5, 8)='1231'

 

                     And f4_1854 = '合并报表')) fdqnlr,

 

(Select F140_1853

 

  From wind.tb_object_1853 T

 

 Where nvl(BbQk, 0) >= 1

 

   And F1_1853 = F25_4516

 

   And F4_1853 = '合并报表'

 

   And substr(F2_1853, 5, 8) = '1231'

 

   And f2_1853 = (Select Max(f2_1853)

 

                    From Wind.tb_object_1853

 

                   Where T.f1_1853 = f1_1853

 

                     And substr(F2_1853, 5, 8)='1231'

 

                     And f4_1853 = '合并报表'))  FsnmfxrNav

 

,  Ffxrdchqyz

 

 

 

  From (

 

  (SELECT to_char(&PlanTime - 1, 'YYYYMMDD') fdate,

 

                f1_4516 zqdm,

 

                f2_4516 zqmc,

 

                f43_4516,

 

                f25_4516,

 

                f40_1429,

 

                Case

 

                  When f12_4516 = '上交所' Then '1'

 

                  When f12_4516 = '深交所' Then '2'

 

                  When f12_4516 = '银行间' Then '3'

 

                  Else '4' End As jysc,

 

                Case

 

                  When f2_1639 = to_char(&PlanTime - 1, 'YYYYMMDD') Then f5_1639

 

                  Else  0

 

                  End As xzjq,

 

                Case

 

                  When f2_1639 = to_char(&PlanTime - 1, 'YYYYMMDD') Then  f3_1639 Else Null End As dqsyl,

 

                Case

 

                  When f2_1639 = to_char(&PlanTime - 1, 'YYYYMMDD') Then  f9_1639

 

                  Else  0

 

                End As fsynx,

 

                f8_4516 ffxjg,

 

                f7_4516 fpmje,

 

                f5_4516 fsqpmlv,

 

                f9_4516 ffxcs,

 

                Case

 

                  When F10_4516 = '4' Then  '1'

 

                  When F10_4516 = '0' Then  '2'

 

                  When F10_4516 = '1' Then  '3'

 

                  When F10_4516 = '2' Then  '4'

 

                  When F10_4516 = '3' Then  '5'

 

                  Else  Null  End As ffxfs,

 

                (Select Min(f8_1270)  From wind.tb_object_1270  Where F1_1270 = F43_4516) fscfxr,

 

                Case f13_4516

 

                  When '上交所' Then 'H'

 

                  When '深交所' Then 'S'

 

                  When '银行间' Then 'Y'

 

                  Else Null End As fssdd,

 

                f3_4516 fjxqsr,

 

                f4_4516 jxjzr,

 

                F26_4516 * 100000000 fxgm,

 

                Case

 

                  When f47_1429 Is Not Null Then  '1'

 

                  Else '0' End As jhht,

 

                Case

 

                  When F48_1429 = '不可撤销连带责任担保' Then '1'

 

                End As fldzrdb,

 

                f25_4516 ffxgsdm,

 

                f24_4516 fgsjc,

 

                Case

 

                  When F17_4516 = '321' Then '1'

 

                  When F17_4516 = '333' Then '3'

 

                  When F17_4516 = '322' And F29_1429 Is Null Then '2'

 

                  When F17_4516 = '322' And F29_1429 = '公司债' Then '11'

 

                  When F17_4516 = '322' And F29_1429 = '中期票据' Then '13'

 

                  When F17_4516 = '323' And F29_1429 <> '可分离转债' Then '4'

 

                  When F17_4516 = '323' And F29_1429 = '可分离转债' Then '12'

 

                  When F17_4516 = '310' And F29_1429 Is Null Then '6'

 

                  When F17_4516 = '310' And F29_1429 Is Not Null Then '14'

 

                  When F17_4516 In ('331', '340') Then '7'

 

                  When F17_4516 = '341' Then '8'

 

                  When F17_4516 = '342' Then '9'

 

                  Else '0 '

 

                End As zqlx,

 

                f20_4516 fxrq,

 

                Case

 

                  When F29_1429 = '可转债' Then

 

                   '中国证券监督管理委员会'

 

                  When F4_1090 = 'Q' And F29_1429 Not In ('中期票据', '短时间融资券') Then

 

                   '发展和改革委员会'

 

                  Else 'NULL'

 

                End As shzt,

 

                Case

 

                  When (F32_1429 Like '%铁路%' And F47_1429 = '2002638') Or

 

                       (F32_1429 Like '%三峡%' And F47_1429 = '2002650') Then  '1'

 

                  Else   '0'  End As Jjbz,

 

                nvl((Select t.ob_object_name_1020 From Wind.tb_object_1020 t

 

                Where f27_1429=t.ob_object_id),'NULL') CxsMc,

 

                f15_4516 cxqsr,

 

                f16_4516 cxjzr,

 

                Null bjyy,

 

                Case

 

                  When f34_1429 = '贴现' Then   '贴现债券'

 

                  When f11_1429 = '固定利率' Then '固定利率'

 

                  When f11_1429 = '累进利率' Then '浮动利率-固定利率'

 

                  When f11_1429 = '浮动利率' Then '浮动利率-市场利率'

 

                  Else  Null End As lvlx,

 

                Case

 

                  When f48_1429 = '不可撤销连带责任担保' Then  1 Else  0 End As syyhdb,

 

                NVL((SELECT f4_1703 - f39_1429

 

                   FROM wind.tb_object_1703

 

                  Where f1_1703 = f1_1429

 

                    And f3_1703 = (SELECT Min(f3_1703)

 

                                     FROM wind.tb_object_1703

 

                                    Where f1_1703 = f1_1429)),0) flvjz,

 

                NVL(f15_1639,0) fjcjq,

 

                NVL(f16_1639,0) flljq,

 

                Case

 

                  When f47_1429 Is Not Null Then  f47_1429 Else 'N' End As fdbrdm,

 

                Case

 

                  When f34_1018 = F47_1429 Then ob_object_name_1018 Else 'N' End As fdbr,

 

                Case

 

                  When f34_1018 = F47_1429 And F40_1018 = 2 Then '1' Else  '2'  End As Fdbrxz,

 

                Case

 

                  When f34_1018 = F47_1429 And F40_1018 = '2' And

 

                       F41_1018 = 49 Then  '105'

 

                  When f34_1018 = F47_1429 And F40_1018 <> '2' And

 

                       F41_1018 = 49 Then  '205'

 

                  When f34_1018 = F47_1429 And F40_1018 = '2' And

 

                       F41_1018 <> 49 Then  '1'

 

                  When f34_1018 = F47_1429 And F40_1018 <> '2' And

 

                       F41_1018 <> 49 Then '2'

 

                  When f34_1018 = F47_1429 And F41_1018 = '25' Then '3'

 

                  When f40_1429='2000850' Then '2'