2

在我们的数据库级应用程序中,我在模式 Billing 和 Billing_History 中有一个名为 Installments 的表。

显示的触发器位于计费模式中的分期付款表上。

这样做是每次在计费模式中插入/更新记录时,它也会写入历史文件。

如果从计费表中删除记录,则将其写入历史表,并带有“已删除”指示符 = true。

我认为随着更多记录的添加,“如果不存在(从插入中选择 *)正在扼杀我的表现。

有没有更有效的方法是写这个触发器?

Create TRIGGER [Billing].[Installments_InsertDeleteUpdate_History]
ON [Billing].[Installments]
AFTER INSERT, DELETE, UPDATE
AS BEGIN
Insert Into Billing_History.Installments
    Select *, GetDate(), 0 From Inserted

If Not Exists (Select * From Inserted)
    Insert Into Billing_History.Installments
        Select *, GetDate(), 1 From Deleted

SET NOCOUNT ON;

-- Insert statements for trigger here

结尾

4

3 回答 3

1

I would suggest that the trigger form you have is the best performing, given it's required tasks. There really aren't much better ways to achieve the same auditing result.

The answer here would agree Creating audit triggers in SQL Server and here's a long discussion about performance of audit solutions.

Your situation is slightly different, because you actually DON'T want the deleted (original) table in UPDATE situations, hence the IF.

于 2012-09-27T20:57:46.400 回答
0

为 INSERT 和 UPDATE 创建一个触发器,为 DELETE 创建第二个触发器。然后,您不必使用 IF 语句和慢速查询来检查记录的位置。

From a design perspective, see if you can eliminate triggers. They're a mess.

于 2012-09-27T20:27:45.330 回答
0

Well you could make this simple change:

Create TRIGGER [Billing].[Installments_InsertDeleteUpdate_History]
ON [Billing].[Installments]
AFTER INSERT, DELETE, UPDATE
AS BEGIN

If Not Exists (Select * From Inserted)
    Insert Into Billing_History.Installments
        Select *, GetDate(), 1 From Deleted
ELSE
    Insert Into Billing_History.Installments
        Select *, GetDate(), 0 From Inserted

SET NOCOUNT ON;

-- Insert statements for trigger here

Which is logically more efficient, but whether it's physically more performant is an open question. If it is actually faster, it sure won't be by very much.

于 2012-09-27T21:47:10.303 回答