-- tf_wfget_appvr_records
create function [dbo].[tf_wfget_appvr_records] (@cls_id int, @start_date datetime, @end_date datetime, @approver_id int, @co_id varchar(5))
returns @wfapp_approver_status
table
(app_recgid bigint,
app_status varchar(1),
app_status_desc varchar(80)
)
as
begin
insert into @wfapp_approver_status
(app_recgid, app_status, app_status_desc)
select a.recgid, a.status, p.action
from cm_wfapp a
inner join cm_process p on a.proc_id = p.recgid
where a.co_id = @co_id and a.cls_id = @cls_id
and a.apply_date between @start_date and @end_date
and a.approver_id = @approver_id
-- insert others from cm_wftrn
insert into @wfapp_approver_status
(app_recgid, app_status, app_status_desc)
select distinct a.recgid, a.status, p.action
from cm_wftrn c
inner join cm_process p on c.proc_id = p.recgid and p.action is not null
inner join cm_wfapp a on a.recgid = c.wfapp_id
where a.co_id = @co_id and a.cls_id = @cls_id
and a.apply_date between @start_date and @end_date
and c.wfapp_id not in (select app_recgid from @wfapp_approver_status)
and c.appr_id = @approver_id
return
end
GO
它包含NOT IN約束,該約束需要花費大量時間從數據庫中提取結果。 數據庫很明顯。使用NOT IN優化查詢(select ...)
如何優化此查詢? 有沒有辦法用NOT IN(select ...)優化查詢?
請幫忙謝謝。
** 1 **二Insert語句將永遠** **返回任何東西。所有已經插入第一個插入的app_regcid。 ** 2 **爲什麼使用功能? –