Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do triggers decreases the performance? Inserted and deleted tables?

Suppose i am having stored procedures which performs Insert/update/delete operations on table.

Depending upon some criteria i want to perform some operations.

Should i create trigger or do the operation in stored procedure itself.

Does using the triggers decreases the performance?

Does these two tables viz Inserted and deleted exists(persistent) or are created dynamically?

If they are created dynamically does it have performance issue.

If they are persistent tables then where are they?

Also if they exixts then can i access Inserted and Deleted tables in stored procedures?

like image 335
Panache Avatar asked Sep 19 '09 18:09

Panache


People also ask

Do triggers affect performance?

The INSERT, DELETE, UPDATE, and SELECT statements that initiate triggers might appear to execute slowly because they execute additional SQL statements, and the user might not know that other actions are occurring.

Do triggers slow down database?

Yes, the trigger must slow down inserts. The reason is that relational databases are ACID compliant: All actions, including side-effects like triggers, must be completed before the update transaction completes.

What are the disadvantages of triggers in SQL?

Cons of SQL Server TriggersTriggers add overhead to DML statements. If there are many nested triggers it could get very hard to debug and troubleshoot, which consumes development time and resources.

What is inserted and deleted table in a trigger?

Inserted and deleted are the magic tables in the SQL Server trigger that used to manage pre-updated and post updated row.


2 Answers

Will it be less performant than doing the same thing in a stored proc. Probably not but with all performance questions the only way to really know is to test both approaches with a realistic data set (if you have a 2,000,000 record table don't test with a table with 100 records!)

That said, the choice between a trigger and another method depends entirely on the need for the action in question to happen no matter how the data is updated, deleted, or inserted. If this is a business rule that must always happen no matter what, a trigger is the best place for it or you will eventually have data integrity problems. Data in databases is frequently changed from sources other than the GUI.

When writing a trigger though there are several things you should be aware of. First, the trigger fires once for each batch, so whether you inserted one record or 100,000 records the trigger only fires once. You cannot assume ever that only one record will be affected. Nor can you assume that it will always only be a small record set. This is why it is critical to write all triggers as if you are going to insert, update or delete a million rows. That means set-based logic and no cursors or while loops if at all possible. Do not take a stored proc written to handle one record and call it in a cursor in a trigger.

Also do not send emails from a cursor, you do not want to stop all inserts, updates, or deletes if the email server is down.

like image 150
HLGEM Avatar answered Sep 21 '22 05:09

HLGEM


Yes, a table with a trigger will not perform as well as it would without it. Logic dictates that doing something is more expensive than doing nothing.

I think your question would be more meaningful if you asked in terms of whether it is more performant than some other approach that you haven't specified.

Ultimately, I'd select the tool that is most appropriate for the job and only worry about performance if there is a problem, not before you have even implemented a solution.

Inserted and deleted tables are available within the trigger, so calling them from stored procedures is a no-go.

like image 38
JohnFx Avatar answered Sep 21 '22 05:09

JohnFx