Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Bizarre issue while using datetime variable instead of harcoded value Expand / Collapse
Author
Message
Posted Tuesday, October 16, 2012 3:34 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, October 1, 2014 12:07 AM
Points: 97, 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
Post #1373093
Posted Tuesday, October 16, 2012 3:38 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, October 1, 2014 12:07 AM
Points: 97, Visits: 124
.
Post #1373096
Posted Tuesday, October 16, 2012 4:09 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, November 18, 2014 3:45 AM
Points: 2,873, Visits: 5,188
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!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1373105
Posted Tuesday, October 16, 2012 4:12 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, October 31, 2014 11:06 AM
Points: 5,430, Visits: 10,108
What is the data type of the coldate column?

Have you compared the execution plans for the two queries?

John
Post #1373106
Posted Tuesday, October 16, 2012 4:40 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, October 1, 2014 12:07 AM
Points: 97, 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 .. :)
Post #1373121
Posted Tuesday, October 16, 2012 4:48 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, November 18, 2014 3:45 AM
Points: 2,873, Visits: 5,188
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 .. :)


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!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1373126
Posted Tuesday, October 16, 2012 5:03 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 10:03 AM
Points: 40,385, Visits: 36,829
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 2008, MVP
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

Post #1373136
Posted Tuesday, October 16, 2012 5:03 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, October 1, 2014 12:07 AM
Points: 97, 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.
Post #1373137
Posted Tuesday, October 16, 2012 5:14 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, October 1, 2014 12:07 AM
Points: 97, 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
Post #1373143
Posted Tuesday, October 16, 2012 5:18 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 10:03 AM
Points: 40,385, Visits: 36,829
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 2008, MVP
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

Post #1373147
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse