OK, from experience, knowledge and docs...

The code that is included inside an UPDATE trigger runs every time its
related table is updated. In most UPDATE triggers, the code in the trigger
affects only certain columns, not all of them. Because of this, it is
pointless (and a waste of SQL Server resources) to run all of the code in
the trigger if the column or columns you are interested in have not been
updated. In other words, even if a column you are not interested in is
updated, the UPDATE trigger will fire and run its code.

To help reduce the unnecessary running of code in an UPDATE trigger, you can
take advantage of one of two different functions: UPDATE() (available in SQL
Server 2000) or COLUMNS_UPDATED() (available in SQL Server 7.0 and 2000).

Either function can be used to test to see if a particular column you are
interested in has changed or not. Because of this, you can write code in
your trigger to only run if the column you are interested in has changed,
otherwise you can prevent the code from running if the column you are
interested in has not changed. This can reduce the amount of work the
trigger needs to do, boosting overall performance of your database.

The UPDATE() function is used to check only one column at a time. The
COLUMNS_UPDATED() function can be used to check multiple columns at a time.

If  you must use triggers, keep em small. The amount of time that a trigger
takes to run is mostly a function of the number of tables referenced in the
trigger and the number of rows affected by the code inside the trigger.
Because of this, always try to minimize the number of tables referenced in a
trigger, and minimize the number of rows being affected.

In addition, keep the code in your triggers to the very minimum to reduce
overhead. This is important because triggers typically fire during INSERTs,
UPDATEs, and DELETEs, all of which can be common occurrences in OLTP
applications. The more code that runs in the trigger, the slower each
INSERT, UPDATE, and DELETE that fires it will be.

If you need to implement cascading referential integrity (such as cascading
deletes) in your SQL 2000 databases, use the cascading referential integrity
constraint instead of triggers to perform the cascading delete, as they are
much more efficient and can boost performance. If you have an older (7.0 and
older) application that you have moved to SQL Server 2000, and it used
triggers to perform cascading deletes, consider removing the triggers and
using cascading referential integrity instead.

Don't use a trigger to enforce referential integrity if you have the option
to use SQL Server's built-in referential integrity instead. Using SQL
Server's built-in referential integrity is much faster than using a trigger
to perform the same task.

If you have a choice between using a trigger or a CHECK constraint to
enforce rules or defaults within your SQL Server databases, you will
generally want to choose a CHECK constraint as they are faster than using
triggers when performing the same task.

Try to avoid rolling back triggers because of the overhead involved. Instead
of letting the trigger find a problem and rolling back a transaction,
instead catch the error before it can get to the trigger (if possible based
on your code). Catching an error early (before the trigger fires) consumes
much fewer server resources than letting the trigger roll back.

Just a few snippets.


-----Original Message-----
From: Ken Ferguson [mailto:[EMAIL PROTECTED] 
Sent: 18 April 2005 15:13
To: CF-Talk
Subject: RE: SQL Triggers (was Re: Last ID inserted)

Just in case my thinking has been wrong for the last few years, how are
triggers a performance drain??? If I can make one query to a database
and then have the db handle some of the other operations I need to run,
that sounds like a pretty efficient way to handle it to me.

--Ferg

This e-mail is from Reed Exhibitions (Oriel House, 26 The Quadrant,
Richmond, Surrey, TW9 1DL, United Kingdom), a division of Reed Business,
Registered in England, Number 678540.  It contains information which is
confidential and may also be privileged.  It is for the exclusive use of the
intended recipient(s).  If you are not the intended recipient(s) please note
that any form of distribution, copying or use of this communication or the
information in it is strictly prohibited and may be unlawful.  If you have
received this communication in error please return it to the sender or call
our switchboard on +44 (0) 20 89107910.  The opinions expressed within this
communication are not necessarily those expressed by Reed Exhibitions.
Visit our website at http://www.reedexpo.com

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Find out how CFTicket can increase your company's customer support 
efficiency by 100%
http://www.houseoffusion.com/banners/view.cfm?bannerid=49

Message: http://www.houseoffusion.com/lists.cfm/link=i:4:203238
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/4
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:4
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54

Reply via email to