SQLSERVER 同步更新记录
Posted Airsolstice
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQLSERVER 同步更新记录相关的知识,希望对你有一定的参考价值。
在SQL SERVER 2008以后,存在两种数据库同步策略。分别是Change Trace和Chang Data Capture。这两种策略,各有各的优缺点,适用于不同的场景当中。
CDC是异步获取,是在操作发生,写入日志后触发的监听。记录操作发生的时间,类型和实际影响数据和具体的变化值。而CT是同步获取,在DML提交时就会触发,有点类似于trigger,因此对性能造成一定的影响,但是反应能力更快。客户端通过对比CT记录的版本号,进行数据的更新操作。记录了DML的操作时间,类型和影响的字段。CDC只有相关的存储过程操作,CT有SQL语句操作
1、关于CT的操作记录:
/******* 开启数据库更改跟踪Chang Tracking *******/
--启用更改跟踪(Chang Tracking),天清理一次(HOURS)
ALTER DATABASE my_db
SET CHANGE_TRACKING = ON
(CHANGE_RETENTION = 2 DAYS,
AUTO_CLEANUP = ON)
SELECT DB_NAME(database_id) DataBaseName,is_auto_cleanup_on,
retention_period,retention_period_units_desc
FROM sys.change_tracking_databases
--对表启用更改跟踪
ALTER TABLE [dbo].[test]
ENABLE CHANGE_TRACKING
WITH (TRACK_COLUMNS_UPDATED = ON)
--查看表是否启用更改跟踪
SELECT OBJECT_NAME(object_id) TableName,is_track_columns_updated_on
FROM sys.change_tracking_tables
--插入数据
INSERT INTO test VALUES (1, 'aaa');
INSERT INTO test VALUES (2, 'aaa');
INSERT INTO test VALUES (3, 'aaa');
INSERT INTO test VALUES (4, 'aaa');
INSERT INTO test VALUES (5, 'aaa');
INSERT INTO test VALUES (6, 'aaa');
INSERT INTO test VALUES (7, 'aaa');
INSERT INTO test VALUES (8, 'aaa');
INSERT INTO test VALUES (9, 'aaa');
INSERT INTO test VALUES (10, 'aaa');
--表记录
SELECT * FROM dbo.test;
--当前版本
SELECT CHANGE_TRACKING_CURRENT_VERSION ()
AS CURRENT_VERSION
--最小版本
SELECT CHANGE_TRACKING_MIN_VALID_VERSION
(OBJECT_ID('dbo.Department')) AS MIN_VERSION
--使用Changes关键字查看更改信息
SELECT id,SYS_CHANGE_OPERATION,SYS_CHANGE_VERSION
FROM CHANGETABLE(CHANGES dbo.test, 0) AS CT
--更新数据
UPDATE test SET test.name = 'bbb' WHERE id = 10;
--表记录
SELECT * FROM dbo.test;
--当前版本
SELECT CHANGE_TRACKING_CURRENT_VERSION ()
AS CURRENT_VERSION
--最小版本
SELECT CHANGE_TRACKING_MIN_VALID_VERSION
(OBJECT_ID('dbo.Department')) AS MIN_VERSION
--使用Changes关键字查看更改信息
SELECT id,SYS_CHANGE_OPERATION,SYS_CHANGE_VERSION
FROM CHANGETABLE(CHANGES dbo.test, 0) AS CT
--返回哪些列被修改,1为真,0为假
SELECT id, CHANGE_TRACKING_IS_COLUMN_IN_MASK (COLUMNPROPERTY(OBJECT_ID('dbo.test'),'name','ColumnId'),SYS_CHANGE_COLUMNS) '是否改变name'
FROM CHANGETABLE (CHANGES dbo.test, 10) AS CT WHERE SYS_CHANGE_OPERATION = 'U'
--使用Version关键字查看更改信息
SELECT * FROM dbo.test t CROSS APPLY CHANGETABLE(VERSION dbo.test, (id), (t.id)) AS ct
https://www.cnblogs.com/gaizai/p/3482579.html
/******* 开启数据库更改跟踪Chang Tracking *******/
2、关于CDC的操作记录:
/******* 开启数据库更改捕获Chang Data Capture *******/
USE my_db
GO
--数据库启动CDC
EXECUTE sys.sp_cdc_enable_db
EXEC sys.sp_cdc_enable_db
GO
--启用成功,然后通过以下语句检查是否成功:
SELECT
is_cdc_enabled,
CASE WHEN is_cdc_enabled=0 THEN 'CDC DISABLE' ELSE 'CDC ENABLE' END 描述
FROM sys.databases
WHERE NAME = 'my_db'
--表设置CDC
EXEC sys.sp_cdc_enable_table @source_schema = 'DBO',
@source_name = 'test',@role_name = NULL,@capture_instance = N'dbo_test';
--查询表是否设置成功:
SELECT name ,
is_tracked_by_cdc ,
CASE WHEN is_tracked_by_cdc = 0 THEN 'CDC DISABLE'
ELSE 'CDC ENABLE'
END 描述
FROM sys.tables
WHERE OBJECT_ID = OBJECT_ID('dbo.test')
--查询全部配置
EXECUTE sys.sp_cdc_help_change_data_capture
--查看作业详情
SELECT * FROM msdb.dbo.cdc_jobs
EXEC sp_cdc_help_jobs
--启动和关闭作业
EXEC sys.sp_cdc_stop_job N'cleanup'
EXEC sys.sp_cdc_start_job N'cleanup'
--备份CDC数据库系统表
select * into cdc_captured_columns from cdc.captured_columns
select * into cdc_change_tables from cdc.change_tables
select * into cdc_dbo_Logistics_CT from cdc.dbo_test_CT
select * into cdc_ddl_history from cdc.ddl_history
select * into cdc_index_columns from cdc.index_columns
select * into cdc_lsn_time_mapping from cdc.lsn_time_mapping
--关闭CDC表
DECLARE @from_lsn binary(10), @to_lsn binary(10)
SET @from_lsn =
sys.fn_cdc_get_min_lsn('dbo_test')
SET @to_lsn = sys.fn_cdc_get_max_lsn()
SELECT * FROM cdc.fn_cdc_get_all_changes_dbo_test
(@from_lsn, @to_lsn, N'all update old');
GO
--关闭CDC库,所有CDC相关对象都会删除 包括 cdc的capture和cleanup 作业
EXECUTE sys.sp_cdc_disable_db
--插入数据
INSERT INTO test(id,name,udate) SELECT id + (SELECT COUNT(1) FROM test), name+'1',GETDATE() FROM test;
--监控CDC记录
SELECT * FROM cdc.dbo_test_CT ORDER BY __$start_lsn, __$seqval
SELECT * FROM cdc.ddl_history
http://www.cnblogs.com/lyhabc/p/3383484.html
http://blog.csdn.net/dba_huangzj/article/details/8130448
/******* 开启数据库更改捕获Chang Data Capture *******/
以上是关于SQLSERVER 同步更新记录的主要内容,如果未能解决你的问题,请参考以下文章