SQL查询更多行返回更短

时间:2017-03-28 20:05:31

标签: sql sql-server sql-server-2008 optimization

我有一个查询报告:

select 
    hp.KOD, hp.AD, hp.HES_ID,
    sum(fs.BORC_TUTARI) B_T, 
    sum(fs.ALACAK_TUTARI) A_T,
    max(hp.UST_HESAP_ID) UST_HESAP_ID, 
    max(hp. ALT_HESAP_SAYISI) ALT_HESAP_SAYISI
from 
    W_HES_PLAN hp, FIS_SATIRLARI fs, FIS_BASLIGI FB, 
    (select h.HESAP_KODU HESKOD, FS.HESAP_ID, FS.FIS_ID
     from HESAP_PLANI H, FIS_SATIRLARI fs
     where H.HESAP_ID = FS.HESAP_ID       
     group by h.HESAP_KODU,FS.HESAP_ID, FS.FIS_ID) CC 
where 
    CC.HESKOD like HP.KOD+'%'
    and FB.FIS_ID = FS.FIS_ID
    and CC.HESAP_ID = fs.HESAP_ID 
    and CC.FIS_ID = FS.FIS_ID 
     --  and hp.KOD>='' and hp.KOD<='200'
group by 
    hp.KOD, hp.AD, hp.HES_ID
order by 
    HP.KOD

CC表的子查询在7秒内返回676.427行,但所有查询在23分钟内返回14.000行。我无法理解这一点。为什么更多行返回更短?任何优化建议?

1 个答案:

答案 0 :(得分:0)

我怀疑性能问题是因为你仍在使用ANSI-89样式的连接,它为你提供了一个你不想要的交叉连接。这是您的查询转换为ANSI-92样式连接。 Sinc eyou没有where断言来查看W_HES_PLAN和FIS_SATIRLARI您有交叉连接。我有一种不是你想要的感觉。它会导致一些巨大的性能问题,很可能会出现不正确的值。

select 
    hp.KOD
    , hp.AD
    , hp.HES_ID
    , sum(fs.BORC_TUTARI) as B_T
    , sum(fs.ALACAK_TUTARI) as A_T
    , max(hp.UST_HESAP_ID) as UST_HESAP_ID
    , max(hp. ALT_HESAP_SAYISI) as ALT_HESAP_SAYISI
from 
    W_HES_PLAN hp
    cross join FIS_SATIRLARI fs
    join FIS_BASLIGI FB on FB.FIS_ID = FS.FIS_ID
    join 
    (
        select h.HESAP_KODU HESKOD
            , FS.HESAP_ID
            , FS.FIS_ID
        from HESAP_PLANI H
        join FIS_SATIRLARI fs on H.HESAP_ID = FS.HESAP_ID       
        group by h.HESAP_KODU
            ,FS.HESAP_ID
            , FS.FIS_ID
    ) CC on CC.HESKOD like HP.KOD+'%'
        and CC.HESAP_ID = fs.HESAP_ID 
        and CC.FIS_ID = FS.FIS_ID 
     --  and hp.KOD>='' and hp.KOD<='200'
group by 
    hp.KOD
    , hp.AD
    , hp.HES_ID
order by 
    HP.KOD