Deleted and Inserted On A Trigger

  • Lets Say I have a table with 500K of rows

    I have an Update a Query that update 30k of the rows

    I have a trigger that monitor changes in the fileds by making a join

    between Inserted and deleted

    Insert Arc (key,oldv,newv,ChangeDate)

    Select D.Key , D.sex , I.sex ,GetDate())

    From

    Deleted D

    Inner Join

    Inserted I

    On I.Key = D.Key

    where

    D.Sex <> I.Sex

    Deleted And Inserted are some kind of a picture of the data,

    the Question Is : Does SQL Server Uses The Indexes on The Table to compile the query (obve) in the trigger.

  • Hi Gil, Inserted and Deleted tables are maintained by the system. You can not modify the data there or to create index on these two tables.

    Regards,

    Iulian

  • Thanks for the answer but , this information was almost obvious to me

    My real Concern is performance

    Do I Need to create indexes on the base table in order to improve tirgger performance

  • No. The indexes on the base table do not reflect onto the deleted and inserted pseudo tables.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • So what can be done to boost trigger performance on a large scale update

    Does Instead of trigger with cte or temporary table is beter and in the use an update to the base table

  • Gil_Adi (11/8/2011)


    So what can be done to boost trigger performance on a large scale update

    Depends on what's causing the performance problem

    Does Instead of trigger with cte or temporary table is beter and in the use an update to the base table

    No, probably no better, possibly even worse depending what you're doing.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • What I had In Mind Is to Create Temporary Tables for deleted and inserted and than create an Index on them for the fields Key and sex ,then Log the changes as in the first query and in the end Update the base table from the Instead of trigger

    Will it be faster

  • Unlikely, the overhead of creating and indexing the temp table will likely outweigh any improvements, unless you're using the deleted and inserted many, many times.

    Test it and see.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Gil_Adi (11/8/2011)


    What I had In Mind Is to Create Temporary Tables for deleted and inserted and than create an Index on them for the fields Key and sex ,then Log the changes as in the first query and in the end Update the base table from the Instead of trigger

    You're using an INSTEAD OF trigger? Is that because the updates are being performed against a view? Or are you doing some other processing you haven't mentioned? Otherwise, wouldn't it be better to use an AFTER trigger?

    On the more general point, I wouldn't expect 30,000 rows in inserted and deleted to cause a problem unless the optimizer was choosing a nested loops join for some reason. Normally, with this sort of query, the optimizer will choose to hash join inserted and deleted. Have you captured an 'actual' execution plan for the trigger? If so, please share it. If not, perhaps that would be a good idea...

  • I still want to know if there is a performance problem or if this is a theoretical question. If the latter, go test, see if the performance is acceptable and, only if it is not, worry about stuff like this.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Thanks for every body

    1. Right now it is a theoretical question on wat is the best practice

    2. I sugested the insted of trigger just to be able to use temporary tables but as I thought it over

    It can be done inside the trigger it self

  • Gil_Adi (11/8/2011)


    Thanks for every body

    1. Right now it is a theoretical question on wat is the best practice

    The best practice is to write code as simply as possible, without unnecessary complexities and to do performance tests on that code. If the performance is not acceptable, then take any necessary steps.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • the Question Is : Does SQL Server Uses The Indexes on The Table to compile the query (obve) in the trigger.

    No, it does not. Query Compilation has nothing to do with index usage in trigger (or stored procedures as well). Per my understanding, Compilation just verifies syntaxical errors & object dependencies (permissions & availability etc.)

    Optimizer decides to use indexes based on their availability & usefulness.

  • Does Instead of trigger with cte or temporary table is beter and in the use an update to the base table

    I don't think so. It will add overhead.

  • Dev @ +91 973 913 6683 (11/8/2011)


    Per my understanding, Compilation just verifies syntaxical errors & object dependencies (permissions & availability etc.)

    Syntax and object existence is done by the parser and algebriser. That's not a compile.

    Optimizer decides to use indexes based on their availability & usefulness.

    Generating a plan is what's referred to as 'compilation', so compilation is the point at which indexes are considered.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass

Viewing 15 posts - 1 through 15 (of 42 total)

You must be logged in to reply to this topic. Login to reply