Solved

SQL Syntax Performance Tuning advice - dates

Posted on 2014-03-21
2
130 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
Comment Utility
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:
ScottPletcher earned 250 total points
Comment Utility
>> 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

763 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

6 Experts available now in Live!

Get 1:1 Help Now