• ananda.murugesan (9/19/2012)


    Hi,

    Is it possible to change alternative way to modified this sql statement. its tooks 2.47 min for fetching 151 records,

    ...

    There are several. Putting all those many many correlated subqueries into CROSS APPLY constructs is a good place to start;

    SELECT

    b.JOBCODE,

    b.COMPLEXCODE,

    b.UNITCODE,

    b.GA_Drg_NO,

    ISNULL(SUM(cast(b.item_wt as decimal(18,3))),0) as ItemWt,

    WR_Scope_GAD_Location.[fabscope_Site],

    WR_Scope_GAD_Location.[fabscope_Shop],

    Redundant_qty_Locationwise.*,

    Refabrication_qty_Locationwise.*,

    ModificationQty.*,

    Shop_Fab_Shop.*,

    Shop_Fab_Site.*,

    Shop_Paint_Details_Shop.*,

    Shop_Paint_Details_Site.*

    FROM WR_Scope_GAD_Location_DynamicQuery as b

    CROSS APPLY (

    SELECT

    [fabscope_Site] = SUM(CASE WHEN c.location='Site' THEN c.item_wt ELSE 0 END),

    [fabscope_Shop] = SUM(CASE WHEN c.location='Shop' THEN c.item_wt ELSE 0 END)

    FROM dbo.WR_Scope_GAD_Location_DynamicQuery as c

    WHERE c.GA_Drg_NO = b.GA_Drg_NO

    and c.JOBCODE=b.JOBCODE

    and c.COMPLEXCODE=b.COMPLEXCODE

    and c.UNITCODE=b.UNITCODE

    ) WR_Scope_GAD_Location

    CROSS APPLY (

    SELECT

    [Redqty_Site] = SUM(CASE WHEN c.location='Site' THEN c.itemwt ELSE 0 END),

    [Redqty_Shop] = SUM(CASE WHEN c.location='Shop' THEN c.itemwt ELSE 0 END)

    FROM dbo.Redundant_qty_Locationwise_DynamicQuery as c

    WHERE c.GA_Drg_NO = b.GA_Drg_NO

    and c.JOBCODE=b.JOBCODE

    and c.COMPLEXCODE=b.COMPLEXCODE

    and c.UNITCODE=b.UNITCODE

    ) Redundant_qty_Locationwise

    CROSS APPLY(

    SELECT

    [Rfabqty_Site] = SUM(CASE WHEN d.location='Site' THEN d.itemwt ELSE 0 END),

    [Rfabqty_Shop] = SUM(CASE WHEN d.location='Shop' THEN d.itemwt ELSE 0 END)

    FROM dbo.Refabrication_qty_Locationwise_DynamicQuery as d

    WHERE d.GA_Drg_NO = b.GA_Drg_NO

    and d.JOBCODE=b.JOBCODE

    and d.COMPLEXCODE=b.COMPLEXCODE

    and d.UNITCODE=b.UNITCODE

    ) Refabrication_qty_Locationwise

    CROSS APPLY (

    SELECT

    [Modqty_Site] = SUM(CASE WHEN e.location='Site' THEN e.item_wt ELSE 0 END),

    [Modqty_Shop] = SUM(CASE WHEN e.location='Shop' THEN e.item_wt ELSE 0 END)

    FROM dbo.ModificationQty_DynamicQuery as e

    WHERE e.GAD_NO = b.GA_Drg_NO

    and e.JOBCODE=b.JOBCODE

    and e.COMPLEXCODE=b.COMPLEXCODE

    and e.UNITCODE=b.UNITCODE

    ) ModificationQty

    CROSS APPLY (

    SELECT

    [shop_Dispatch_Front] = SUM(f.[shop Dispatch Front]),

    [shop_Dispatch_Wt] = SUM(f.[shop Dispatch Wt]),

    [shop_Dispatch_Balance] = SUM(f.[shop Dispatch Balance]),

    [shop_DPT_Wt] = SUM(f.[shop DPT Wt]),

    [shop_Laydown_Wt] = SUM(f.[shop Laydown Wt]),

    [shop_MPT_Wt] = SUM(f.[shop MPT Wt]),

    [shop_NDE_Completed] = SUM(f.[shop NDE Completed]),

    [shop_RT_Wt] = SUM(f.[shop RT Wt]),

    [shop_UT_Wt] = SUM(f.[shop UT Wt])

    FROM dbo.Shop_Fab_DynamicQuery as f

    WHERE f.GA_Drg_NO = b.GA_Drg_NO

    and f.JOBCODE=b.JOBCODE

    and f.COMPLEXCODE=b.COMPLEXCODE

    and f.UNITCODE=b.UNITCODE

    AND f.location = 'Shop'

    ) Shop_Fab_Shop

    CROSS APPLY (

    SELECT

    [site_Dispatch_Front] = SUM(f.[shop Dispatch Front]),

    [site_Dispatch_Wt] = SUM(f.[shop Dispatch Wt]),

    [site_Dispatch_Balance] = SUM(f.[shop Dispatch Balance]),

    [site_DPT_Wt] = SUM(f.[shop DPT Wt]),

    [site_Laydown_Wt] = SUM(f.[shop Laydown Wt]),

    [site_MPT_Wt] = SUM(f.[shop MPT Wt]),

    [site_NDE_Completed] = SUM(f.[shop NDE Completed]),

    [site_RT_Wt] = SUM(f.[shop RT Wt]),

    [site_UT_Wt] = SUM(f.[shop UT Wt])

    FROM dbo.Shop_Fab_DynamicQuery as f

    WHERE f.GA_Drg_NO = b.GA_Drg_NO

    and f.JOBCODE=b.JOBCODE

    and f.COMPLEXCODE=b.COMPLEXCODE

    and f.UNITCODE=b.UNITCODE

    AND f.location = 'Site'

    ) Shop_Fab_Site

    CROSS APPLY (

    SELECT

    [shop_SPP_Scope] = SUM(g.[shop SP&P Scope]),

    [shop_SPP_Front] = SUM(g.[shop SP&P Front]),

    [shop_SPP_Done] = SUM(g.[shop SP&P Done]),

    [shop_SPP_Balance] = SUM(g.[shop SP&P Balance]),

    [shop_Intermediate_Wt] = SUM(g.[shop Intermediate Wt]),

    [shop_Final_Coat_Wt] = SUM(g.[shop Final Coat Wt])

    FROM dbo.Shop_Paint_Details_DynamicQuery as g

    WHERE g.drawing_no = b.GA_Drg_NO

    and g.JOBCODE=b.JOBCODE

    and g.COMPLEXCODE=b.COMPLEXCODE

    and g.UNITCODE=b.UNITCODE

    AND g.location='Shop'

    ) Shop_Paint_Details_Shop

    CROSS APPLY (

    SELECT

    [site_SPP_Scope] = SUM(CASE WHEN g.location='Site' THEN g.[shop SP&P Scope] ELSE 0 END),

    [site_SPP_Front] = SUM(CASE WHEN g.location='Site' THEN g.[shop SP&P Front] ELSE 0 END),

    [site_SPP_Done] = SUM(CASE WHEN g.location='Site' THEN g.[shop SP&P Done] ELSE 0 END),

    [site_SPP_Balance] = SUM(CASE WHEN g.location='Site' THEN g.[shop SP&P Balance] ELSE 0 END),

    [site_Intermediate_Wt] = SUM(CASE WHEN g.location='Site' THEN g.[shop Intermediate Wt] ELSE 0 END),

    [site_Final_Coat_Wt] = SUM(CASE WHEN g.location='Site' THEN g.[shop Final Coat Wt] ELSE 0 END)

    FROM dbo.Shop_Paint_Details_DynamicQuery as g

    WHERE g.drawing_no = b.GA_Drg_NO

    and g.JOBCODE=b.JOBCODE

    and g.COMPLEXCODE=b.COMPLEXCODE

    and g.UNITCODE=b.UNITCODE

    AND g.location='Site'

    ) Shop_Paint_Details_Site

    GROUP BY b.JOBCODE, b.COMPLEXCODE, b.UNITCODE, b.GA_Drg_NO

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden