情况:
我一直试图获取一个临时表以在1天的历史记录保留期内自动清除(出于测试目的)。我相信,根据MS的文档,我已经达到了Microsoft的所有标准,可以正常工作,
在系统计划和运行的后台任务中,对匹配行的标识及其从历史表中的删除是透明的。
如您所见,在Query 2: Results
和Query 3: Results
下,历史记录表保留的数据远超过1天标记。临时查询适当地隐藏了保留窗口之外的记录,因此我假设SQL Server正在识别历史记录保留期。
问题:
我是否错过了关键组件,导致历史记录表无法清除?
A。临时表正在清除。
这个神秘的“后台任务”不是每天都会发生吗/有人知道这个任务的频率吗?我尚未通过Bing,Google和StackOverflow搜索找到这些信息。
A。似乎不是日常活动。我已经设置了一个测试环境来推断时间表(请参阅下面的更新)。
╒═════════╗
│ Query 1 ║
├─────────╜
│ --DB and Table retention configurations
│
│ SELECT DB_NAME() as DatabaseName, DB.is_temporal_history_retention_enabled as TemporalHistoryRetentionEnabled
│ , concat(SCHEMA_NAME(T1.schema_id), '.', T1.name) as TemporalTableName
│ , concat(SCHEMA_NAME(T2.schema_id), '.', T2.name) as HistoryTableName
│ , concat(T1.history_retention_period, ' ' , T1.history_retention_period_unit_desc) as HistoryRetentionPeriod
│ FROM sys.tables T1
│ OUTER APPLY (select is_temporal_history_retention_enabled from sys.databases
│ where name = DB_NAME()) AS DB
│ LEFT JOIN sys.tables T2
│ ON T1.history_table_id = T2.object_id WHERE T1.temporal_type = 2
└
╒══════════════════╗
│ Query 1: Results ║
├──────────────────╜
│ DatabaseName TemporalHistoryRetentionEnabled TemporalTableName HistoryTableName HistoryRetentionPeriod
│ ------------ ------------------------------- ----------------- ------------------------- ----------------------
│ Sandbox 1 dbo.TemporalTest3 dbo.TemporalTest3_History 1 DAY
└
╒═════════╗
│ Query 2 ║
├─────────╜
│ --Temporal and Manual *all* records in table and history
│ --Agent runs job every 30 minutes, writes to TemporalCleanupTest
│
│ select 'Temporal' as [Action], *
│ from TemporalTest3
│ for system_time all
│ union
│ select 'Manual' as [Action], *
│ from TemporalTest3
│ union
│ select 'Manual' as [Action], *
│ from TemporalTest3_History
│ order by Action desc, ValidTo desc
└
╒══════════════════╗
│ Query 2: Results ║
├──────────────────╜
│ Action col1 col2 ValidFrom ValidTo
│ -------- ------ ---- --------------------------- ---------------------------
│ Temporal ABC123 3 2019-12-04 22:50:28.4229184 9999-12-31 23:59:59.9999999
│ Manual ABC123 3 2019-12-04 22:50:28.4229184 9999-12-31 23:59:59.9999999
│ Manual ABC123 3 2019-12-04 22:50:22.7708507 2019-12-04 22:50:28.4229184
│ Manual ABC123 3 2019-12-04 22:25:39.5188391 2019-12-04 22:50:22.7708507
│ Manual ABC123 2 2019-12-04 22:25:35.9240760 2019-12-04 22:25:39.5188391
│ Manual ABC123 1 2019-12-04 22:25:31.6265788 2019-12-04 22:25:35.9240760
│ Manual ABC123 NULL 2019-12-04 22:25:22.4114106 2019-12-04 22:25:31.6265788
└
╒═════════╗
│ Query 3 ║
├─────────╜
│ --TemporalCleanupTest: record count by TimeStamp
│
│ select TimeStamp, Method, count(*)
│ from TemporalCleanupTest
│ where TimeStamp = (select max(TimeStamp) from TemporalCleanupTest)
│ group by TimeStamp, Method
│ order by TimeStamp desc, Method desc
└
╒══════════════════╗
│ Query 3: Results ║
├──────────────────╜
│ TimeStamp Method RecordCount
│ ------------------- -------- -----------
│ 2019-12-06 11:30:00 Temporal 1
│ 2019-12-06 11:30:00 Manual 6
└
╒═════════════════╗
│ Required Index: ║
├─────────────────╜
│ /*
│ Object: Index [ix_TemporalTest3_History]
│ The cleanup task for tables with rowstore clustered index requires index to start with
│ the column corresponding the end of SYSTEM_TIME period. If such index doesn't exist,
│ you cannot configure a finite retention period
│ */
│ CREATE CLUSTERED INDEX [ix_TemporalTest3_History] ON [dbo].[TemporalTest3_History]
│ (
│ [ValidTo] ASC, --column corresponding the end of SYSTEM_TIME period
│ [ValidFrom] ASC
│ )
│ WITH
│ (
│ PAD_INDEX = OFF
│ , STATISTICS_NORECOMPUTE = OFF
│ , SORT_IN_TEMPDB = OFF
│ , DROP_EXISTING = OFF
│ , ONLINE = OFF
│ , ALLOW_ROW_LOCKS = ON
│ , ALLOW_PAGE_LOCKS = ON
│ ) ON [PRIMARY]
└
更新2019-12-10 15:23 UTC
您可以共享用于创建表的DDL(我对时间参数感兴趣)吗? – Ben Thul
create table [Sandbox].[dbo].[TemporalTest3]
(
[col1] nvarchar(100)
, constraint [PK_TemporalTest3_col1] primary key clustered ([col1] ASC)
, [col2] nvarchar(100) NULL
, [ValidFrom] datetime2(7) generated always as row start
, [ValidTo] datetime2(7) generated always as row end
, period for system_time ([ValidFrom], [ValidTo])
)
with
(
system_versioning = on
(
history_table = [dbo].[TemporalTest3_History]
, history_retention_period = 1 days
)
)
更新2019-12-10 15:41 UTC 临时表正在清理中,但是我的每小时捕获工作已被IT员工打断了……所以我不知道什么时候会发生清理会。我现在有以下问题:
我将再次设置此测试,并每小时进行一次捕获,并提供我的发现的最新信息。当然,我不是唯一拥有这些数据保留/清除问题的人。 记录清除记录的捕获日志:
┌─────────────────────┬──────────┬────────┬──────┬─────────────────────────┬─────────────────────────┐
│ TimeStamp │ Method │ col1 │ col2 │ ValidFrom │ ValidTo │
├─────────────────────┼──────────┼────────┼──────┼─────────────────────────┼─────────────────────────┤
│ 2019-12-07 06:00:00 │ Temporal │ ABC123 │ 3 │ 2019-12-04 22:50:28.423 │ 9999-12-31 23:59:59.999 │
│ 2019-12-07 06:00:00 │ Manual │ ABC123 │ 3 │ 2019-12-04 22:50:28.423 │ 9999-12-31 23:59:59.999 │
│ ******************* │ ******** │ ****** │ **** │ *********************** │ *********************** │
│ 2019-12-06 17:00:00 │ Temporal │ ABC123 │ 3 │ 2019-12-04 22:50:28.423 │ 9999-12-31 23:59:59.999 │
│ 2019-12-06 17:00:00 │ Manual │ ABC123 │ 3 │ 2019-12-04 22:50:28.423 │ 9999-12-31 23:59:59.999 │
│ 2019-12-06 17:00:00 │ Manual │ ABC123 │ 3 │ 2019-12-04 22:50:22.771 │ 2019-12-04 22:50:28.423 │
│ 2019-12-06 17:00:00 │ Manual │ ABC123 │ 3 │ 2019-12-04 22:25:39.519 │ 2019-12-04 22:50:22.771 │
│ 2019-12-06 17:00:00 │ Manual │ ABC123 │ 2 │ 2019-12-04 22:25:35.924 │ 2019-12-04 22:25:39.519 │
│ 2019-12-06 17:00:00 │ Manual │ ABC123 │ 1 │ 2019-12-04 22:25:31.627 │ 2019-12-04 22:25:35.924 │
│ 2019-12-06 17:00:00 │ Manual │ ABC123 │ NULL │ 2019-12-04 22:25:22.411 │ 2019-12-04 22:25:31.627 │
└─────────────────────┴──────────┴────────┴──────┴─────────────────────────┴─────────────────────────┘
答案 0 :(得分:0)
后台任务确实是在某种松散的“ Microsoft质量计划”下进行的。大多数清除事件都发生在一天的开始时间(00:00)和中午(12:00)左右,尽管有些发生在其他时间。我不知道为什么要花时间开始清理时间历史,但是它正在起作用。我已经从每小时的日志中提取了发生更改的数据到下表中。享受吧!
如果需要进一步的数据,请告诉我,我将尽力提供。
np.float64