我一直在使用这种方法来过滤我的查询:
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
Run Code Online (Sandbox Code Playgroud)
这实际上会使我的查询在664行结果集上减慢2秒.SQL调优顾问没有多大帮助,所以我认为这不是正确的方法.除了大量的if语句之外,还有正确的方法吗?
假设您已正确索引选择所在的表,并且这些字段是索引的一部分,我的猜测是它将是对isnull的调用.我会改变它们:
(@clientID is null or a.clientID = @clientId) and ...
Run Code Online (Sandbox Code Playgroud)
至于case语句,位字段的索引是没有意义的,因此没有太多可做的事情.
| 归档时间: |
|
| 查看次数: |
428 次 |
| 最近记录: |