Bloggin in the UK RSS 2.0
 Friday, April 01, 2005

We use a set of three triggers to Audit all Insert, Update and Delete activity on every table in our database. We have a piece of SQL that auto generates trigger create scripts for each table automatically. This means that every time we make a structural alteration to a table we must re-run the script for that table and update the triggers as part of the release.

In the tables with lots of columns we noticed poor performance during updates, looking in Query Analyser at the Execution Plan revealed that for each update there were multiple scans of the Inserted and the Deleted pseudo tables.

This was happening because the Update Audit trigger had an Insert statement in it for each column in the table. The Insert statement caused the scan of the Inserted and Deleted pseudo tables. I remembered reading in the books online that you can use IF UPDATE (column) to test if a column has been updated, by placing this statement around each Insert statement we were able to greatly increase the speed with which the trigger operates, because the Inserted/Deleted scans were only happening for the columns that had been updated.

I dedicate this blog post to Kimberly Tripp's interview on .Net Rocks, it was a great show full of invaluable SQL Server performance tuning advice.

 

 

Friday, April 01, 2005 3:43:23 PM (GMT Standard Time, UTC+00:00)  #    Comments [2] -

Saturday, May 28, 2005 9:20:40 PM (GMT Standard Time, UTC+00:00)
Not quite. The UPDATE doesn't work quite as expected. You would think it meant the column data actually changed, but that is not the case.

From microsoft support: [Doc] states that IF UPDATE is used to test whether the specified column has been modified. IF UPDATE actually returns true whenever the specified column is included in an update statement.

http://support.microsoft.com/default.aspx?scid=kb;en-us;64238


They suggest adding
"and (select count(*) from inserted) > 0".
Monday, May 30, 2005 5:10:46 PM (GMT Standard Time, UTC+00:00)
Thanks for picking me up on that, my original post is misleading. The behaviour of IF UPDATE still works for us because we wanted to prevent the SQL that was testing to see if a column had been altered from executing unless the column had changed. We were seeing the biggest performance hit with tables that had many columns because the pseudo table scans were taking the most time. IF UPDATE is the only way I have found of preventing the pseudo table scans.

Cheers,

Charlie
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

Live Comment Preview
Archive
<July 2010>
SunMonTueWedThuFriSat
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010
Charlie Barker
Sign In
Statistics
Total Posts: 169
This Year: 11
This Month: 2
This Week: 0
Comments: 77
Themes
Pick a theme:
All Content © 2010, Charlie Barker
DasBlog theme 'Business' created by Christoph De Baene (delarou)