0

I have 2 queries, they are almost similar.

Query #1.

declare @dateStart datetime= '4/3/2014 12:00:00 AM'
declare @dateEnd datetime = '4/25/2014 12:00:00 AM'

SELECT * 
FROM vFailed (NOLOCK) 
WHERE dtm>=  @dateStart AND dtm< @dateEnd AND mediaNo = 'xyz'

Query #2:

SELECT * 
FROM vFailed (NOLOCK) 
WHERE dtm >=  '4/3/2014 12:00:00 AM'
  AND dtm < '4/25/2014 12:00:00 AM' AND mediaNo= 'xyz'

First query returns result in a few seconds, but the second returns result for a long time or does not return any result.

What may be reason for results of these similar queries?

4
  • 6
    Maybe because the second query cast every record from string to Date! May 26, 2014 at 11:30
  • like @meysam said, do casting for date instead of using as string. May 26, 2014 at 11:33
  • It must be an issue with execution plan of both the queries. May be the issue is that first query is running using execution plan generated earlier, while the second query is generating execution plan every time. Share the execution plan of both the queries to get more idea.
    – Roopesh
    May 26, 2014 at 12:56
  • We need more information, like the EXPLAIN plans of both queries. Otherwise, this makes no sense - SQL Server can implicitly cast character-based types to date/time types, and any optimizer worth its salt would cast the parameters instead of all rows (sorry, @MeysamTolouee, although the EXPLAIN plan might reveal I'm wrong). I'm assuming that dtm is also some sort of datetime type... May 26, 2014 at 14:00

2 Answers 2

0

in query_1 you are comparing a datetime to a datetime. in query_2 you are comparing to a varchar, thus converting all datetime in the query to a string and matching it against the given string. what query_2 should look like (converting your strings to datetime beforehand):

SELECT * 
FROM vFailed (NOLOCK) 
WHERE dtm >= convert(datetime,'2014-04-03 00:00:00',120)
  AND dtm <  convert(datetime,'2014-04-25 00:00:00',120)
  AND mediaNo= 'xyz'
3
  • did not help, it is still returning. I did it but even that didn't help.
    – mtinyavuz
    May 26, 2014 at 11:56
  • i changed tarih to dtm like you did. are you really using the same column in both queries and if not is one of them indexed? May 26, 2014 at 12:29
  • While I do tend to recommend making the cast/convert explicit for clarity reasons, SQL Server can/will implicitly cast all character-based types to all date/time based types, so I find this unlikely. Any optimizer worth it's salt would chose to convert the parameters over all rows in the table. May 26, 2014 at 13:54
0

I ran a test and got identical execution plans and identical times
Side by side the query optimizer said each was 50% of the total
Something else is going here

Post/examine your execution plans

To take out I/O try select count(*)

I am looking at a table with over 1 million rows
I tried some other date columns and did get some different execution plans
And the second was faster
But both ran in a fraction of a second
Do you have and index on dtm?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.