Solved

SQL Query Performance

Posted on 2014-03-05
3
276 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
Comment Utility
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:ScottPletcher
ScottPletcher earned 167 total points
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

771 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

11 Experts available now in Live!

Get 1:1 Help Now