Friday, March 4, 2016

Database : Stored Procedure Plan Recompilation and Performance Tuning

The main advantage of stored procedure is, to execute T-SQL statements in less time than the similar set of T-SQL statements is executed individually. The reason to take less time is that the query execution plan for the stored procedures is already stored in the "sys.procedures" system defined view.
The recompilation process of stored procedure is like as compilation process and also reduce Sql Server performance. Stored procedure with recompilation option was introduced in Sql Server 2005. We should recompile stored procedure in following cases
  1. Changing to the schema (means adding/dropping columns, constraints, rules, index, trigger etc) of the tables or referenced table(s) or view(s).
  2. Updating the statistics used by the execution plan of stored procedure
We have two options for stored procedure recompilation
  1. Recompile option at the time of Creation

    In this we create a stored procedure with RECOMPILE option. When we call this procedure than every time this procedure will be recompile before executing.
    1. CREATE PROCEDURE usp_InsertEmployee
    2. WITH RECOMPILE
    3. @flag bit output,-- return 0 for fail,1 for success
    4. @EmpID int,
    5. @Name varchar(50),
    6. @Salary int,
    7. @Address varchar(100)
    8. AS
    9. BEGIN
    10. BEGIN TRANSACTION
    11. BEGIN TRY
    12. Insert into Employee(EmpID,Name,Salary,Address) Values(@EmpID,@Name,@Salary,@Address);
    13. set @flag=1;
    14. commit TRANSACTION;
    15. END TRY
    16. BEGIN CATCH
    17. rollback TRANSACTION;
    18. set @flag=0;
    19. END CATCH
    20. END
    1. Declare @flag bit
    2. --Now Execute this procedure. Every time this procedure will be recompiled
    3. EXEC usp_InsertEmployee @flag output,1,'Deepak',14000,'Noida'
    4. if @flag=1
    5. print 'Successfully inserted'
    6. else
    7. print 'There is some error'
  2. Recompile option at the time of Execution

    In this we call a stored procedure with RECOMPILE option. Hence this stored procedure will be compiled only when we use RECOMPILE option at the time of calling. This is the best option for stored procedure recompilation.
    1. CREATE PROCEDURE usp_InsertEmployee
    2. @flag bit output,-- return 0 for fail,1 for success
    3. @EmpID int,
    4. @Name varchar(50),
    5. @Salary int,
    6. @Address varchar(100)
    7. AS
    8. BEGIN
    9. BEGIN TRANSACTION
    10. BEGIN TRY
    11. Insert into Employee(EmpID,Name,Salary,Address) Values(@EmpID,@Name,@Salary,@Address);
    12. set @flag=1;
    13. commit TRANSACTION;
    14. END TRY
    15. BEGIN CATCH
    16. rollback TRANSACTION;
    17. set @flag=0;
    18. END CATCH
    19. END
    1. Declare @flag bit
    2. --Now Execute this procedure with RECOMPILE option, if you want to recompile its execution plan
    3. EXEC usp_InsertEmployee @flag output,2,'Jitendra',15000,'Noida' WITH RECOMPILE
    4. if @flag=1
    5. print 'Successfully inserted'
    6. else
    7. print 'There is some error'

Note

  1. Creating the stored procedure by using "WITH RECOMPILE" option force the SQL Server to recompile the stored procedure every time when it is called.
  2. Call the stored procedure by using "WITH RECOMPILE" option in the EXEC command.
  3. Altering the procedure will cause the SQL Server to create a new execution plan
  4. If SQL Server is restarted or stopped then all the execution plans will be flush from server cache and recreated when the stored procedure is executed after restarting the server.
  5. The "Sp_recompile" system defined stored procedure can be called to refresh the query execution plan for a particular stored procedure

No comments:

Post a Comment