Solved

optimize where clause on a query

Posted on 2014-02-06
3
226 Views
Last Modified: 2014-02-15
Hello,

I have checked execution plan.
How can I optimize this query :
SELECT 
[SplitId],[PKey],[affId],[SalesKey]  ,[Trader]   ,[NetBro] ,[Ccy] ,[NetBroFuncCcy] ,[FuncCcy] ,[Side] ,[NetBroNoDiscount]
FROM   [dbo].[FI_Splits]
WHERE affDate<CAST(ROUND(CAST(getdate() AS float)-0.25,0)AS Datetime)
   and affDate > cast(   year(current_timestamp)-2 as char(4)) +'0101' 

Open in new window


Thanks
0
Comment
Question by:bibi92
3 Comments
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
do you have an index on affDate?

have you tried casting the AND clause as a date?
CAST( cast(   year(current_timestamp)-2 as char(4)) +'0101' AS DATETIME)
0
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
For starters, the WHERE clause is doing a comparison with both a datetime and a character values?  That would mean implicit conversion, possibly for every row.

Better to do your formulas in scalar variables of the same data type, then refer to those variables in your WHERE clause.
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
Comment Utility
Cluster dbo.FI_Splits by affDate if that is how you typically query that table (instead of the existing cluster on the id/identity column).  Add a nonclustered index on the id/identity column if you actually use it to do lookups w/o specifying a small affDate range.

To avoid unwanted column conversions, use varchar values for both affDate comparison values:

...same as before...
WHERE affDate < CONVERT(char(8), CAST(ROUND(CAST(getdate() AS float)-0.25,0) AS Datetime), 112)...same as before...
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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 INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

743 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

16 Experts available now in Live!

Get 1:1 Help Now