SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Queries with different execution plan


Queries with different execution plan

Author
Message
alvaromartin82
alvaromartin82
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 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.
Grant Fritchey
Grant Fritchey
SSC Guru
SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)

Group: General Forum Members
Points: 73301 Visits: 32913
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 Query Performance Tuning and SQL Server Execution Plans
Product Evangelist for Red Gate Software
alvaromartin82
alvaromartin82
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 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.
Attachments
Query1.sqlplan (25 views, 31.00 KB)
Query2.sqlplan (21 views, 23.00 KB)
Grant Fritchey
Grant Fritchey
SSC Guru
SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)

Group: General Forum Members
Points: 73301 Visits: 32913
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 Query Performance Tuning and SQL Server Execution Plans
Product Evangelist for Red Gate Software
Dave Ballantyne
Dave Ballantyne
SSCarpal Tunnel
SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)

Group: General Forum Members
Points: 4922 Visits: 8370
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
alvaromartin82
alvaromartin82
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 17
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!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search