Solved

SQL Query Performance

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

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…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

831 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