Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How to make this query sargable

Author  Topic 

sql-lover
Yak Posting Veteran

99 Posts

Posted - 2012-08-01 : 16:21:23
Hi,

Got a query that is so slow, that just returns a timeout error to the user, via application.

I checked the store procedure and execution plan and I believe this is the reason:

WHERE TimeAdded between @StartDate and DATEADD(d, 1, @EndDate)

How can I change that statement, while keeping logic, to a sargable one? I though about using >= and < plus and AND operator, removing the BETWEEN, but I have not tested it yet.

Any suggestion is highly appreciated.

Thanks.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-01 : 16:35:10
why do you think the above condition is NOn sargeable?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2012-08-01 : 16:35:49
That should already be sargable, if not:
SET @EndDate=DATEADD(d, 1, @EndDate)
...
WHERE TimeAdded between @StartDate and @EndDate
What do you mean by "sargable" anyway? If you're selecting all or most of the columns, and the date range is not very selective a table scan may likely be the most efficient. Make sure your statistics are up to date.
Go to Top of Page

sql-lover
Yak Posting Veteran

99 Posts

Posted - 2012-08-01 : 16:49:54
Ignore... no caffeine today ...

The issue is lack of a proper Index. It was working before (as per the developer) a few years ago, but we do have much more data now.

I'll deploy a proper Index and validate.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-01 : 16:57:33
quote:
Originally posted by sql-lover

Ignore... no caffeine today ...

The issue is lack of a proper Index. It was working before (as per the developer) a few years ago, but we do have much more data now.

I'll deploy a proper Index and validate.


No problem
let us know the outcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -