Solved

SQL Syntax Performance Tuning advice - dates

Posted on 2014-03-21
2
133 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

863 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

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now