39

我有 2 张桌子,Table-A并且Table-A-History.

  • Table-A包含当前数据行。
  • Table-A-History包含历史数据

我想在 中包含我的数据的最新行Table-A,并Table-A-History包含历史行。

我可以想到两种方法来实现这一点:

  1. 每当有新数据行可用时,将当前行从Table-A移至Table-A-History并使用最新数据更新该Table-A行(通过insert into selectselect into table

    或者

  2. 每当有新数据行可用时,更新Table-A的行并将新行插入Table-A-History.

关于性能,方法 1 还是 2 更好?有没有更好的不同方法来实现这一点?

4

8 回答 8

65

基本上,您希望跟踪/审核对表的更改,同时保持主表的大小。

有几种方法可以解决这个问题。下面讨论每种方式的优缺点。

1 -使用触发器审计表。

如果您要审核表(插入、更新、删除),请查看我的如何取消不需要的事务 - SQL 星期六幻灯片,带代码 - http://craftydba.com/?page_id=880。如果您选择,填充审计表的触发器可以保存来自多个表的信息,因为数据保存为 XML。因此,如有必要,您可以通过解析 XML 来取消删除操作。它跟踪谁和什么做出了改变。

或者,您可以将审计表放在它自己的文件组上。

Description:
    Table Triggers For (Insert, Update, Delete)
    Active table has current records.
    Audit (history) table for non-active records.

Pros:
    Active table has smaller # of records.
    Index in active table is small.
    Change is quickly reported in audit table.
    Tells you what change was made (ins, del, upd)

Cons:
    Have to join two tables to do historical reporting.
    Does not track schema changes.

2 -有效约会记录

如果您永远不会从审计表中清除数据,为什么不将该行标记为已删除,而是永远保留它呢?许多系统,如 people soft 使用有效约会来显示记录是否不再有效。在 BI 世界中,这称为类型 2 维表(缓慢变化的维度)。请参阅数据仓库研究所的文章。http://www.bidw.org/datawarehousing/scd-type-2/每条记录都有一个开始和结束日期。

所有活动记录的结束日期均为空。

Description:
    Table Triggers For (Insert, Update, Delete)
    Main table has both active and historical records.

Pros:
    Historical reporting is easy.
    Change is quickly shown in main table.

Cons:
    Main table has a large # of records.
    Index of main table is large.
    Both active & history records in same filegroup.
    Does not tell you what change was made (ins, del, upd)
    Does not track schema changes.

3 -更改数据捕获(企业功能)。

Micorsoft SQL Server 2008 引入了变更数据捕获功能。虽然这会在事后使用 LOG 阅读器跟踪数据更改 (CDC),但它缺少诸如谁和什么进行了更改之类的信息。MSDN 详细信息 - http://technet.microsoft.com/en-us/library/bb522489(v=sql.105).aspx

此解决方案取决于运行的 CDC 作业。sql 代理的任何问题都会导致数据显示延迟。

请参阅变更数据捕获表。 http://technet.microsoft.com/en-us/library/bb500353(v=sql.105).aspx

Description:
    Enable change data capture

Pros:
    Do not need to add triggers or tables to capture data.
    Tells you what change was made (ins, del, upd) the _$operation field in 
    <user_defined_table_CT>
    Tracks schema changes.    

Cons:
    Only available in enterprise version.
    Since it reads the log after the fact, time delay in data showing up.
    The CDC tables do not track who or what made the change.
    Disabling CDC removes the tables (not nice)!
    Need to decode and use the _$update_mask to figure out what columns changed.

4 -更改跟踪功能(所有版本)。

Micorsoft SQL Server 2008 引入了更改跟踪功能。与 CDC 不同,它带有所有版本;但是,它带有一堆 TSQL 函数,您必须调用它们才能弄清楚发生了什么。

它旨在通过应用程序将一个数据源与 SQL 服务器同步。TechNet 上有一个完整的同步框架工作。

http://msdn.microsoft.com/en-us/library/bb933874.aspx http://msdn.microsoft.com/en-us/library/bb933994.aspx http://technet.microsoft.com/en-我们/图书馆/bb934145(v=sql.105).aspx

与 CDC 不同,您指定在清除之前数据库中的更改持续多长时间。此外,插入和删除不记录数据。更新仅记录更改了哪些字段。

由于您正在将 SQL Server 源同步到另一个目标,因此可以正常工作。除非您编写定期作业以找出更改,否则这对审计不利。

您仍然必须将该信息存储在某处。

Description:
    Enable change tracking

Cons:
    Not a good auditing solution

前三个解决方案将适用于您的审计。我喜欢第一个解决方案,因为我在我的环境中广泛使用它。

真挚地

约翰

演示文稿中的代码片段(汽车数据库)

-- 
-- 7 - Auditing data changes (table for DML trigger)
-- 


-- Delete existing table
IF OBJECT_ID('[AUDIT].[LOG_TABLE_CHANGES]') IS NOT NULL 
  DROP TABLE [AUDIT].[LOG_TABLE_CHANGES]
GO


-- Add the table
CREATE TABLE [AUDIT].[LOG_TABLE_CHANGES]
(
  [CHG_ID] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
  [CHG_DATE] [datetime] NOT NULL,
  [CHG_TYPE] [varchar](20) NOT NULL,
  [CHG_BY] [nvarchar](256) NOT NULL,
  [APP_NAME] [nvarchar](128) NOT NULL,
  [HOST_NAME] [nvarchar](128) NOT NULL,
  [SCHEMA_NAME] [sysname] NOT NULL,
  [OBJECT_NAME] [sysname] NOT NULL,
  [XML_RECSET] [xml] NULL,
 CONSTRAINT [PK_LTC_CHG_ID] PRIMARY KEY CLUSTERED ([CHG_ID] ASC)
) ON [PRIMARY]
GO

-- Add defaults for key information
ALTER TABLE [AUDIT].[LOG_TABLE_CHANGES] ADD CONSTRAINT [DF_LTC_CHG_DATE] DEFAULT (getdate()) FOR [CHG_DATE];
ALTER TABLE [AUDIT].[LOG_TABLE_CHANGES] ADD CONSTRAINT [DF_LTC_CHG_TYPE] DEFAULT ('') FOR [CHG_TYPE];
ALTER TABLE [AUDIT].[LOG_TABLE_CHANGES] ADD CONSTRAINT [DF_LTC_CHG_BY] DEFAULT (coalesce(suser_sname(),'?')) FOR [CHG_BY];
ALTER TABLE [AUDIT].[LOG_TABLE_CHANGES] ADD CONSTRAINT [DF_LTC_APP_NAME] DEFAULT (coalesce(app_name(),'?')) FOR [APP_NAME];
ALTER TABLE [AUDIT].[LOG_TABLE_CHANGES] ADD CONSTRAINT [DF_LTC_HOST_NAME] DEFAULT (coalesce(host_name(),'?')) FOR [HOST_NAME];
GO



--
--  8 - Make DML trigger to capture changes
--


-- Delete existing trigger
IF OBJECT_ID('[ACTIVE].[TRG_FLUID_DATA]') IS NOT NULL 
  DROP TRIGGER [ACTIVE].[TRG_FLUID_DATA]
GO

-- Add trigger to log all changes
CREATE TRIGGER [ACTIVE].[TRG_FLUID_DATA] ON [ACTIVE].[CARS_BY_COUNTRY]
  FOR INSERT, UPDATE, DELETE AS
BEGIN

  -- Detect inserts
  IF EXISTS (select * from inserted) AND NOT EXISTS (select * from deleted)
  BEGIN
    INSERT [AUDIT].[LOG_TABLE_CHANGES] ([CHG_TYPE], [SCHEMA_NAME], [OBJECT_NAME], [XML_RECSET])
    SELECT 'INSERT', '[ACTIVE]', '[CARS_BY_COUNTRY]', (SELECT * FROM inserted as Record for xml auto, elements , root('RecordSet'), type)
    RETURN;
  END

  -- Detect deletes
  IF EXISTS (select * from deleted) AND NOT EXISTS (select * from inserted)
  BEGIN
    INSERT [AUDIT].[LOG_TABLE_CHANGES] ([CHG_TYPE], [SCHEMA_NAME], [OBJECT_NAME], [XML_RECSET])
    SELECT 'DELETE', '[ACTIVE]', '[CARS_BY_COUNTRY]', (SELECT * FROM deleted as Record for xml auto, elements , root('RecordSet'), type)
    RETURN;
  END

  -- Update inserts
  IF EXISTS (select * from inserted) AND EXISTS (select * from deleted)
  BEGIN
    INSERT [AUDIT].[LOG_TABLE_CHANGES] ([CHG_TYPE], [SCHEMA_NAME], [OBJECT_NAME], [XML_RECSET])
    SELECT 'UPDATE', '[ACTIVE]', '[CARS_BY_COUNTRY]', (SELECT * FROM deleted as Record for xml auto, elements , root('RecordSet'), type)
    RETURN;
  END

END;
GO



--
--  9 - Test DML trigger by updating, deleting and inserting data
--

-- Execute an update
UPDATE [ACTIVE].[CARS_BY_COUNTRY]
SET COUNTRY_NAME = 'Czech Republic'
WHERE COUNTRY_ID = 8
GO

-- Remove all data
DELETE FROM [ACTIVE].[CARS_BY_COUNTRY];
GO

-- Execute the load
EXECUTE [ACTIVE].[USP_LOAD_CARS_BY_COUNTRY];
GO 

-- Show the audit trail
SELECT * FROM [AUDIT].[LOG_TABLE_CHANGES]
GO

-- Disable the trigger
ALTER TABLE [ACTIVE].[CARS_BY_COUNTRY] DISABLE TRIGGER [TRG_FLUID_DATA];

** 审计表的外观 **

在此处输入图像描述

于 2013-10-02T18:42:25.667 回答
28

记录更改是我通常使用基表上的触发器来记录日志表中的更改。日志表有额外的列来记录数据库用户、操作和日期/时间。

create trigger Table-A_LogDelete on dbo.Table-A
  for delete
as
  declare @Now as DateTime = GetDate()
  set nocount on
  insert into Table-A-History
    select SUser_SName(), 'delete-deleted', @Now, *
      from deleted
go
exec sp_settriggerorder @triggername = 'Table-A_LogDelete', @order = 'last', @stmttype = 'delete'
go
create trigger Table-A_LogInsert on dbo.Table-A
  for insert
as
  declare @Now as DateTime = GetDate()
  set nocount on
  insert into Table-A-History
    select SUser_SName(), 'insert-inserted', @Now, *
      from inserted
go
exec sp_settriggerorder @triggername = 'Table-A_LogInsert', @order = 'last', @stmttype = 'insert'
go
create trigger Table-A_LogUpdate on dbo.Table-A
  for update
as
  declare @Now as DateTime = GetDate()
  set nocount on
  insert into Table-A-History
    select SUser_SName(), 'update-deleted', @Now, *
      from deleted
  insert into Table-A-History
    select SUser_SName(), 'update-inserted', @Now, *
      from inserted
go
exec sp_settriggerorder @triggername = 'Table-A_LogUpdate', @order = 'last', @stmttype = 'update'

日志记录触发器应始终设置为最后触发。否则,后续触发器可能会回滚原始事务,但日志表已经更新。这是一种令人困惑的状况。

于 2012-08-09T20:22:12.187 回答
28

最新版本的 SQL Server(2016+ 和 Azure)具有临时表,可提供所需的确切功能,作为一流的功能。 https://docs.microsoft.com/en-us/sql/relational-databases/tables/temporal-tables

Microsoft 的某个人可能会阅读此页面。:)

