Solved

SQL Syntax Performance Tuning advice - dates

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Simple SQL query from two tables 13 54
Need some help to cast ntext to nvarchar SQL 2000 7 33
sql query to calculate avaerage 21 39
Query Syntax 17 35
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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.
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

773 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