Query Optimization help

  • I am trying to optimize one query which runs in SSIS package.

    I ran db tuning advisor as well, it recommended 2 non clustered indexes and 7 create statistics for that query.

    Issue: it runs sometime fine and gives results in 1.25 mins.

    sometimes it runs for hours and hours. else I will kill the process and again re-run.

    I see when db log backup or differential runs, it goes to suspended status. at that time we kill the process and wait until the job

    completes, and rerun.

    I also verified that this query was created 3 yrs ago and I am not much that familiar with tuning queries.

    I see the CPU utilization increases high and DiskIO also increases high when this SSIS package runs.

    Can anyone desperately help me in tuning the query.

    Please find the attached.

    Cheers,
    - Win
    "Dont Judge a Book by its Cover"

  • If you don't have much experience of tuning queries, then I would advise you to seek advice from someone does. You'll need to pay for that if you don't have anyone in your organisation. If you post the Actual execution plan, we can take a look and point out anything that's obvious, but we can't see your table definitions or data and therefore it's difficult for us to offer anything other than general suggestions.

    John

  • Thanks for your reply.

    I tuned it to better than earlier, since earlier query was bit critical with joins.

    But I am not that much proficient to minimize the query and tune it.

    I created the recommended Index, but now its in Index scan.

    Please find the attached execution plan and suggest me.

    Cheers,
    - Win
    "Dont Judge a Book by its Cover"

  • As John mentioned, we need the Actual execution plan and not the estimated plan. I also suggest not using NOLOCK everywhere.

    The SQL Guy @ blogspot[/url]

    @SeanPearceSQL

    About Me[/url]

  • Thanks for your response.

    As per the logic we need to fetch full data from tables.

    I modified the script using temp tables in it is working as expected, normal run as of now.

    Cheers,
    - Win
    "Dont Judge a Book by its Cover"

  • Please find the actual execution plan.

    Please let me know for any recommendations and modifications in the query as posted earlier.

    The logic is, we need to pull complete data from a table XXIST, where it fetches 70 billions of records every run (scheduled).

    Awaiting for responses.

    Have great times..

    Cheers,
    - Win
    "Dont Judge a Book by its Cover"

  • Any help is appreciated.

    Cheers,
    - Win
    "Dont Judge a Book by its Cover"

  • There are a number of performance issues here. I would quote a client 2 days minimum to optimize this one. However what did jump out at me was the excessive lookups from [DB_Staging].[dbo].[EXCPTRNSPF] due to a poor estimation. Make sure stats are up to date on [EXCPTRNSPF].

    You might see decent improvements if you can make [idx_EXCPTRNSPF_i_nc_02] covering by including LOBCODE.

    The SQL Guy @ blogspot[/url]

    @SeanPearceSQL

    About Me[/url]

  • [JOKE]

    What's in tbl_gods_lineOfBusiness. Does the query only run on Sunday's?

    [/JOKE]

    The SQL Guy @ blogspot[/url]

    @SeanPearceSQL

    About Me[/url]

  • One other thing is you might want to read up on NOLOCK. You have it littered all over this query. That hint is not a magic go fast solution. It is fraught with a number of very real and possible negative impacts. It can and will return duplicate and/or missing rows which can be nearly impossible to recreate on demand. Please read up and understand what that hint does.

    http://sqlblogcasts.com/blogs/tonyrogerson/archive/2006/11/10/1280.aspx

    http://blogs.msdn.com/b/davidlean/archive/2009/04/06/sql-server-nolock-hint-other-poor-ideas.aspx

    http://www.jasonstrate.com/2012/06/the-side-effect-of-nolock/[/url]

    _______________________________________________________________

    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/

  • Thanks for your replies...

    I will consider all of the posts and replies..

    Cheers,
    - Win
    "Dont Judge a Book by its Cover"

  • I am Sorry for delayed reply...

    I was away these many days.

    Thanks for your help and options for better performance.

    Yes, the job runs on Saturday & Sunday.

    Cheers,
    - Win
    "Dont Judge a Book by its Cover"

  • Not knowing what changes you made to your code I made a simple change, I moved your CTE pdtmstr into a select into statement to populate a temporary table called #pdtmstr. I then replaced the reference to the CTE with this temporary table.

  • Great help...!

    I am planning to test the changes and code in DEV environment tomorrow.

    I made this query into a Procedure and it is running fine as of now.

    I will use your changes as well.

    Thanks much.. I will keep you updated. I will be back again on this..

    Cheers,
    - Win
    "Dont Judge a Book by its Cover"

  • - Win (10/11/2013)


    Thanks for your replies...

    I will consider all of the posts and replies..

    I can only hope that's true.

    Tell your company that it's no longer lawful to embed SSN's in Policy Numbers and that storing unencrypted SSN's in their databases can get them shut down.

    Sheesh... and people bitch about the NSA violating privacy.

    --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 15 (of 16 total)

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