Strange query plan behaviour

  • I have a stored procedure with a number of parameters - the important one is @in_forename Nvarchar(35)

    In the stored procedure the code is

    IF (@in_forename IS NULL)

    SET @in_forename = '%%';

    ELSE

    if (@in_forename = '')

    SET @in_forename = '%%';

    ELSE

    SET @in_forename = @in_forename + '%';

    it then uses the @in_forename in the query

    if @in_forename is '%' the query returns 2+ million rows in 1 minutes 6 seconds, If @in_forename is null it returns in 6 mins + and if @in_forename is '' it takes 20 minutes.

    For each run it uses a different query plan and only for '%' is there are degree of parallelism.

    Any suggestions?

  • Can you post the stored procedure and the execution plan please?

    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
  • Attached

  • This: http://sqlinthewild.co.za/index.php/2008/05/22/parameter-sniffing-pt-3/

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

    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
  • Tried to fix as parameter sniffing but still getting the problem

  • Please post the revised procedure and exec plan.

    Main point, make sure you are not changing parameter values in the procedure then using those parameter values in queries

    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
  • Updated proc and plan attached. Using parameter sniffing is now causing the test with forename ='%' to not use a parallel query.

  • i have altered the procedure so that it builds the sql query at run time and executes it dynamically. The query now executes as a parallel query.

Viewing 8 posts - 1 through 7 (of 7 total)

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