开发者

Triggers versus Views: which provides better (row) version control?

We are using "versioned" columns in our order processing system.

+----------------------------+
| ID | VERSION               |
+----+-----------------------+
| 1  | '09-20-2011 12:01 AM' |
+----+-----------------------+
| 1  | '09-20-2011 12:06 AM' |
+----+-----------------------+
| 2  | '09-21-2011 02:01 AM' |
+----+-----------------------+
| 2  | '09-22-2011 10:21 AM' |
+----+-----------------------+

The unique key is a composite key ID+VERSION.

Should we write a view that INNER JOINS all ID's with matching MAX(VERSION)?

Or should we do something like this?

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO

ALTER TRIGGER [dbName].[Trigger_Example] ON [dbName].[Example]
AFTER INSERT, UPDATE
AS BEGIN
--Copy inserted data to Archived_Example
INSERT INTO Archived_Example 
    SELECT ID, CURRENT_TIMESTAMP AS VERSION FROM inserted
END

Which of these two scales best? Which will prove to be less of a headache?

(My first thought: managing the archives on insert will keep the "current" table slim. Managing the "current" table as开发者_StackOverflow a view is...the same? Different?)


As I'm sure you suspect, doing a view will be more resource-intensive, but also much simpler to set up. Doing a trigger is probably the 'right' way if you're concerned about scaling, but can be a pain to get going.

Although if you've already got a trigger working, I would just use that.

EDIT: really if your table is going to get large, a trigger is the only realistic option. If you're only going to have say 20 objects with 10 versions each, you probably won't notice a difference in speed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