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


Bizarre issue while using datetime variable instead of harcoded value


Bizarre issue while using datetime variable instead of harcoded value

Author
Message
jyz
jyz
SSC-Enthusiastic
SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)

Group: General Forum Members
Points: 121 Visits: 124
Hi, I am facing a bizarre issue while testing a query, When i test with hardcoded date value, the query runs in 2 mins (tables used in joins are huge)
, but when i assign the date in a variable and use, it took more than 20 mins and then i cancelled the query.

Sample query
SELECT *
FROM table1 A
,table2 B
,table3 C
,table4 D

WHERE A.col1= B.col1
AND C.col1=D.col2
AND C.col1=A.col1
AND D.col3 LIKE '%test%'
AND D.coldate> '10/12/2012'

above one takes 2 min and below one more than 20 mins

declare @dt datetime
set @dt ='10/12/2012'

SELECT *
FROM table1 A
,table2 B
,table3 C
,table4 D

WHERE A.col1= B.col1
AND C.col1=D.col2
AND C.col1=A.col1
AND D.col3 LIKE '%test%'
AND D.coldate > @dt


Could any one help me out of this.

Any suggestions would be appreciated

Thanks
jyz
jyz
SSC-Enthusiastic
SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)

Group: General Forum Members
Points: 121 Visits: 124
.
Eugene Elutin
Eugene Elutin
SSCarpal Tunnel
SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)

Group: General Forum Members
Points: 4976 Visits: 5478
Is it just a query behaviour or you see it only when wrapped into stored procedure?
Does look like parameter sniffing to me...
Also, it's a bad practice to use strings for datetime in non-ISO format. Always use YYYYMMDD (or YYYY-MM-DD)

_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
John Mitchell-245523
John Mitchell-245523
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14083 Visits: 15950
What is the data type of the coldate column?

Have you compared the execution plans for the two queries?

John
jyz
jyz
SSC-Enthusiastic
SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)

Group: General Forum Members
Points: 121 Visits: 124
datatype of coldate is datetime,

there is no difference in the execution plan of both queries.

Thanks Eugene Elutin for that date format practice .. old habits hard to change .. Smile
Eugene Elutin
Eugene Elutin
SSCarpal Tunnel
SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)

Group: General Forum Members
Points: 4976 Visits: 5478
jeyis (10/16/2012)
datatype of coldate is datetime,

there is no difference in the execution plan of both queries.

Thanks Eugene Elutin for that date format practice .. old habits hard to change .. Smile


Is it just a query or stored procedure?
Also, why you are not using proper JOINS? That is even worse than not using dates in ISO format...

Can you try this:


DECLARE @dt DATETIME
SET @dt ='20121210'

SELECT *
FROM table1 A
JOIN table2 B ON B.col1 = A.col1
JOIN table3 C ON C.col1 = A.col1
JOIN table4 D ON D.col2 = C.col1
WHERE D.col3 LIKE '%test%'
AND D.coldate > @dt



_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
GilaMonster
GilaMonster
SSC Guru
SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)

Group: General Forum Members
Points: 87011 Visits: 45267
Definitely looks like parameter sniffing (or more correctly, the lack thereof)
http://sqlinthewild.co.za/index.php/2008/02/25/parameter-sniffing-pt-2/

Can you post execution plans please?

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


jyz
jyz
SSC-Enthusiastic
SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)

Group: General Forum Members
Points: 121 Visits: 124
Thanks again for the reply,

I am testing it as query now, eventually i'll have to put this part into a stored procedure
I agree with you Join practice and the comma separated is really outdated. But even after trying with that it doesn't give the desired output.
jyz
jyz
SSC-Enthusiastic
SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)

Group: General Forum Members
Points: 121 Visits: 124
Hi Gail Shaw, The server which i refer to is sql 2000. i don't find an option to save execution plan. Let me read the article about parameter sniffing which you provided. Thanks for your reply
GilaMonster
GilaMonster
SSC Guru
SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)

Group: General Forum Members
Points: 87011 Visits: 45267
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/ Shows how to save the plan on SQL 2000.

p.s. Please post SQL 2000 questions in the SQL 2000 forums in future. In the SQL 2008 forums we'll assume you're running SQL 2008.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


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