Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2008
»
SQL Server 2008 - General
»
Queries with different execution plan
Queries with different execution plan
Rate Topic
Display Mode
Topic Options
Author
Message
alvaromartin82
alvaromartin82
Posted Wednesday, December 16, 2009 4:43 AM
Forum Newbie
Group: General Forum Members
Last Login: Tuesday, February 02, 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
Grant Fritchey
Grant Fritchey
Posted Wednesday, December 16, 2009 8:04 AM
SSChampion
Group: General Forum Members
Last Login: Today @ 3:41 AM
Points: 13,383,
Visits: 25,189
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
alvaromartin82
alvaromartin82
Posted Thursday, December 17, 2009 5:38 AM
Forum Newbie
Group: General Forum Members
Last Login: Tuesday, February 02, 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
(
7 views,
31.76 KB
)
Query2.sqlplan
(
3 views,
23.42 KB
)
Post #835608
Grant Fritchey
Grant Fritchey
Posted Thursday, December 17, 2009 5:46 AM
SSChampion
Group: General Forum Members
Last Login: Today @ 3:41 AM
Points: 13,383,
Visits: 25,189
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
Dave Ballantyne
Dave Ballantyne
Posted Thursday, December 17, 2009 6:09 AM
SSCommitted
Group: General Forum Members
Last Login: Today @ 3:42 AM
Points: 1,943,
Visits: 8,228
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
alvaromartin82
alvaromartin82
Posted Tuesday, December 29, 2009 4:42 AM
Forum Newbie
Group: General Forum Members
Last Login: Tuesday, February 02, 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 »
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.