浏览代码

QS clear affected plans

Pedro Lopes 8 年之前
父节点
当前提交
2cd5e0fc1c

+ 61 - 0
After-Installing-SQL-Server-2017_CU3/ClearAffectedPlans_BeforeCU3_Upgrade.sql

@@ -0,0 +1,61 @@
+SET NOCOUNT ON;
+DROP TABLE IF EXISTS #tmpUserDBs;
+
+SELECT [database_id], 0 AS [IsDone]
+INTO #tmpUserDBs
+FROM master.sys.databases
+WHERE [database_id] > 4
+	AND [state] = 0 -- must be ONLINE
+	AND is_read_only = 0 -- cannot be READ_ONLY
+	AND [database_id] NOT IN (SELECT dr.database_id FROM sys.dm_hadr_database_replica_states dr -- Except all local Always On secondary replicas
+		INNER JOIN sys.dm_hadr_availability_replica_states rs ON dr.group_id = rs.group_id
+		INNER JOIN sys.databases d ON dr.database_id = d.database_id
+		WHERE rs.role = 2 -- Is Secondary
+			AND dr.is_local = 1
+			AND rs.is_local = 1)
+
+DECLARE @userDB sysname;
+
+WHILE (SELECT COUNT([database_id]) FROM #tmpUserDBs WHERE [IsDone] = 0) > 0
+BEGIN
+	SELECT TOP 1 @userDB = DB_NAME([database_id]) FROM #tmpUserDBs WHERE [IsDone] = 0
+
+	-- PRINT 'Working on database ' + @userDB
+
+	EXEC ('USE [' + @userDB + '];
+DECLARE @clearPlan bigint;
+IF EXISTS (SELECT [actual_state] FROM sys.database_query_store_options WHERE [actual_state] IN (1,2))
+BEGIN
+	IF EXISTS (SELECT plan_id FROM sys.query_store_plan WHERE engine_version = ''14.0.3008.27'')
+	BEGIN
+		DROP TABLE IF EXISTS #tmpclearPlans;
+
+		SELECT plan_id, 0 AS [IsDone]
+		INTO #tmpclearPlans
+		FROM sys.query_store_plan WHERE engine_version = ''14.0.3008.27''
+
+		WHILE (SELECT COUNT(plan_id) FROM #tmpclearPlans WHERE [IsDone] = 0) > 0
+		BEGIN
+			SELECT TOP 1 @clearPlan = plan_id FROM #tmpclearPlans WHERE [IsDone] = 0
+			EXECUTE sys.sp_query_store_remove_plan @clearPlan
+
+			UPDATE #tmpclearPlans 
+			SET [IsDone] = 1 
+			WHERE plan_id = @clearPlan
+		END;
+
+		PRINT ''- Cleared possibly affected plans in database [' + @userDB + ']''
+	END
+	ELSE
+	BEGIN
+		PRINT ''- No affected plans in database [' + @userDB + ']''
+	END
+END
+ELSE
+BEGIN
+	PRINT ''- Query Store not enabled in database [' + @userDB + ']''
+END')
+		UPDATE #tmpUserDBs 
+		SET [IsDone] = 1 
+		WHERE [database_id] = DB_ID(@userDB)
+END

+ 7 - 0
After-Installing-SQL-Server-2017_CU3/Readme.md

@@ -0,0 +1,7 @@
+We have recently found an issue where if you are using Query Store feature with SQL Server 2017 CU2, and later upgrade to SQL Server 2017 CU3 (or higher when available), an attempt to use the stored showplan fails. This includes the ability to force a specific plan captured by Query Store while SQL Server 2017 CU2 (14.0.3008.27) was installed, while already running with CU3 (14.0.3015.40).
+As such, to assist DBAs in removing any of the affected plans collected while SQL Server 2017 CU2 was installed, we have created this T-SQL script to remove only the affected plans in Query Store. Execute it immediately after installing SQL Server 2017 CU3 or above.
+The respective KB articles for SQL Server 2017 CU2 and CU3 will be updated to reflect this recommendation.
+
+Note: any query plans captured by Query Store in versions prior to 14.0.3008.27 are not affected. Only plans captured in 14.0.3008.27.
+
+Note2: While Query Store is also available in SQL Server 2016, this version is not affected in any build.