I have been using this method to filter my queries:
Create PROCEDURE [dbo].[pGetTask] @showCompletedTasks bit = 1 ,@showInProgressTasks bit = 1 ,@taskID int = null ,@projectID int = null ,@applicationID int = null ,@clientID int = null ... Snip ... where a.clientID = isnull(@clientID, a.clientID) and a.applicationID = isnull(@applicationID, a.applicationID) and p.projectID = isnull(@projectID, p.projectID) and t.taskID = isnull(@taskID, t.taskID) and curr.complete = case @showCompletedTasks when 0 then 0 else curr.complete end and curr.complete = case @showInProgressTasks when 0 then 1 else curr.complete end
This actually slows my queries by 2 seconds on a 664 row result set. The SQL tuning advisor isn’t much help, so I figure this is not the right way to do this. Is there a right way, besides a ton of if statements?
Assuming you have properly indexed the table that the select is on, and these fields are part of the index, my guess is that it would be the calls to isnull. I would change them to this:
As for the case statements, indexes on bit fields are pointless, so there’s not much to do there.