Solved

SQL Query Performance

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Slow Connectivity over ODBC 8 36
migration MS SQL database to Oracle 30 61
How to search for strings inside db views 4 28
SQL - Copy data from one database to another 6 19
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

777 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