Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Queries with different execution plan Expand / Collapse
Author
Message
Posted Wednesday, December 16, 2009 4:43 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, February 2, 2010 3:13 AM
Points: 3, Visits: 17
Hello.

This is a curious problem:



This query is very fast, however the next query:



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.
Post #834977
Posted Wednesday, December 16, 2009 8:04 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Yesterday @ 7:32 PM
Points: 15,517, Visits: 27,895
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
The Scary DBA
Author of: SQL Server 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #835109
Posted Thursday, December 17, 2009 5:38 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, February 2, 2010 3:13 AM
Points: 3, Visits: 17
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.



  Post Attachments 
Query1.sqlplan (9 views, 31.76 KB)
Query2.sqlplan (4 views, 23.42 KB)
Post #835608
Posted Thursday, December 17, 2009 5:46 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Yesterday @ 7:32 PM
Points: 15,517, Visits: 27,895
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
The Scary DBA
Author of: SQL Server 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #835611
Posted Thursday, December 17, 2009 6:09 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, July 15, 2014 7:03 AM
Points: 1,949, Visits: 8,303
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
Kent user group
Post #835617
Posted Tuesday, December 29, 2009 4:42 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, February 2, 2010 3:13 AM
Points: 3, Visits: 17
Thank you very much for your answer Grant Fritchey and Dave Ballantyne.

I have tried several alternatives and none has worked. but thank you
for your help.

PS: Merry Christmas and Happy New Year!
Post #839768
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse