Skip to content

shannonlowder.com

Menu
  • About
  • Biml Interrogator Demo
  • Latest Posts
Menu

SQL103 – Designing Triggers

Posted on October 17, 2011October 17, 2011 by slowder

We’ve finally made it to the last post in the SQL103 series.  I’ve written posts about triggers before.  I’ve shared with you how triggers can be good things and bad things.  It’s all in how you design them.  Today, I hope to teach you how you can design good triggers.  Let’s dive right in!

Understand There are Two Types of Triggers

There are two types of triggers available to you in SQL Server.  You’ve got DML (data manipulation Language) triggers these are the triggers most people think of when you start talking about triggers.  These triggers happen when you do an insert, update or delete (any query that would change the data in your database).

Then you have DDL (data definition Language) triggers, these fire when you make changes to the database itself.  These can fire when you add, alter, or drop a table.  These are useful, but you’ve really got to plan out how you want to respond to these actions before you start building.

You Want Triggers To Be Simple

When you design your triggers you want them to do simple things, usually a single DML statement.  You do not want them to call a stored procedure that starts a job, that scans your largest table, then summarizes the data for every customer, and then write that to another server on the other side of the planet through a linked server connection.  You have to remember to keep the body of your triggers simple, because whatever code you put into the trigger will fire every time the event happens that you’ve added that trigger on.

Understand the Trigger Fires For the Batch

One of the biggest mistakes I see in trigger design is they’re built under the assumption that only one record is hitting the trigger.  You must never assume that!  Always assume that you’re going to get more than one record firing your trigger.  Get an idea of how many records are updated in each batch before you implement your trigger.  Test the code you’re going to put into the trigger with batches of that size.  Make sure your code runs quickly, if it doesn’t, you’re going to be slowing down your system when you add the trigger.

Speaking of Testing

If you’re adding an update trigger to a table, get a baseline on how long update statements take before you add the trigger.  Then when you add your trigger, test it again.  How much impact did your trigger make on the update times.  If it’s noticeable, you want to revisit your code, and consider a change.

Look at the execution plan of your update before and after you add the trigger.  See how it’s changed.  Is the performance impact acceptable?  If not, revisit your trigger code.

Don’t Avoid Triggers, Embrace Them!

If you’ve ever read any of the Star Wars novels that are set after Return of the Jedi, you know that Luke learns something very important about The Force, there’s no Dark Side or Light.  There is simply how you use The Force. The Force itself doesn’t know good or evil.  It’s up to the wielder. If someone uses some Force lightning to fire up an electric generator and keep a hospital full of sick children running, that’s not Dark Side, even though he used a Dark Side power to save the day.

It’s the same with triggers, they’re not good or evil.  It’s all in the implementation.  If you make sure your trigger does exactly what you need it to do (and nothing more), and you minimize your impact on performance, they can be very good things.

But avoiding triggers can be worse than using them.  I’ve seen cases where application programmers put code into the user interface to handle archiving change data.  The performance was horrible.  I was actually able to help them add triggers to their database and speed up their application’s performance!  Since we let the database do what it was best at doing, and then let the user interface code do what it was best at, the whole system sped up.

And that’s what database design is all about.  Let the database do what it does best, and nothing more.  Always delegate functionality when it makes sense.

Well, that’s it for database design.  If you have any questions, send them in.  I’m here to help.  By the way, I’m going to be presenting this class at SQL Saturday #87, in Louisville, KY at the end of the month.  Come and check it out!

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • A New File Interrogator
  • Using Generative AI in Data Engineering
  • Getting started with Microsoft Fabric
  • Docker-based Spark
  • Network Infrastructure Updates

Recent Comments

  1. slowder on Data Engineering for Databricks
  2. Alex Ott on Data Engineering for Databricks

Archives

  • July 2023
  • June 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • October 2018
  • August 2018
  • May 2018
  • February 2018
  • January 2018
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • June 2017
  • March 2017
  • February 2014
  • January 2014
  • December 2013
  • November 2013
  • October 2013
  • August 2013
  • July 2013
  • June 2013
  • February 2013
  • January 2013
  • August 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • August 2005
  • July 2005
  • June 2005
  • May 2005
  • April 2005
  • March 2005
  • February 2005
  • January 2005
  • November 2004
  • September 2004
  • August 2004
  • July 2004
  • April 2004
  • March 2004
  • June 2002

Categories

  • Career Development
  • Data Engineering
  • Data Science
  • Infrastructure
  • Microsoft SQL
  • Modern Data Estate
  • Personal
  • Random Technology
  • uncategorized
© 2025 shannonlowder.com | Powered by Minimalist Blog WordPress Theme