T-SQL Performance tuning on Aggregates

  • The below query resulted after 30minutes. It uses 2 uses and SUM().

    Attached the execution plan too. Index was created as below. Please help me to tune the performance.

    CREATE TABLE #tbl_ChannelViewCost

    (

    rcc_idNUMERIC(10,0),

    activity_dataset_id NUMERIC(10,0),

    profit_centre_idNUMERIC(10,0),

    channel_dataset_id NUMERIC(10,0),

    customer_dataset_id NUMERIC(10,0),

    product_category_dataset_id NUMERIC(10,0),

    channel_cost NUMERIC(22,8),

    driver_idNUMERIC(5,0),

    )

    CREATE TABLE #tblRCCCommanMOGEValues

    (

    rcc_id NUMERIC(10,0),

    common_cost_rcc_id NUMERIC(10,0),

    budget_cost NUMERIC(22,8) DEFAULT 0,

    oge_cost NUMERIC(22,8) DEFAULT 0,

    globe_cost NUMERIC(22,8) DEFAULT 0,

    mge_cost NUMERIC(22,8) DEFAULT 0,

    total_cost NUMERIC(22,8) DEFAULT 0,

    budget_percentage NUMERIC(22,12) DEFAULT 0,

    oge_percentage NUMERIC(22,12) DEFAULT 0,

    globe_percentage NUMERIC(22,12) DEFAULT 0,

    mge_percentage NUMERIC(22,12) DEFAULT 0,

    cc_group TINYINT

    )

    CREATE TABLE #tblChannelCost

    (

    profit_centre_id NUMERIC(10,0),

    channel_dataset_id NUMERIC(10,0),

    customer_dataset_id NUMERIC(10,0),

    product_category_dataset_id NUMERIC(10,0),

    activity_dataset_id NUMERIC(10,0),

    rcc_id NUMERIC(10,0),

    driver_id NUMERIC(5,0),

    channel_cost_budg NUMERIC(22,8),

    channel_cost_oge NUMERIC(22,8),

    channel_cost_globe NUMERIC(22,8),

    cc_group TINYINT,

    common_cost_rcc_id NUMERIC(10,0)

    )

    #tbl_ChannelViewCost has 3559048 rows.

    #tblRCCCommanMOGEValues has 16576 rows. It has pair of rows for the same rcc_id one with 0 in cc_group column and another row has 1 in cc_group column.

    Result is stored in #tblChannelCost and has 3410915 rows.

    Also, attached the Execution Plan as zip file. Refer the "Query 19" which is related to the query.

    CREATE NONCLUSTERED INDEX

    idx_tbl_ChannelViewCost_id ON #tbl_ChannelViewCost(profit_centre_id,channel_dataset_id,customer_dataset_id,product_category_dataset_id,activity_dataset_id,rcc_id,driver_id,channel_cost)

    CREATE NONCLUSTERED INDEX

    idx_tblRCCCommanMOGEValues_rcc_id ON #tblRCCCommanMOGEValues(rcc_id)

    Insert into #tblChannelCost

    SELECT

    c.profit_centre_id,

    c.channel_dataset_id,

    c.customer_dataset_id,

    c.product_category_dataset_id,

    c.activity_dataset_id,

    c.rcc_id,

    c.driver_id,

    SUM(c.channel_cost*(r.budget_percentage)) AS channel_cost_budg,

    SUM(c.channel_cost*(r.oge_percentage)) AS channel_cost_oge,

    SUM(c.channel_cost*(r.globe_percentage)) AS channel_cost_globe,

    r.cc_group,

    r.common_cost_rcc_id

    FROM

    #tbl_ChannelViewCost c

    INNER JOIN #tblRCCCommanMOGEValues r

    ON (

    r.rcc_id = c.rcc_id

    )

    GROUP BY

    c.profit_centre_id,

    c.channel_dataset_id,

    c.customer_dataset_id,

    c.product_category_dataset_id,

    c.activity_dataset_id,

    c.rcc_id,

    c.driver_id,

    r.cc_group,

    r.common_cost_rcc_id

  • I think you forget to attach the execution plan....

    _______________________________________________________________
    To get quick answer follow this link:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • Sorry, I don't know why it's not attaching!

    I clicked on "Insert Image" icon and typed the local file path and it inserted the html text here. But image is not displaying!

    Please provide me the email id so that I can send you.

    or Please suggest me few tuning steps for the query without the execution plan!

  • d_uvarajan (11/8/2013)


    Sorry, I don't know why it's not attaching!

    I clicked on "Insert Image" icon and typed the local file path and it inserted the html text here. But image is not displaying!

    Please provide me the email id so that I can send you.

    or Please suggest me few tuning steps for the query without the execution plan!

    Don't send a screen shot of the execution plan. That is pretty much useless. Attach the actual file. Gail explained it in your other thread. Right click -> Save Execution Plan As...

    Make sure it is the ACTUAL execution plan, not the estimated one.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Attached the execution plan. Please refer "Query 54" in the plan.

    If you could not ableto view it, please let me know the steps to upload the same. (I clicked the image icon and provided the the local file path)

  • d_uvarajan (11/9/2013)


    Attached the execution plan. Please refer "Query 54" in the plan.

    If you could not ableto view it, please let me know the steps to upload the same. (I clicked the image icon and provided the the local file path)

    An image of the execution plan will do us no good. Please see the 2nd link under "Helpful Links" in my signature line below on how to post a performance problem to get the best help.

    Also, is the rcc_id a UNIQUE column in the #tblRCCCommanMOGEValues table?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Thank you for providing me the link.

    I have updated the post with more detail with respect to Tables definition, execution plan etc., The RCC_Id in #tblRCCCommanMOGEValues is not Unique and there will 2 rows for each Rcc_id in the table #tblRCCCommanMOGEValues. One row will have 0 for its cc_group column and another will have 1 for the cc_group column.

    Please let me know if you require any more details.

  • d_uvarajan (11/9/2013)


    Attached the execution plan. Please refer "Query 54" in the plan.

    Query 54 is 0% of the cost of the batch. Why do you think it's the problem?

    Can you go through that procedure (as described in the article Jeff referenced), identify exactly which queries are the problems and upload the plans of just those queries. It will involve running them one by one.

    Also, we need an actual plan (as the article explains), not an estimated plan. You attached an estimated plan.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Thanks. As requested attached the "Actual Execution Plan". Now it shows Query cost 75%.

    Refer the "Query 19" which is related to the query.

  • d_uvarajan (11/11/2013)


    As requested attached the "Actual Execution Plan".

    Attached where?

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Can I suggest you start with the obvious and chose appropriate data types for your columns?

    CREATE TABLE #tbl_ChannelViewCost

    (

    rcc_id NUMERIC(10,0),

    activity_dataset_id NUMERIC(10,0),

    profit_centre_id NUMERIC(10,0),

    channel_dataset_id NUMERIC(10,0),

    customer_dataset_id NUMERIC(10,0),

    product_category_dataset_id NUMERIC(10,0),

    channel_cost NUMERIC(22,8),

    driver_id NUMERIC(5,0),

    )

    Why are you using NUMERIC with 0 scale? That's 9 bytes for each one.

    6 columns x 9bytes that's 54 bytes + 5 for the smaller NUMERIC

    3559048 rows X 59 bytes = 231338120 bytes = 200MB

    Changing these columns to INTs will cut the size in half:

    3559048 rows X 28 = 99653344 bytes = 95MB

    It's a start....

    ---------------------------------------------------------

    It takes a minimal capacity for rational thought to see that the corporate 'free press' is a structurally irrational and biased, and extremely violent, system of elite propaganda.
    David Edwards - Media lens[/url]

    Society has varying and conflicting interests; what is called objectivity is the disguise of one of these interests - that of neutrality. But neutrality is a fiction in an unneutral world. There are victims, there are executioners, and there are bystanders... and the 'objectivity' of the bystander calls for inaction while other heads fall.
    Howard Zinn

  • I have attached in my original post. Please check it!

    For your quick reference let me attach it here again.

    Please refer the "Query 19" and it's cost is 75%.

  • I use Numeric(10,0) for all Ids in the temeperary tables because the same was defined earlier in their corresponding original tables.

  • d_uvarajan (11/11/2013)


    I use Numeric(10,0) for all Ids in the temeperary tables because the same was defined earlier in their corresponding original tables.

    Ya just gotta love tables developed by tools (human or otherwise). 😛

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 15 posts - 1 through 14 (of 14 total)

You must be logged in to reply to this topic. Login to reply