Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Syntax Performance Tuning advice - dates

Posted on 2014-03-21
2
Medium Priority
?
143 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 49

Assisted Solution

by:PortletPaul
PortletPaul earned 1000 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 70

Accepted Solution

by:
Scott Pletcher earned 1000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

885 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