Solved

SQL Query Performance

Posted on 2014-03-05
3
281 Views
Last Modified: 2014-03-10
A few questions on SQL performance. Query will be run against millions of records so performance is key.

1. If I have the following condition: where t1.date < getdate()

Would it improve performance to run getdate() once at the beginning of the query, and save it to a variable? Then do: where t1.date < variable? This way each record is checked against a variable instead of having to run getdate() a million times. I'm assuming it's run for every record -- one record at a time? At least that's how I think SQL parses queries.

2. Does it matter what order the conditions are placed in the where clause? For example, certain conditions eliminate 50% of all records. Should such conditions be placed at the beginning of the where clause, and conditions that eliminate only 5% of records placed towards the end?
0
Comment
Question by:pzozulka
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 167 total points
ID: 39908397
One GETDATE() I believe is okay, but multiple GETDATE() could result in one execution for each row.   Multiple GETDATE()'s are still fine, but you run the risk of different values.

If you use a function though, like DATEDIFF(d, -1, GETDATE()), then it would process once for every row.

The variable route..

Declare @dt datetime = GETDATE()
SELECT blah, blah, blah FROM your_table WHERE some_date_column < @dt

Open in new window

>2. Does it matter what order the conditions are placed in the where clause?
No, unless you have parentheses marks ( ) to explicitly state order of execution.
0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 167 total points
ID: 39910240
1.  No, the query plan should be the same.  As I understand it, SQL will generate only a single GETDATE() value for all uses of GETDATE() in the same batch.

Edit: Far more important is the data type of the table column.  To prevent unwanted implicit conversions of the data column, you'd be better off converting the GETDATE() to the relevant character string.

2.  Generally no, although it can't hurt to put the more-restricting checks first.  Within a CASE WHEN, however, the clauses are always evaluated in order, so order would of course matter then.
0
 
LVL 35

Assisted Solution

by:David Todd
David Todd earned 166 total points
ID: 39916745
Hi,

Just very occasionally, on older versions of SQL, I found it was beneficial to use derived tables in the from clause, and put as many conditions as possible in that subquery - particularly if the base table was large.) Or even return that first query to a temp table, if necessary add indexes, and then perform the rest of the query.

HTH
  David
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.

752 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