Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Parameter Sniffing


Parameter Sniffing

Author
Message
Louis Hillebrand
Louis Hillebrand
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1530 Visits: 3209
If I run this Query using the procedure-parameters (@From, @To) in the WHERE clause, the execution time is over 40 sec.
When I use the local parameters (@F_Loc and @T_Loc) execution time is less than 1 sec.
I have attached both execution plans.

I solved the issue by using a local variable, but is there another way to change this behavior?

The query is not the full code, but enough the represent the issue.
The table dbo.Quotation has about 100.000 rows, Quotation.Follow_up about 344..


CREATE PROCEDURE [Quotation].[Get_Quotes_LH] ( @From DateTime = null, @To DateTime = null )
AS
BEGIN

SELECT @From = COALESCE(@From, Min(QuotationDate)),
@To = COALESCE(@To , Max(QuotationDate))
FROM dbo.Quotation;

DECLARE @F_Loc DateTime = @From,
@T_Loc DateTime = @To;

DECLARE @CutOffDate DateTime = DATEADD(MONTH, -3, CURRENT_TIMESTAMP);

WITH QU_Memo(Quotation_Id, R, Next_Follow_Up) AS (
SELECT Quotation_Id,
ROW_NUMBER() OVER (PARTITION BY Quotation_Id ORDER BY Id DESC),
Next_Follow_Up
FROM Quotation.Follow_Up
)
SELECT QU.QuotationId,
COALESCE(QUIC.Next_Follow_Up, 0)
FROM dbo.Quotation QU LEFT OUTER JOIN
(SELECT * FROM QU_Memo WHERE R = 1) QUIC ON QU.QuotationId = QUIC.Quotation_Id
--WHERE (QU.QuotationDate > @From OR COALESCE(QUIC.Next_Follow_Up, 0) > 0 )
--AND QU.QuotationDate <= @To
WHERE (QU.QuotationDate > @F_Loc OR COALESCE(QUIC.Next_Follow_Up, 0) > 0 )
AND QU.QuotationDate <= @T_Loc
ORDER BY QU.QuotationId DESC;

END
GO
--#########################
EXEC [Quotation].[Get_Quotes_LH]


Attachments
Query_Using_From.sqlplan (16 views, 76.00 KB)
Query_Using_FLocal.sqlplan (8 views, 75.00 KB)
Eirikur Eiriksson
Eirikur Eiriksson
SSCertifiable
SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)

Group: General Forum Members
Points: 6812 Visits: 17757
Quick thought, have you tried using WITH RECOMPILE?
Cool

In addition, are the statistics up to date?
Louis Hillebrand
Louis Hillebrand
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1530 Visits: 3209
I tried WITH RECOMPILE, and Statistics are up to date.
The table [Follow_Up] was created recently, I deleted and recreated the table an indexes, but this did not change this behavior.
Eirikur Eiriksson
Eirikur Eiriksson
SSCertifiable
SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)

Group: General Forum Members
Points: 6812 Visits: 17757
Here is a quick suggestion, should result in more consistent plan (for better or worse)
Cool

CREATE PROCEDURE [Quotation].[Get_Quotes_LH]  ( @From DateTime = null, @To DateTime = null ) 
AS
BEGIN

SELECT @From = COALESCE(@From, Min(QuotationDate)),
@To = COALESCE(@To , Max(QuotationDate))
FROM dbo.Quotation;

DECLARE @F_Loc DateTime = @From,
@T_Loc DateTime = @To;

DECLARE @CutOffDate DateTime = DATEADD(MONTH, -3, CURRENT_TIMESTAMP);

WITH QU_Memo(Quotation_Id, R, Next_Follow_Up) AS (
SELECT Quotation_Id,
ROW_NUMBER() OVER (PARTITION BY Quotation_Id ORDER BY Id DESC),
Next_Follow_Up
FROM Quotation.Follow_Up
)
SELECT QU.QuotationId,
COALESCE(QUIC.Next_Follow_Up, 0)
FROM dbo.Quotation QU
OUTER APPLY QU_Memo QUIC

WHERE QUICK.R = 1
AND QU.QuotationId = QUIC.Quotation_Id
AND (QU.QuotationDate > @F_Loc OR COALESCE(QUIC.Next_Follow_Up, 0) > 0 )
AND QU.QuotationDate <= @T_Loc
ORDER BY QU.QuotationId DESC;

END
GO
--#########################
EXEC [Quotation].[Get_Quotes_LH]


ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 9027 Visits: 19036
Here are several suggestions -
--SELECT @From = COALESCE(@From, Min(QuotationDate)),
-- @To = COALESCE(@To , Max(QuotationDate))
--FROM dbo.Quotation;

--DECLARE @F_Loc DateTime = @From,
-- @T_Loc DateTime = @To;

-- DECLARE @CutOffDate DateTime = DATEADD(MONTH, -3, CURRENT_TIMESTAMP);

SELECT QU.QuotationId,
ISNULL(QUIC.Next_Follow_Up, 0)

FROM dbo.Quotation QU

OUTER APPLY (
SELECT TOP 1 Next_Follow_Up
FROM Quotation.Follow_Up f
WHERE f.QuotationId = QU.QuotationId
ORDER BY Id DESC
) QUIC

WHERE ((@From IS NULL OR QU.QuotationDate > @From) OR QUIC.Next_Follow_Up > 0)

AND (@To IS NULL OR QU.QuotationDate <= @To)

ORDER BY QU.QuotationId DESC;



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Louis Hillebrand
Louis Hillebrand
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1530 Visits: 3209
Thanks for the solutions,
I tried both versions, but they are slower than my version using a local variable.

It is strange that the execution plans are so very different between using local variables or parameters in the query..
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 9027 Visits: 19036
It would be interesting to see the Actual execution plans for them...

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Louis Hillebrand
Louis Hillebrand
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1530 Visits: 3209
I included the actual execution plans from my versions in my initial post and generate the EP from your versions when I'm back at work on monday.
Louis Hillebrand
Louis Hillebrand
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1530 Visits: 3209
Chris,

Here is the execution plan..
The OUTER APPLY does 100k seeks on the Follow-Up table.

Louis.
Attachments
ChrisM_Solution.sqlplan (7 views, 109.00 KB)
WorkasDBA
WorkasDBA
Valued Member
Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)

Group: General Forum Members
Points: 68 Visits: 122
Below are some of the options for parameter sniffing:
1. Local variables
2. WITH RECOMPILE
3. Rebuild index - it creates new plan
4. Trace Flag 4136 with QUERYTRACEON HINT
more options can be found at http://sqlperformance.com/2013/08/t-sql-queries/parameter-sniffing-embedding-and-the-recompile-options
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