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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Anyone know WHY?

Author  Topic 

nizguy
Starting Member

37 Posts

Posted - 2012-09-07 : 14:09:04
Here is what happens.
When I hard coded the date to '8/8/12', It get me the result in less than 5 seconds.
When I use the @FromDate variable, It gave me the same result but took over 2 minutes, I tried it many times, It took much longer to return the result.
Does anyone know WHY?

Thanks




DECLARE @FromDate as smalldatetime
SET @FromDate = dateadd(day, -30, getdate())

SELECT
Bill_num
,Pickup_date
,Shipper_Id, Shipper_name, Shipper_addr1
FROM
Bill
WHERE
--Pickup_date > @FromDate
Pickup_date > '8/8/2012'
and (Re_Weight_Class is not null and Re_Weight_Class <> '')

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-09-07 : 14:14:09
Take a look at this thread to see if the suggestions there would help you: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=177995

When you use the variable, it ends up with a bad execution plan. All the suggestions are to trick query optimizer from getting stuck like that.
Go to Top of Page

lionofdezert
Aged Yak Warrior

885 Posts

Posted - 2012-09-07 : 14:48:04
Local variables can reduce performance. WHY
http://connectsql.blogspot.com/2012/07/sql-server-how-local-variables-can.html

--------------------------
http://connectsql.blogspot.com/
Go to Top of Page

nizguy
Starting Member

37 Posts

Posted - 2012-09-07 : 15:35:08
thank you all all these info
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-09-08 : 07:23:21
quote:
Originally posted by lionofdezert

Local variables can reduce performance. WHY
http://connectsql.blogspot.com/2012/07/sql-server-how-local-variables-can.html

--------------------------
http://connectsql.blogspot.com/

Interesting article; I want to run that for myself and see. Just as interesting is Peso's comment where he draws a different conclusion.
Go to Top of Page
   

- Advertisement -