Dựa vào thông số plan_generation_num. Đã kiểm nghiệm trên SQL Server 2012 và chắc chắn script này cho ra kết quả chính xác.
SELECT --TOP 5 qst.plan_generation_num, qst.execution_count, stm.TEXT, DB_NAME(stm.dbid) AS DbName, stm.objectid FROM sys.dm_exec_query_stats qst CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS stm ORDER BY qst.plan_generation_num DESC
Nếu muốn recompile mỗi khi thực thi một Stored Procedure nào đó:
Option 1:
CREATE PROCEDURE dbo.PersonAge (@MinAge INT, @MaxAge INT) WITH RECOMPILE AS SELECT * FROM dbo.tblPerson WHERE Age >= @MinAge AND Age <= @MaxAge GO
Option 2:
EXEC dbo.PersonAge 65,70 WITH RECOMPILE
Nếu muốn recompile mỗi khi thực thi một SQL script:
USE AdventureWorks2012; DECLARE @PersonName nvarchar(100); SET @PersonName = 'Abercrombie'; SELECT * FROM Person.Person WHERE LastName <= @PersonName OPTION (RECOMPILE);
Tham khảo:
- http://www.sqlserver-dba.com/2012/11/sql-server-how-to-improve-execution-plan-reuse.html
- http://blog.sqlauthority.com/2010/02/20/sql-server-recompile-stored-procedure-at-run-time/
Về query Hints: http://technet.microsoft.com/en-us/library/ms181714.aspx
0 comments:
Post a Comment