Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

This question is a take-off from the excellent one posed here:

Cast to date is sargable but is it a good idea?

In my case, I am not concerned with the WHERE clause but in joining to an events table which has a column of type DATE

One table has DATETIME2 and the other has DATE... so I can effectively JOIN using a CAST( AS DATE) or I can use a "traditional" range query (>= date AND < date+1).

My question is which is preferable? The DATETIME values will almost never match the predicate DATE value.

I expect to stay on the order of 2M rows having the DATETIME and under 5k having the DATE (if this consideration makes a difference)

Should I expect the same behavior on the JOIN as I might using the WHERE clause? Which should I prefer to retain performance with scaling? Does the answer change with MSSQL 2012?

My generalized use-case is to treat my events table like a calendar table

SELECT
    events.columns
    ,SOME_AGGREGATIONS(tasks.column)
FROM
    events
LEFT OUTER JOIN
    tasks
        --This appropriately states my intent clearer
        ON CAST(tasks.datetimecolumn AS DATE) = events.datecolumn 
        --But is this more effective/scalable?
        --ON tasks.datetimecolumn >= events.datecolumn 
        --AND tasks.datetimecolumn < DATEADD(day,1,events.datecolumn)
GROUP BY
    events.columns
share|improve this question
1  
Another alternative is ON a.DatetimeColumn >= e.EventDate AND a.DatetimeColumn < DATEADD(day,1,e.EventDate) –  ypercube Oct 10 '13 at 18:50
    
@ypercube yes, precisely my question... preference and pro/con between the two. I guess I phrased it poorly. –  Matthew Oct 10 '13 at 18:51
    
Oh, I didn't get that the phrase "traditional range query" meant this. –  ypercube Oct 10 '13 at 18:53
2  
For efficiency, you should also consider making that CAST( AS DATE) a computed and indexed column. –  ypercube Oct 10 '13 at 19:03
1  
@ypercube - Or another thought that occurred to me would be to simply store the date and time separately and have a composite index on date,time. The datetimecolumn could then be a non persisted computed column derived from those components. Though I've found the definition of that surprisingly difficult. Not sure if they have to be combined as strings then cast to datetime2 or if there is a better way. –  Martin Smith Oct 13 '13 at 14:00

2 Answers 2

up vote 4 down vote accepted

"It depends".

One advantage of the = predicate and cast to date is that the join can be hash or merge. The range version will force a nested loops plan.

If there are no useful indexes to seek into the datetimecolumn on tasks this would make a substantial difference.

Setting up the 5K/ 2 million rows of test data mentioned in the question

CREATE TABLE events
  (
     eventId    INT IDENTITY PRIMARY KEY,
     datecolumn DATE NOT NULL,
     details    CHAR(1000) DEFAULT 'D'
  )

INSERT INTO events
            (datecolumn)
SELECT TOP 5000 DATEADD(DAY, ROW_NUMBER() OVER (ORDER BY @@SPID), GETDATE())
FROM   spt_values v1,
       spt_values v2

CREATE TABLE tasks
  (
     taskId         INT IDENTITY PRIMARY KEY,
     datetimecolumn DATETIME2 NOT NULL,
     details        CHAR(1000) DEFAULT 'D'
  );

WITH N
     AS (SELECT number
         FROM   spt_values
         WHERE  number BETWEEN 1 AND 40
                AND type = 'P')
INSERT INTO tasks
            (datetimecolumn)
SELECT DATEADD(MINUTE, number, CAST(datecolumn AS DATETIME2))
FROM   events,
       N

Then turning on

SET STATISTICS IO ON;
SET STATISTICS TIME ON;

And trying the CAST version

SELECT events.eventId,
       MAX(tasks.details)
FROM   events
       LEFT OUTER JOIN tasks
         ON CAST(tasks.datetimecolumn AS DATE) = events.datecolumn
GROUP  BY events.eventId

