views:

15

answers:

0

I have a really weird problem that baffled even seasoned DBA's here where I work. I'm trying to troubleshoot performance problems with this legacy app that was originally developed with VB/SQL 2000. Most clients using this app with SQL 2000 don't have any performance issues. Some clients have SQL 2005 and have really bad performance executing certain Stored Procedures, even with compatibility level set to SQL 2000 (80).

Somebody recently made a interesting discovery. One of the stored procedures that performs specially bad, contains some queries within IF-ELSE-END the are never touched, only one query is ever executed. We reviewed the code and used SQL profiler to capture the actual execution. Those queries that are never executed contains the FOR XML RAW clause. One of those queries has not WHERE clause. The only SELECT statement that is actually executing DOES NOT USES "FOR XML RAW"

It seems that those queries are being executed somehow (although not output is generated from those). When the "FOR XML RAW" are commented from the SP, the execution time goes from 10-15 seconds to less that a second and still returning the expected results.

Although the answer may seem obvious (remove XML RAW where is not actually used), the problem is that this app has hundreds of SP's and going through all of them will take time. I was wondering if there is a setting or something that is making SQ 2005 behave this way.

Thanks in advance for any assistance.

Here is a "sample" (not the actual one) Stored Procedure (names has been changed to protect the innocent):

ALTER proc [dbo].[SP_TEST] ( @param1 varchar(6)='', @param2 varchar(5)='', @param3 varchar(6)='', @param4 varchar(10) = '' ) as begin

if @param1 = '' and @param2 = ''
    SELECT column1, column2, column3, column4, column5, column6, column7
    FROM    VW_TEST
    ORDER BY column8
    for xml RAW
else if @param1 = ''
    SELECT  column1, column2, column3, column4, column5, column6, column7
    FROM    VW_TEST
    WHERE   column3 = @param1
    for xml RAW
else if @param2 = ''
    SELECT column1, column2, column3, column4, column5, column6, column7        
    FROM    VW_TEST
    WHERE   column4 = @param2
    for xml RAW
else
    if @param3 = ''  
        SELECT column1, column2, column3, column4, column5, column6, column7
        FROM    VW_TEST
        WHERE   column3 = @param1
        AND column4 = @param2 ORDER BY column2
        for xml RAW
    else 
        if not exists (select 1 from table1supervising where supervisingprocode = @param3)
        begin   
            SELECT column1, column2, column3, column4, column5, column6, column7
            FROM  VW_TEST VFSP
            WHERE column3 = @param1 AND column4 = @param2 AND column9 = @param3         
        end
        else
        begin
            Select column1, column2, column3, column4, column5, column6, column7
            FROM  VW_TEST VFSP
            WHERE column3 = @param1 AND column4 = @param2 AND 
            (column9 = @param3 or column9 in (select column9 from #assist where  column10 = VFSP.column10))
        end             

end