2012-06-13 59 views
0

我在item_audit table中有100萬行。以下是我在項目表上的觸發器。爲什麼這麼慢?SQL Server 2008插入後,當我在審計表中插入或刪除表時,更新觸發非常慢

create trigger [dbo].[item_audit_trigger] on [dbo].[item] after insert, update as 
begin 
    declare @type char(1), @begindate varchar(16), @enddate varchar(16),  @hostname 
      nvarchar(128), @programname nvarchar(128), @loginname nvarchar(128),  
    @logintime varchar(16), @ocuserid varchar(12), @ocquefromid varchar(10), @lcontextinfo 
      varchar(128) 


    set @begindate = dbo.DateToD16(getdate()) 
    set @enddate = '9999123100000000' 
    select @hostname  = host_name from sys.dm_exec_sessions where session_id = @@SPID 
    select @programname = program_name from sys.dm_exec_sessions where session_id = @@SPID 
    select @loginname = login_name from sys.dm_exec_sessions where session_id = @@SPID 
    select @logintime = dbo.DateToD16(login_time) from sys.dm_exec_sessions where 
      session_id = @@SPID 
    select @lcontextinfo = cast(context_info as varchar(128)) from sys.dm_exec_sessions where 
      session_id = @@spid 
    set @ocuserid = substring(@lcontextinfo, 1, 12) 
    set @ocquefromid = substring(@lcontextinfo, 13, 10) 
... 

     set @type = 'I' 

--------------- Below update takes 2 minutes 

     update medorder_audit set audit_end = @begindate 
     from medorder_audit a inner join inserted d 
     on a.MO_ID = d.MO_ID and a.PAT_ID = d.PAT_ID 
     where audit_end = @enddate 
--------------------------------------------------------------- 

     insert into medorder_audit (mo_id, mo_stat, pat_id, item_id, physn_id, physn_name, pharm_id, start_dati, stop_dati, frequency, dose, route, site, prn, sol_time, 
     sol_amt, sol_units, adm_times, adm_instr, add_dati, lu_dati, lu_from, er_wnd, lt_wnd, mo_qty, item_name, dos_str, str_units, dos_units, dos_amt, tvol_amt, tvol_units, 
     dos_form, mo_pstatus, mo_doseunt, xact_dati, user_id, user_name, omni_stid, qty, dose_text, mo_alert, give_amt, give_units, dose_max, rx_suffix, rx_name, rx_disp, 
     duration, interval, stat, only_days, wnd_type, sch_done, use_def, mocf_stat, mocf_user, mocf_dati, admin_dati, adm_dur, adm_site, comp_ct, comp_dati, mocf_inits, 
     mocf_title, ftdue_dati, is_iv, mix_id, mix_type, mo_disp, lu_user, modisp_db, psb_fill, audit_begn, audit_end, audit_type, audit_host, audit_prgm, audit_logn, 
     audit_date) select mo_id, mo_stat, pat_id, item_id, physn_id, physn_name, pharm_id, start_dati, stop_dati, frequency, dose, route, site, prn, sol_time, sol_amt, 
     sol_units, adm_times, adm_instr, add_dati, lu_dati, lu_from, er_wnd, lt_wnd, mo_qty, item_name, dos_str, str_units, dos_units, dos_amt, tvol_amt, tvol_units, dos_form, 
     mo_pstatus, mo_doseunt, xact_dati, user_id, user_name, omni_stid, qty, dose_text, mo_alert, give_amt, give_units, dose_max, rx_suffix, rx_name, rx_disp, duration, 
     interval, stat, only_days, wnd_type, sch_done, use_def, mocf_stat, mocf_user, mocf_dati, admin_dati, adm_dur, adm_site, comp_ct, comp_dati, mocf_inits, mocf_title, 
     ftdue_dati, is_iv, mix_id, mix_type, mo_disp, lu_user, modisp_db, psb_fill, @begindate, @enddate, @type, @hostname, @programname, @loginname, @logintime 
     from inserted 
+1

的索引上定義你的表?什麼說你的執行計劃? –

+0

審計表medorder_audit被索引? –

回答

0

而不是做插入,更新,刪除相同的觸發,你應該在表 使用3種不同的觸發操作,並檢查你的表的索引

相關問題