Wednesday, 12 August 2015

Avoid using function calls in queries.


Function calls prevent sql server to use indexes.

SELECT OrderId,Amount FROM Orders
WHERE OrderDate > GetDate()

In this query despite of having index on OrderDate, a full table scan will be performed to search each and every record of table because of function call, hence no advantage of indexes and a huge loss in performance.

better solution for this query would be to avoid calling get date in query like this

Declare @DTime DateTime Set @DTime = GetDate()
SELECT OrderId,Amount FROM Orders
WHERE OrderDate > @DTime