Previous | Table of Contents | Next |
Lets walk through the steps, understanding what resources are in use and what is occurring as SQL Server processes the statement:
Note: SQL Server supports three different update modes: deferred updates, direct updates, and direct updates in place. The direct updates in place are by far the most efficient because rows are not deleted and then updated; instead, the change is made to the row where it sits on the data page and a single modification record is written to the log.Unfortunately, one of the rules for the direct update in place is that the table must not have an update trigger. An update on a table with an update trigger is always handled as a DELETE followed by an INSERT.
If all of this seems enormously complex and slow, it can be; you have good reason to be concerned. Although it is complex, its usually very fast because all the work prior to an actual COMMIT operation takes place in memory.
From a performance standpoint, the most important thing to keep in mind is the locking. When your trigger is running, there are almost always exclusive, blocking locks in place. Other users will end up waiting while your trigger runs. This means that trigger processing has to be minimal (do as little as possible) and very efficient (write your very best SQL). Well look at some specific examples of this later today.
Triggers have resources other SQL constructions dont. They can roll back transactions. They can look at the old and new versions of rows modified by a statement. And they can determine which columns were modified. Well write some simple examples to show how these resources are used; a little later, well use them in a more complex and realistic context.
Earlier, I mentioned that the server writes all inserts and deletes to the transaction log prior to writing them to the table. These logged rows are available to a trigger as the inserted and deleted tables.
Note: Sybase, whose SQL Server product is very similar to Microsoft SQL Server, has for years referred to the inserted and deleted tables in class materials as magic tables. Others have come up with equally goofy names, believing that a clear description of these tables would overwhelm a SQL novice. Lets find out.Actually, inserted and deleted tables are highly restricted views of the transaction log, visible only within a trigger, localized for your session, and displaying only those rows modified by your session and current statement.
Is everybody feeling OK? Phew!
Within the trigger code, you can use SELECT with inserted and deleted tables just as you would with any ordinary table. For example, to get a better feeling for what these tables are, you can display their contents inside the trigger code. Go ahead and create this trigger:
create trigger tr2 on t for insert, update as print ============ inserted table ============== select * from inserted print ============ deleted table ============== select * from deleted return
Note: As usual, when you create an object, SQL Server returns no response if you get it right.
insert t (c2, c3) values (88, jkl)
============ inserted table ============== c1 c2 c3 ---------- ---------- ---- 7 88 jkl ============ deleted table ============== c1 c2 c3 ----------- ----------- ----
The trigger code shows you the contents of the inserted and deleted tables. The deleted table is empty; no rows are deleted as the result of an insert. The inserted table shows the contents of the row we added.
Notice that the inserted and deleted tables have the same structure as the table itself.
Lets update some rows now:
update t set c3 = qqq where c2 between 80 and 90
============ inserted table ============== c1 c2 c3 ----------- ----------- ---- 4 80 qqq 5 88 qqq ============ deleted table ============== c1 c2 c3 ----------- ----------- ---- 4 80 abc 5 88 jkl
Theres no updated table, right? Instead, the deleted table shows you the rows before the update, and inserted shows you the rows after the update.
What kinds of things can you do with the inserted and deleted tables? You can validate column values and row consistency, check referential integrity, determine whether rows are ready for archive, maintain an archive or update derived values, or keep an audit trail, just to name a few. Inserted and deleted tables allow you to look at just the rows that were modified, without having to scan the entire table.
What cant you do with them? For one thing, you cant access them from any place other than a trigger. For instance, if you were to execute a stored procedure from within a trigger, the procedure would not be able to access inserted or deleted. You also cant modify them because they are read-only.
Previous | Table of Contents | Next |