Solved

optimize where clause on a query

Posted on 2014-02-06
3
234 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 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:
Scott Pletcher 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Job Failed 6 31
how to use ROW_NUMBER() correctly 8 44
SQL Server and Access Database (Project Codes) get the next record 43 35
learning MS SSIS 13 23
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

751 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