于 2017-05-02T02:17:58.487 回答
4

方法 3 怎么样:Table-ATable-A-History. 插入Table-A-History并让适当的过滤逻辑生成Table-A。这样你只插入一个表。

于 2012-08-09T19:51:01.397 回答
4

尽管它会占用更多空间,但拥有包含最新记录的历史记录表也会让您省去编写报告和查看更改是如何发生的以及何时发生的痛苦。在我看来,一些值得思考的事情。

就性能而言,我希望它们是相同的。但是,您肯定不想从非历史表中删除记录(选项 1 的“移动”),因为您在两个表之间使用了参照完整性,对吗?

于 2012-08-09T20:00:14.443 回答
2

我更喜欢方法1
此外,我还将在历史表中维护当前记录,
这取决于需要。

于 2012-08-09T20:00:36.187 回答
2

选项 1 没问题。但是你也有方法4:)

  1. 将新记录插入到您的表中,

  2. 使用 mysql 调度程序将旧记录移至常规基础上的存档表。您可以在负载最小的时间安排数据归档,例如在夜间。

于 2012-08-09T20:13:02.823 回答
0

您可以像这样简单地创建过程或作业来克服此问题:

 create procedure [dbo].[sp_LoadNewData]
 AS
INSERT INTO [dbo].[Table-A-History]
 (
 [1.Column Name], [2.Column Name], [3.Column Name], [4.Column Name]
 )    
 SELECT [1.Column Name], [2.Column Name], [3.Column Name], [4.Column Name]
 FROM dbo.[Table-A] S

 WHERE NOT EXISTS
 (
 SELECT  * FROM [dbo].[Table-A-History] D WHERE D.[1.Column Name] =S.[1.Column Name]
 )

注意:[1.Column Name]是表格的通用列。

于 2020-10-23T14:35:23.543 回答