Performance question on triggers

From: Bernd Maierhofer \(dato\) (bernd.maierhofer_at_dato.at)
Date: 04/07/04


Date: Wed, 7 Apr 2004 14:38:21 +0200

Hi and thanks for reading:

MSSQL 2000 Given a table

Table A
id integer
x,a,b,c float

x is calclulated from the values a,b,c of the rows < id id is the primary
key

Up to now I calculated x at runtime using a function:

select id,a,b,c,f_func(id) as x f_func(id) basically is a select sum(a)
where id < @id

But as the table grows, this is too slow. As changes to a,b,c usually occur
for the newest records only, I wanted to switch to using a field x wich is
only calculated when a,b,c for one of the preceeding rows is changed. Thus
by using a trigger:

create trigger xx on a
after insert, update, delete as

set nocount off
IF ( (SELECT trigger_nestlevel( object_ID('lsmp') ) ) = 1 ) begin
  update a set x = f_func(id) where id in (select id from deleted)
  update a set x = f_func(id) where id in (select id from inserted)
end
set nocount on
end

The performance is very poor, as the trigger is fired recursively. One
option is to disable recursive and/or nested triggers. Is it possible to
issue a

sp_configure 'nested_triggers','0' within the trigger? And would this
inhibit firering the trigger each time, I update the table?

Any idea? Any different approach?

TIA
Bernd



Relevant Pages

  • Re: Whats wrong with this trigger
    ... I actually had SET NOCOUNT OFF at the top, ... >>table that has the trigger is control data, so the results that are moved ... >>would scrap the Access app at this point, but I don't have time for the ... and the app that uses this data is .NET. ...
    (microsoft.public.access.adp.sqlserver)
  • Re: Edit conflicts between table and trigger
    ... Failing to issue SET NOCOUNT ON results in ADP's confusing the row count message as the result set. ... make sure the trigger does a 'set nocount on' first thing ... I am in the process of migrating a very complex mdb/mde to ADP. ... stage being migration of the backend and some core functionaility to MSDE/mde and the second migrating the remaining ...
    (microsoft.public.access.adp.sqlserver)
  • Can i reuse the code in triggers??
    ... each of them has a Delete trigger ... Delete rr from ResourceRating rr ... Set NoCount Off ... W. Jordan ...
    (microsoft.public.sqlserver.programming)
  • Re: SET NOCOUNT ON - am I doing this wrong?
    ... > place it in the trigger if you also had to place it around every call ... by running a set nocount on one time when you connect to the server. ... create trigger trigtest_ins on trigtest ... exec trigtest_sp ...
    (microsoft.public.sqlserver.server)
  • Re: Fehlerbehandlung
    ... Den Fehler aus dem Trigger kannst Du auch ohne PRobleme aus dem gewöhnlichen ... SET NOCOUNT OFF ... Fehlermeldung in ACCESS angezeigt wird. ...
    (microsoft.public.de.access.clientserver)