Passing Parameter with a wildcard

  • Nevermind. Just reread your original post and that you are going against pre-2005 databases. I am assuming that you are querying SQL Server 2000 databases/servers.

  • Yes, that's correct. They are 2000. They will eventually be upgraded to 2008, but I am stuck working with this until that time.

  • Didn't look to see if it was mentioned before but one sure fire way to increase the performance here would be to use UNION ALL instead of UNION. UNION does its own DISTINCT. UNION ALL does not. Of course if there are dupes present that you can't tolerate, you won't be able to use UNION ALL.

    --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)

  • Jeff Moden (5/10/2012)


    Didn't look to see if it was mentioned before but one sure fire way to increase the performance here would be to use UNION ALL instead of UNION. UNION does its own DISTINCT. UNION ALL does not. Of course if there are dupes present that you can't tolerate, you won't be able to use UNION ALL.

    Even removing literally all of the unions and only passing that first sequence in results in the same rediculousness. But that was a good thought!

  • themangoagent (5/11/2012)


    Jeff Moden (5/10/2012)


    Didn't look to see if it was mentioned before but one sure fire way to increase the performance here would be to use UNION ALL instead of UNION. UNION does its own DISTINCT. UNION ALL does not. Of course if there are dupes present that you can't tolerate, you won't be able to use UNION ALL.

    Even removing literally all of the unions and only passing that first sequence in results in the same rediculousness. But that was a good thought!

    Did you try Drew's suggestion? Seemed like a pretty good approach so you don't hit the same table over and over.

    _______________________________________________________________

    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/

  • Can you get us at least the physical layout of the base table before you unpivot it? I'd like to look at another possibility. It would help if you could provide data types as well.

  • Sean Lange (5/11/2012)


    themangoagent (5/11/2012)


    Jeff Moden (5/10/2012)


    Didn't look to see if it was mentioned before but one sure fire way to increase the performance here would be to use UNION ALL instead of UNION. UNION does its own DISTINCT. UNION ALL does not. Of course if there are dupes present that you can't tolerate, you won't be able to use UNION ALL.

    Even removing literally all of the unions and only passing that first sequence in results in the same rediculousness. But that was a good thought!

    Did you try Drew's suggestion? Seemed like a pretty good approach so you don't hit the same table over and over.

    It appears that the OP is still on SQL 2000, so that approach won't work.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • Hey everyone, thanks for all of your thoughts and ideas. I stripped the code down to the very basic, hitting only 1 table once, but still keeping my parameter but changing my left joins to inner joins. It took 00:00:00 seconds. I slowly rebuilt it again, and instead of taking 30 minutes, I'm down to 22 seconds, and that's only after adding about 20 extra Unions. I'll still be working to make it quicker, but I'm definitely on the right track now. Thanks again!

  • drew.allen (5/10/2012)


    This problem is almost certainly due to parameter sniffing. Gail has a good article on Parameter Sniffing[/url]

    There are other issues with your code, though. You should use a WHERE clause instead of the HAVING clause that you currently have. You should use a DISTINCT clause intsead of the GROUP BY clause that you currently have, although even that indicates a potential problem with your data. You're scanning the table more times than is necessary. You should use a CROSS APPLY for your normalization. For example, this code:

    can be rewritten as

    SELECT m.Sub_ID AS SubNo, d.Diagnosis, m.Prov_Name, m.Claim_Ref_Num, CONVERT(DATETIME, m.Beg_Date_of_Serv) AS DOS

    FROM dbo.Medhdr_New_Format AS m

    CROSS APPLY (

    SELECT m.Primary_Diag_ICD

    UNION

    SELECT m.Second_Diag_ICD

    UNION

    SELECT m.Tertiary_Diag_ICD

    UNION

    SELECT m.Diag_4th

    UNION

    SELECT m.Diag_5th

    UNION

    SELECT m.Diag_6th

    ) AS d(Diagnosis)

    Instead of reading the medhdr_new_format table six times, it only needs to read it once.

    Drew

    Is Cross Apply something that can be used on SQL Server 2000 tables?

  • themangoagent (5/14/2012)


    Is Cross Apply something that can be used on SQL Server 2000 tables?

    No, it's SQL 2005 or greater.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • drew.allen (5/14/2012)


    themangoagent (5/14/2012)


    Is Cross Apply something that can be used on SQL Server 2000 tables?

    No, it's SQL 2005 or greater.

    Drew

    Sadness.

    At least it will come in handy after these tables are migrated. Thanks!

  • drew.allen (5/14/2012)


    Sean Lange (5/11/2012)


    themangoagent (5/11/2012)


    Jeff Moden (5/10/2012)


    Didn't look to see if it was mentioned before but one sure fire way to increase the performance here would be to use UNION ALL instead of UNION. UNION does its own DISTINCT. UNION ALL does not. Of course if there are dupes present that you can't tolerate, you won't be able to use UNION ALL.

    Even removing literally all of the unions and only passing that first sequence in results in the same rediculousness. But that was a good thought!

    Did you try Drew's suggestion? Seemed like a pretty good approach so you don't hit the same table over and over.

    It appears that the OP is still on SQL 2000, so that approach won't work.

    Drew

    Hmmm... it might if you used a correlated sub-query as the replacement for the Cross Apply (which is really just a correlated sub-query in many cases).

    --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)

  • Jeff Moden (5/25/2012)


    drew.allen (5/14/2012)


    Sean Lange (5/11/2012)


    themangoagent (5/11/2012)


    Jeff Moden (5/10/2012)


    Didn't look to see if it was mentioned before but one sure fire way to increase the performance here would be to use UNION ALL instead of UNION. UNION does its own DISTINCT. UNION ALL does not. Of course if there are dupes present that you can't tolerate, you won't be able to use UNION ALL.

    Even removing literally all of the unions and only passing that first sequence in results in the same rediculousness. But that was a good thought!

    Did you try Drew's suggestion? Seemed like a pretty good approach so you don't hit the same table over and over.

    It appears that the OP is still on SQL 2000, so that approach won't work.

    Drew

    Hmmm... it might if you used a correlated sub-query as the replacement for the Cross Apply (which is really just a correlated sub-query in many cases).

    The correlated subquery can only return a single value. Even though we are returning only one column, we need to return up to six rows per record. That means that the correlated subquery will not work for this problem.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

Viewing 13 posts - 16 through 27 (of 27 total)

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