views:

53

answers:

3

Is there a way of finding all the rows that have been updated by a single statement, sql itself must be tracking this as it could roll back the update if required. I'm interested in finding all the changed rows as I'm getting performance hit using update triggers.

I have a some large (2M-10M) row tables in Sql Server, and I'm adding audit triggers to track when records are updated and by what, trouble is this is killing performance. Most of the updates against the table will touch 20,000+ rows and they're now taking 5-10 times longer than previously.

I've thought of some options

1) Ditch triggers entirely and add the audit fields to every update statement, but that relies on everyone's code being changed. 2) Use before/after checksum values on the fields and then use them to update the changed rows a second time, still a performance hit.

Has anyone else solved this problem?

+2  A: 

SqlServer knows how to rollback because it has the transaction log. Is not something that you can find in the data tables.

You can try to add a timestamp column to your rows, then save a "current" timestamp, update all the rows. The changed rows should be all the rows with the timestamp greater than your "current" timestamp. THis will help you to find the changed rows, but not to find what has changed them.

munissor
Just to make it clear, @munissor is referring to the timestamp *data type*, and not any of the date/time datatypes.
Philip Kelley
A: 

An UPDATE trigger already has the records affected by an update statement in the inserted and deleted pseudo columns. You can select their primary key columns into a preliminary audit table serving as a queue, and move more complicated calculation into a separate job.

Another option is the OUTPUT clause for the UPDATE statement, which was introduced in SQL Server 2005. (updated after comment by Philip Kelley)

devio
It may be updated or revised in 2008, but OUTPUT was added to SQL 2005.
Philip Kelley
Phillip and Devio I think the Output clause is the exact answer to what I'm looking for. I'll investigate performance and report back.
MrTelly
+1  A: 

You can use Change Tracking or Change Data Capture. These are technologies built into the Engine for tracking changes and are leveraging the Replication infrastructure (log reader or table triggers). Both are only available in SQL Server 2008 or 2008 R2 and CDC requires Enterprise Edition licensing.

Anything else you'd try to do would ultimately boil down to either one of:

  • reading the log for changes (which is only doable by Replication, including Change Data Capture, otherwise the Engine will recycle the log before you can read it)
  • track changes in triggers (which is what Change Tracking would use)
  • track changes in application

There just isn't any Free Lunch. If audit is a requirement, then the overhead of auditing has to be taken into consideration and capacity planning must be done accordingly. All data audit solution will induce significant overhead, so the an increase of operating cost by factors of 2x, 4x or even 10x are not unheard of.

Remus Rusanu