Completed in 7.4 seconds

Table 'Worktable'. Scan count 0, logical reads 0
Table 'tasks'. Scan count 1, logical reads 28679
Table 'events'. Scan count 1, logical reads 719

   CPU time = 3042 ms,  elapsed time = 7434 ms.

The estimated number of rows coming out of the join and into the GROUP BY was far too small (5006.27 vs actual 2,000,000) and the hash aggregate spilled to tempdb

Plan 1

Trying the range predicate

SELECT events.eventId,
       MAX(tasks.details)
FROM   events
       LEFT OUTER JOIN tasks
         ON tasks.datetimecolumn >= events.datecolumn
            AND tasks.datetimecolumn < DATEADD(day, 1, events.datecolumn)
GROUP  BY events.eventId 

The lack of an equality predicate forces a nested loops plan. As there are no useful indexes to support this query it has no option but to scan the 2 million row table 5,000 times.

On my machine that gave a parallel plan that eventually completed after 1 minute 40 seconds.

Table 'tasks'. Scan count 4, logical reads 143390000
Table 'events'. Scan count 5, logical reads 788
Table 'Worktable'. Scan count 0, logical reads 0
  CPU time = 368193 ms,  elapsed time = 100528 ms.

This time the number of rows coming out of the join and into the aggregate was grossly over estimated (at estimated 124,939,000 vs actual 2,000,000)

Plan 2

Repeating the experiment after altering the tables to make the respective date/time columns the clustered primary key altered the results.

Both queries ended up choosing a nested loops plan. The CAST as DATE version gave a serial version that completed in 4.5 seconds and the range version a parallel plan that completed in elapsed time 1.1 seconds with CPU time of 3.2 seconds.

Applying MAXDOP 1 to the second query to make the figures more easily comparable returns the following.

Query 1

Table 'Worktable'. Scan count 0, logical reads 0
Table 'tasks'. Scan count 5000, logical reads 78137
Table 'events'. Scan count 1, logical reads 719
   CPU time = 3167 ms,  elapsed time = 4497 ms.

Query 2

Table 'tasks'. Scan count 5000, logical reads 49440
Table 'events'. Scan count 1, logical reads 719
   CPU time = 3042 ms,  elapsed time = 3147 ms.

Query 1 had an estimated 5006.73 rows coming out of the join and the hash aggregate spilled to tempdb again.

Query 2 again has a large overestimate (at 120,927,000 this time).

The other obvious difference between the two results is that the range query looks like it manages to seek into tasks more efficiently in some way. Only reading 49,440 pages vs 78,137.

The range that the cast as date version seeks into is derived from an internal function GetRangeThroughConvert. The plan shows a residual predicate on CONVERT(date,[dbo].[tasks].[datetimecolumn],0)= [dbo].[events].[datecolumn].

If Query 2 is altered to

   LEFT OUTER JOIN tasks
     ON tasks.datetimecolumn > DATEADD(day, -1, events.datecolumn)
        AND tasks.datetimecolumn < DATEADD(day, 1, events.datecolumn)

Then the number of reads then becomes the same. The dynamic seek used by the CAST AS DATE version reads unnecessary rows (two days worth rather than one) and then discards them with the residual predicate.

One other possibility would be to restructure the table to store the date and time components in different columns.

CREATE TABLE [dbo].[tasks](
    [taskId] [int] IDENTITY(1,1) NOT NULL,
    [datecolumn] date NOT NULL,
    [timecolumn] time NOT NULL,
    [datetimecolumn]  AS DATEADD(day, DATEDIFF(DAY,0,[datecolumn]), CAST([timecolumn] AS DATETIME2(7))),
    [details] [char](1000) NULL,
PRIMARY KEY CLUSTERED 
(
    [datecolumn] ASC,
    [timecolumn] ASC
))

