SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Parameter query NOT using Index
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

DLTaylor
Posting Yak Master

United Kingdom
136 Posts

Posted - 02/04/2014 :  07:28:50  Show Profile  Reply with Quote
Hi All,

I am working on a sql 2005 Enterprise Edition DB using sp4.

I am seeing strange performance when using a parameter in part of my WHERE clause.
Looking at the Execution plan. The ‘parameter’ query appears NOT use a NON CLUSTERED INDEX on the RowUpdateTime. While the hardcoded value ‘02/02/2014’ does use the NCI when running the TSQL

I have laid out the example sample code to see if anyone can point to anything obvious.
Is it the way im setting my ‘date’ value?

Any help would be great.
As you can imagine the performance difference is huge!
Thanks


--============================================
--Sample TSQL - not
--============================================


----tsql e.g.1 with NO parameter (hardcoded)
--uses INDEX
SELECT *
FROM [LiveDB].[dbo].[LabSpecimenTests]
WHERE (SourceID = 'CCH')
AND ([RowUpdateDateTime] >= '02/02/2014')
AND (NOT [ResultDateTime] IS NULL)

--tsql e.g.2 with parameter
--Does NOT use index?

--declare variable
DECLARE @RefreshDate datetime
--set value
SET @RefreshDate = (SELECT CONVERT(DATETIME, '02/02/2014', 103))

SELECT *
FROM [LiveDB].[dbo].[LabSpecimenTests]
WHERE (SourceID = 'CCH')
AND ([RowUpdateDateTime] >= @RefreshDate)
AND (NOT [ResultDateTime] IS NULL)

--tsql e.g.2 with basic parameter
--Does NOT use index?

--decalre variables
DECLARE @RefreshDate datetime
--set variables
SET @RefreshDate = '02/02/2014'
SELECT *
FROM [LiveDB].[dbo].[LabSpecimenTests]
WHERE (SourceID = 'CCH')
AND ([RowUpdateDateTime] >= @RefreshDate)
AND (NOT [ResultDateTime] IS NULL)

tkizer
Almighty SQL Goddess

USA
37287 Posts

Posted - 02/04/2014 :  13:09:37  Show Profile  Visit tkizer's Homepage  Reply with Quote
This is known as parameter sniffing. I would suggest googling it to see what people have done as workarounds. With SQL Server 2012, you can create a plan guide, which is what we do when we encounter parameter sniffing and are unable to change the stored procedure code.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000