Solved

optimize where clause on a query

Posted on 2014-02-06
3
228 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
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 65

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 69

Accepted Solution

by:
ScottPletcher earned 500 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.

920 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

12 Experts available now in Live!

Get 1:1 Help Now