简体   繁体   English

如何捕获SQL Server 2008 R2中的表级数据更改?

[英]How to capture table level data changes in SQL Server 2008 R2?

I have high volume of data normalized into more than 100 tables. 我将大量数据标准化为100多个表。 There are multiple applications which change underlying data in those tables and I want to raise events on those changes. 有多个应用程序会更改这些表中的基础数据,而我想引发有关这些更改的事件。 Possible options that I know of are: 我知道的可能选项是:

  • Change Data Capture 更改数据捕获
  • Change Tracking 变更追踪
  • Using Triggers on each table (bad option but possible) 在每个表上使用触发器(错误的选项,但可能)

Can someone share the best way of doing this if someone has already done this before? 如果有人以前已经做到过,那么有人可以分享这样做的最佳方式吗?

What I really want in the end is if there is one transaction that affected 12 tables off 100 I should be able to bubble one event up instead of 12. Assume there are concurrent users change these tables. 最后,我真正想要的是,如果有一笔事务影响了100张表中的12张,我应该能够冒起一个事件,而不是12张。假设有并发用户更改这些表。

As long as you want to 'batch' multiple changes, I think you should follow the route of Change Data Capture or Change Tracking (depending on whether you just want to know that something changed or what changes happened). 只要您想“批处理”多个更改,我认为您应该遵循“ 更改数据捕获”或“ 更改跟踪”的路线(取决于您是否只想知道某些更改或发生了什么更改)。

They should be used by a 'polling' procedure, where you poll for changes every few minutes (seconds, miliseconds???) and raise events. 它们应由“轮询”过程使用,在此过程中,您每隔几分钟(几秒钟,几毫秒???)轮询更改并引发事件。 The nice thing about this is that as long as you store the last rowversion of the previous poll -for each table- you can check whenever you like for changes since the last poll. 这样做的好处是,只要存储上一个轮询的最后一个行版本(针对每个表),您就可以随时检查自上次轮询以来的更改。 You don't rely on a real time triggers approach, that if halted you would loose all events forever. 您不必依赖实时触发方法,如果停止触发,则将永远失去所有事件。 The procedure could be easily created inside a procedure that checks each table and you would need only 1 more table to store last rowversion per table. 该过程可以在检查每个表的过程中轻松创建,您只需要再有1个表即可存储每个表的最后一个rowversion。

Also, the overhead of this approach would be controlled by you and by how frequently the polling happens. 同样,此方法的开销将由您以及轮询发生的频率来控制。

Two options I can think of: 我可以想到两个选择:

  1. Triggers ARE the right way to capture change events in the DB layer 触发器是在DB层中捕获变更事件的正确方法
  2. Codewise, I make sure in my app that each table is changed through only one place in the code, regardless what the change is (I call it a hub for that table, as it channels many different pathways into one place), it becomes very easy to catch change events that way in the code layer 从代码角度来说,我确保在应用程序中每个表仅在代码中仅更改了一个位置,无论更改是什么(我称其为该表的枢纽,因为它将许多不同的路径引导到一个位置),它变得非常易于在代码层中以这种方式捕获更改事件

一种可能是SQL Server查询通知: 使用查询通知

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM