Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

optimize where clause on a query

Posted on 2014-02-06
3
Medium Priority
?
242 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 70

Expert Comment

by:Éric Moreau
ID: 39838687
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 66

Expert Comment

by:Jim Horn
ID: 39838763
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 70

Accepted Solution

by:
Scott Pletcher earned 2000 total points
ID: 39839376
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

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

876 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