The datetimecolumn can be derived from the component parts and this has no effect on row size (as width of date + time(n) is the same as the width of datetime2(n)). (With an exception being if the additional column increases the size of the NULL_BITMAP)

The query is then a straight forward = predicate

SELECT events.eventId,
       MAX(tasks.details)
FROM   events
       LEFT OUTER JOIN tasks
         ON tasks.datecolumn = events.datecolumn
GROUP  BY events.eventId

This would allow a merge join between the tables without any need to sort. Though for these table sizes a nested loops join was chosen anyway with stats as below.

Table 'tasks'. Scan count 5000, logical reads 44285
Table 'events'. Scan count 1, logical reads 717
   CPU time = 2980 ms,  elapsed time = 3012 ms.

As well as potentially allowing different logical join types storing the date separately as the leading index column would also potentially benefit other queries on tasks such as grouping by date.

As for why the = predicate shows fewer logical reads on tasks than the > <= version with the same nested loops plan (44,285 vs 49,440) this appears to be related to the read ahead mechanism.

Turning on trace flag 652 reduces the logical reads of the range version to the same as that of the equals version.

share|improve this answer
    
+1 like just about anything, there are always exceptions and more layers of the onion to peel.. –  Aaron Bertrand Oct 12 '13 at 13:34
    
@AaronBertrand - Agreed. I hadn't even noticed the part of the onion that the seeks were on different ranges before. –  Martin Smith Oct 12 '13 at 13:46
    
+1 sorry @AaronBertrand I have to give Martin the answer. –  Matthew Oct 14 '13 at 16:38

I agree with Martin that cardinality estimates can potentially suffer using this approach vs. a date range approach. I'll also add that using CONVERT(DATE and still getting sargability might imply to other folks reading code or learning from it that it's a good idea in general to use functions against the column, particularly when the column is indexed. Since this is the only exception where this works, and in all other cases this actually forces a scan when a seek might have been possible, I don't think it's good practice to use an exception that doesn't have any real benefit except to the author of the code, and even that is short-lived - you save a few seconds writing a more concise expression, and it's something you do once. I face the same opposition all the time when answering questions - I see others posting answers that include bad habits, such as declaring varchar without length, and I often comment. The excuse I hear back is that it works fine in this case, but that's not the point - folks learn from this case and apply what they learned to other cases, where it might not work so well. And it may even break in the same case - for example, imagine if you later want to join on weeks or half days or something, you'll need to use a different data type and you might lose the benefit you thought you were getting.

For an INNER JOIN, using the WHERE clause vs. the ON clause will make no difference. However, similarly, I would prefer to keep joining criteria in the ON clause, and filtering criteria in the WHERE clause. This changes if you're talking about an OUTER JOIN, of course, since the placement of certain criteria can change semantics.

I would write your query this way (and I'd be careful where you use the made-up "word" performant, as it may get funny stares from most):

LEFT OUTER JOIN
  dbo.tasks -- always use schema prefix!
  ON tasks.datetimecolumn >= events.datecolumn
    AND tasks.datetimecolumn < DATEADD(DAY, 1, events.datecolumn)

Of course, you should test this to see what impact the DATEADD() has on the events table. Since it sounds like that is the smaller table by a large margin, I don't expect the effect to be huge, but it can't hurt to check.

share|improve this answer
    
excellent point on the extensibility of the practice. I have updated my question with a general sample to illustrate my use case. –  Matthew Oct 10 '13 at 19:03
    
I think you've effectively answered the question (and I will accept) but I'm curious about your impressions on ypercube's suggestion of a computed column with an index –  Matthew Oct 10 '13 at 19:26
1  
@Matthew I think that can be a good idea in some cases, but I don't know enough about your workload to be sure that it is better overall in your specific case. You seem to be aware that there is maintenance overhead so you are probably in the best position to test this approach in your environment, on your schema / data / hardware, over your business cycle. –  Aaron Bertrand Oct 10 '13 at 19:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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