Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Looong running query
Message
From
11/09/2008 15:42:40
 
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Environment versions
SQL Server:
SQL Server 2005
Miscellaneous
Thread ID:
01346813
Message ID:
01346824
Views:
7
>Hi All,
>
>Is there a way to tune this query somehow. It returns expected results but it takes over 6 hours to complete....
>Thank you,
>DAniel
>
>
>
>Select prtmst.prtnum as SKU, pitems.descr,
>		pitems.status, prtmst.prtfam as family,   
>	(select rtstr1 
>		from njsql.wmsnj.dbo.poldat poldat, njsql.wmsnj.dbo.locmst locmst 
>		where poldat.rtstr1 = locmst.stoloc		
>		and poldat.rtnum1 = 1 
>		and poldat.polcod = 'STORE-ASG-LOC' 
>		and poldat.polvar = 'prtnum' 
>		and replace(poldat.polval, '----|', '') = prtmst.prtnum) as LocAssigned, 
>	(select maxqvl 
>		from njsql.wmsnj.dbo.locmst 
>		where stoloc = (select rtstr1 
>							from njsql.wmsnj.dbo.poldat poldat, njsql.wmsnj.dbo.locmst locmst 
>							where poldat.rtstr1 = locmst.stoloc 
>							and poldat.rtnum1 = 1 
>							and poldat.polcod = 'STORE-ASG-LOC' 
>							and poldat.polvar = 'prtnum' 
>							and replace(poldat.polval, '----|', '') = prtmst.prtnum)) as LocCapacity, 
>	ftpmst.untlen as [length], ftpmst.untwid as width, ftpmst.unthgt as height, history.demcan4 as [4 Wks Sales], 
>	(select count (*) 
>		from njsql.wmsnj.dbo.dlytrn dlytrn 
>		where dlytrn.actcod = 'CASRPL' 
>		and dlytrn.prtnum = prtmst.prtnum) as [4 Wks Repln], 
>	history.demcan26 as [26 Wks Sales], 
>	(select count (*) 
>		from njsql.wmsnj_arch.dbo.dlytrn arch_dlytrn 
>		where arch_dlytrn.actcod = 'CASRPL' 
>		and arch_dlytrn.trndte > = getdate () - 182 
>		and arch_dlytrn.prtnum = prtmst.prtnum) as [26 Wks Repln], 
>	history.NJUseable as [NJ QOH], history.DueNJ as NJDue, 
>	history.NVUseable as [NV QOH], history.DueNV as NVDue 
>from njsql.wmsnj.dbo.prtmst prtmst, 
>	pitems, njsql.wmsnj.dbo.ftpmst ftpmst, history 
>where pitems.status <> 'N1'
>and prtmst.vc_edpnumber = pitems.edpno 
>and prtmst.prtnum = ftpmst.ftpcod 
>and prtmst.vc_edpnumber = history.edpno
>
>
Use JOINS, do not use INLINE Field SELECTs but use derived tables and join them.
First try to remove ALL selects from fields area (just comment them). try and see how fast it will come.
Something like:
Select prtmst.prtnum as SKU, pitems.descr,
        pitems.status, prtmst.prtfam as family  /* Comments begin,   
    (select rtstr1 
        from njsql.wmsnj.dbo.poldat poldat, njsql.wmsnj.dbo.locmst locmst 
        where poldat.rtstr1 = locmst.stoloc        
        and poldat.rtnum1 = 1 
        and poldat.polcod = 'STORE-ASG-LOC' 
        and poldat.polvar = 'prtnum' 
        and replace(poldat.polval, '----|', '') = prtmst.prtnum) as LocAssigned, 
    (select maxqvl 
        from njsql.wmsnj.dbo.locmst 
        where stoloc = (select rtstr1 
                            from njsql.wmsnj.dbo.poldat poldat, njsql.wmsnj.dbo.locmst locmst 
                            where poldat.rtstr1 = locmst.stoloc 
                            and poldat.rtnum1 = 1 
                            and poldat.polcod = 'STORE-ASG-LOC' 
                            and poldat.polvar = 'prtnum' 
                            and replace(poldat.polval, '----|', '') = prtmst.prtnum)) as LocCapacity, 
    ftpmst.untlen as [length], ftpmst.untwid as width, ftpmst.unthgt as height, history.demcan4 as [4 Wks Sales], 
    (select count (*) 
        from njsql.wmsnj.dbo.dlytrn dlytrn 
        where dlytrn.actcod = 'CASRPL' 
        and dlytrn.prtnum = prtmst.prtnum) as [4 Wks Repln], 
    history.demcan26 as [26 Wks Sales], 
    (select count (*) 
        from njsql.wmsnj_arch.dbo.dlytrn arch_dlytrn 
        where arch_dlytrn.actcod = 'CASRPL' 
        and arch_dlytrn.trndte > = getdate () - 182 
        and arch_dlytrn.prtnum = prtmst.prtnum) as [26 Wks Repln] Comments END*/, 
    history.NJUseable as [NJ QOH], history.DueNJ as NJDue, 
    history.NVUseable as [NV QOH], history.DueNV as NVDue 
from njsql.wmsnj.dbo.prtmst prtmst
INNER JOIN  pitems                        ON prtmst.vc_edpnumber = pitems.edpno 
INNER JOIN  njsql.wmsnj.dbo.ftpmst ftpmst ON prtmst.prtnum = ftpmst.ftpcod 
INNER JOIN  history                       ON prtmst.vc_edpnumber = history.edpno
where pitems.status <> 'N1'
If that runs faster then try to remove that INLINE SELECTS one by one untill you get the desired result.
If you didn't get better answer I'll check this again tomorrow.
Against Stupidity the Gods themselves Contend in Vain - Johann Christoph Friedrich von Schiller
The only thing normal about database guys is their tables.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform