Queries with different execution plan

  • Hello.

    This is a curious problem:

    This query is very fast, however the next query:

    http://img709.imageshack.us/img709/7060/query2.jpg%5B/img%5D

    is very slow (10 minutes to end!!!)

    The difference is only the author´s name: "A. Bracho" VS "A. Bout" but the execution plan is totally different.

    I am using SQL Server 2008 and I have removed the stopwords list from the fulltext engine (execution without stopwords)

    Do you know any reason? Any suggest? Thanks in advance.

  • It's very difficult to tell precisely what's occurring when only looking at images. There is a lot of information contained within an execution plan, not simply the graphics. In the future, it would be helpful to upload the .sqlplan files so that it's possible to explore the information within the plan itself.

    Just looking at it, changing the hard coded parameter resulted in a different execution plan. That second execution plan either wasn't optimal for the indexes, etc., or, it's estimated cost wasn't as high as the other plan so parallelism wasn't invoked. Something along those lines was what's involved. Basically, parameter sniffing.

    Your query, as currently construsted, with the hard coded values, is going to get a different execution plan each time that hard coded value is changed. You might want to explore a different construct that takes advantage of paramters which will lead to procedure reuse.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • Thank you for the answer.

    The query is really in a stored procedured:

    ALTER PROCEDURE [dbo].[RetrieveBibliography]

    (

    @clause nvarchar(max)

    )

    AS

    BEGIN

    DECLARE @query AS nvarchar(max)

    SET @query = 'SELECT TOP 100 Id FROM Bibliography WHERE '

    + @clause + 'ORDER BY [Year] DESC'

    EXEC sp_executesql @Query

    END

    I attach the sqlplan for both queries.

    Is possible to force a execute plan for a stored procedure?

    Thanks.

  • You can try using query hints, but the fundamental problem is the approach you're taking with the query. Ad hoc or dynamic SQL like this will create a different execution plan every time you change the values, even if you get a good plan each time, you'll still get a new plan each time. You need to change the query so that it uses parameters appropriately, not building and executing strings.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • The estimated row counts are very different , are your statistics up to date ?

    Check this page in the section chapter "Increasing Full-Text Query Performance" for some good info

    http://technet.microsoft.com/en-us/library/cc721269.aspx

    Besides which, you have left yourself wide open to a sql-injection attack.



    Clear Sky SQL
    My Blog[/url]

  • Thank you very much for your answer Grant Fritchey and Dave Ballantyne.

    I have tried several alternatives and none has worked. :crying: but thank you

    for your help.

    PS: Merry Christmas and Happy New Year!

Viewing 6 posts - 1 through 5 (of 5 total)

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