Please start any new threads on our new 
    site at https://forums.sqlteam.com.  We've got lots of great SQL Server
    experts to answer whatever question you can come up with.
    
        
            
                
                    
                        
                            
                                | Author | 
                                
                                 Topic  | 
                             
                            
                                    | 
                                         DLTaylor 
                                        Posting Yak  Master 
                                         
                                        
                                        136 Posts  | 
                                        
                                        
                                            
                                            
                                             Posted - 2014-02-04 : 07:28:50
                                            
  | 
                                             
                                            
                                            | 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 TSQLI 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 INDEXSELECT *  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 variableDECLARE  	  @RefreshDate datetime	--set valueSET @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 variablesDECLARE  	  @RefreshDate datetime--set variablesSET @RefreshDate =  '02/02/2014'SELECT *	  FROM [LiveDB].[dbo].[LabSpecimenTests]	  WHERE			(SourceID = 'CCH') 				AND ([RowUpdateDateTime] >= @RefreshDate)				AND (NOT [ResultDateTime] IS NULL) | 
                                             
                                         
                                     | 
                             
       
                            
                       
                          
                            
                                    | 
                                     tkizer 
                                    Almighty SQL Goddess 
                                     
                                    
                                    38200 Posts  | 
                                    
                                      
                                        
                                          
                                           
                                            Posted - 2014-02-04 : 13:09:37
                                          
  | 
                                         
                                        
                                          | 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 KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                   
                            
                            
                                | 
                                    
                                      
                                     
                                    
                                 | 
                             
                         
                     | 
                 
             
         |