Stored Procedure and Trigger Execution

SQL Server 2005 stores only the source for stored procedures and triggers. When a stored procedure or trigger is first executed, the source is compiled into an execution plan. If the stored procedure or trigger is again executed before the execution plan is aged from memory, the relational engine detects the existing plan and reuses it. If the plan has aged out of memory, a new plan is built. This process is similar to the process SQL Server 2005 follows for all SQL statements. The main performance advantage that stored procedures and triggers have in SQL Server 2005 compared with batches of dynamic SQL is that their SQL statements are always the same. Therefore, the relational engine easily matches them with any existing execution plans. Stored procedure and trigger plans are easily reused.

Stored procedures had a more pronounced performance advantage over other SQL statements in versions of SQL Server earlier than SQL Server 7.0. These versions of SQL Server did not try to reuse execution plans for batches that were not stored procedures or triggers. The only way to reuse execution plans was to encode the SQL statements in stored procedures.

The execution plan for stored procedures and triggers is executed separately from the execution plan for the batch calling the stored procedure or firing the trigger. This allows for greater reuse of the stored procedure and trigger execution plans.

See Also


Query Processor Architecture

Help and Information

Getting SQL Server 2005 Assistance