Login or Sign Up to become a member!

EXPERTS, INFORMATION, IDEAS & KNOWLEDGE

Social bookmarker Add this

Your profile

Search

November 2008
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

XML Feeds

« How Does Between Work With Dates In SQL Server?SQL Server 2008 geography data type screencasts on Channel 9 »
The Data Management Journal

Converting Columns To Date From Datetime Does Not Result In A Scan In SQL Server 2008

by SQLDenis


Permalink 24 Jul 2008 12:28 , Categories: Data Modelling & Design

I was reading Itzik Ben-Gan’s An Introduction to New T-SQL Programmability Features in SQL Server 2008 article yesterday after one of my friends allerted me to the following from that article
For example, the plan for the following query performs an index seek on the index on the CurrencyRateDate DATETIME column:

  1. USE AdventureWorks;
  2.  
  3. SELECT FromCurrencyCode, ToCurrencyCode, EndOfDayRate
  4.  
  5. FROM Sales.CurrencyRate
  6.  
  7. WHERE CAST(CurrencyRateDate AS DATE) = ‘20040701′;

I was surprised by this, as we all know functions/conversions on column names are generaly bad for performance.

Let’s see how this works. First create this table in the tempdb database.

  1. USE tempdb
  2.  
  3. go
  4.  
  5. CREATE TABLE TestDatetimePerf (SomeCol DATETIME,id INT IDENTITY)
  6.  
  7. go

This will insert 2048 rows with dates between 2008-01-01 12 AM and 2008-03-26 7 AM

  1. INSERT TestDatetimePerf(SomeCol)
  2.  
  3. SELECT DATEADD(hh,number,‘20080101′)
  4.  
  5. FROM master..spt_values
  6.  
  7. WHERE type =‘P’
  8.  
  9. go
  10.  
  11. CREATE INDEX ix_Date ON TestDatetimePerf(SomeCol)
  12.  
  13. go

Turn on the execution plan

  1. SET showplan_text ON
  2.  
  3. go

Execute the following query

  1. SELECT *
  2.  
  3. FROM TestDatetimePerf
  4.  
  5. WHERE CONVERT(VARCHAR(30),SomeCol,112) = ‘20080103′

Here is the plan

  1. |–Table Scan(OBJECT:([tempdb].[dbo].[TestDatetimePerf]),
  2. –WHERE:(CONVERT(varchar(30),[tempdb].[dbo].[TestDatetimePerf].[SomeCol],112)=[@1]))

As you can see that results in a scan.

What happens when you convert to date?

  1. SELECT *
  2.  
  3. FROM TestDatetimePerf
  4.  
  5. WHERE CONVERT(DATE,SomeCol) = ‘20080103′

Here is the plan

  1. |–Nested Loops(Inner Join, OUTER REFERENCES:([Bmk1000]))
  2. |–Nested Loops(Inner Join, OUTER REFERENCES:([Expr1007], [Expr1008], [Expr1006]))
  3. | |–Compute Scalar(DEFINE:(([Expr1007],[Expr1008],[Expr1006])=GetRangeThroughConvert(’2008-01-03′,’2008-01-03′,(62))))
  4. | | |–Constant Scan
  5. | |–Index Seek(OBJECT:([tempdb].[dbo].[TestDatetimePerf].[ix_Date]),
  6. –SEEK:([tempdb].[dbo].[TestDatetimePerf].[SomeCol] > [Expr1007]
  7. –AND [tempdb].[dbo].[TestDatetimePerf].[SomeCol] < [Expr1008]),
  8. –WHERE:(CONVERT(date,[tempdb].[dbo].[TestDatetimePerf].[SomeCol],0)=’2008-01-03′) ORDERED FORWARD)
  9. |–RID Lookup(OBJECT:([tempdb].[dbo].[TestDatetimePerf]), SEEK:([Bmk1000]=[Bmk1000]) LOOKUP ORDERED FORWARD)

See that? You get a seek instead, very interesting. It would be nice that when you use convert with the style optional parameter that the optimizer would be smart enough to convert that also to a seek.

1 comment »1 trackback » 1093 views

Trackback address for this post

Trackback URL (right click and copy shortcut/link location)

1 comment, 1 trackback

Trackback from: Pythian Group Blog [Visitor]
Log Buffer #108: A Carnival of the Vanities for DBAs
Welcome to the 108th edition of Log Buffer, the weekly review of database blogs.
With almost no ado at all, let’s begin with the bad news–from StatisticsIO and Jason Massie: The Death of the DBA. And who is the perpetrator of this crime? ...
08/01/08 @ 12:42
Comment from: Dave [Visitor]
*****
I just wanted to point out that when using the Select All option to copy and paste into SQL Management Studio, the " ' " around the date is not correct for some reason. Just wanted to let you know.
10/24/08 @ 14:09

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
PoorExcellent
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)