Solved

SQL Syntax Performance Tuning advice - dates

Posted on 2014-03-21
2
137 Views
Last Modified: 2014-04-07
Are there any DONT DO for SQL and dates when it comes to performance

Specifically i'm trying to optimise this
If i pass in a @TwoDaysBeforeDate as '2014-3-19'
is this the best way, or should i use something like getDate(-2) for performance ?

select * from vwQuickAlertMaster where vDeviceName != '' and dDeviceLoggedDate >= @TwoDaysBeforeDate and bIsacknowledged = 0 and iSoftwarePriority = 3 and bIsAlert = 1 order by dDeviceLoggedDate desc		  

Open in new window


thanks
0
Comment
Question by:websss
2 Comments
 
LVL 48

Assisted Solution

by:PortletPaul
PortletPaul earned 250 total points
ID: 39945189
This depends on what you want, try this
SELECT
  getDate()                                     AS "getDate"
, dateadd(DAY, -2, getDate() )                  AS "deduct 2 days"
, dateadd(DAY, datediff(DAY,0, getDate() ), -2) AS "2 days ago at start of day"

|                      GETDATE |                DEDUCT 2 DAYS |   2 DAYS AGO AT START OF DAY |
|------------------------------|------------------------------|------------------------------|
| March, 21 2014 13:37:13+0000 | March, 19 2014 13:37:13+0000 | March, 19 2014 00:00:00+0000 |

Open in new window

By the way:
It is advised that you use this literal structure for dates:

set @TwoDaysBeforeDate = '20140319'

& Here are some worthwhile references on dates in SQL Server:

start here:
"The ultimate guide to the datetime datatypes"

"Bad habits to kick : mis-handling date / range queries"
"What do BETWEEN and the devil have in common?"
"Beware of Between"

DATE and TIME ... don't be scared, and do it right (the first time) (E-E, by angelIII)

--
oh, and for the performance portion of your question, it won't matter which option you choose as long as there are indexes for the fields used in that where clause - that is much more important than the method used for setting the date parameter.
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 250 total points
ID: 39945288
>> Are there any DONT DO for SQL and dates when it comes to performance <<

The single biggest factor for performance is getting the correct clustered index on the table.  Thus, if you (almost) always specify dDeviceLoggedDate when SELECTing from this table, cluster the table on that column.

When comparing date columns, convert the comparison date (not the table column) to format 'YYYYMMDD', as that format is 100% accurate under all SQL date and language settings and will work efficiently even if the data type of the underlying column changes.

If you need to check for a range of dates, use >= and < rather than between.

For example, to search for only yesterday's data, do this:

WHERE
    dDeviceLoggedDate >= CONVERT(char(8), GETDATE() - 1, 112) AND
    dDeviceLoggedDate < CONVERT(char(8), GETDATE(), 112)
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question