Solved

SQL Query Performance

Posted on 2014-03-05
3
277 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
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

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

24 Experts available now in Live!

Get 1:1 Help Now