Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T-SQL ISNULL() Optimization

I have this clause on a join in a stored procedure I inherited:

WHERE a.is_active = 1
AND b.due_date <= ISNULL(@due_date_limit, b.due_date)

How would I re-write this to remove the ISNULL as it's causing severe performance problems?

like image 227
Frozenskys Avatar asked Sep 18 '09 14:09

Frozenskys


1 Answers

in this instance, i would have an if statement, checking @due_date_limit

IF (@due_date_limit IS NULL)
BEGIN
    ...
    WHERE   a.is_active = 1 --not required to compare b.due_date <= b.due_date
END
ELSE
BEGIN
    ...
    WHERE   a.is_active = 1
    AND     b.due_date <= @due_date_limit
END
like image 141
Adriaan Stander Avatar answered Sep 21 '22 19:09

Adriaan Stander