Friday, January 04, 2008

Recompilation in SQL Server 2005

Before executing SQL statement, SQL server checks for the validity and correctness of the query and generates the execution plan if it's not all already available. No need to say compilation is very CPU intensive process. Before executing the already compiled SQL statement, SQL server perform several other checks and if any of these checks fails, SQL server perform again compilation of SQL statement. Such compilations are known as recompilations. Following are some reasons for recompilation:

  1. Schema change
  2. Statistics change
  3. Deferred compile
  4. SET option change
  5. Temporary table change
  6. Stored procedure created with RECOMPILE query hint or OPTION(RECOMPILE)

In SQL Server 2000, if you need to recompile a portion of stored procedure, you need to compile the whole stored procedure, however in SQL Server 2005, you can perform recompilation at statement level. Less recompilation means less CPU consumption and less contention.

For more information, see Identifying Recompilation

No comments: