From cbcbca0eb461d5651f0e824ccdd5473271f6067d Mon Sep 17 00:00:00 2001 From: Sergii Syrovatchenko Date: Sun, 27 Jun 2021 22:27:17 +0300 Subject: [PATCH] Bugfix during truncate table execution --- Forms/MainBox.cs | 2 +- Properties/Resources.Designer.cs | 9 +++++++++ Properties/Resources.resx | 3 +++ Server/Index.cs | 5 ++++- Server/Query.cs | 18 ++++++++++++++++++ Server/QueryEngine.cs | 1 + 6 files changed, 36 insertions(+), 2 deletions(-) diff --git a/Forms/MainBox.cs b/Forms/MainBox.cs index eba4b1a..d066cba 100644 --- a/Forms/MainBox.cs +++ b/Forms/MainBox.cs @@ -633,7 +633,7 @@ private List GetIndexOperations(Index ix) { } } - if (ix.IsTable) { + if (ix.IsTable && !ix.IsFKs && ((ix.IsPartitioned && Settings.ServerInfo.MajorVersion >= ServerVersion.Sql2016) || !ix.IsPartitioned)) { i.Add(IndexOp.TRUNCATE_TABLE); } diff --git a/Properties/Resources.Designer.cs b/Properties/Resources.Designer.cs index 4bb917b..48cc45f 100644 --- a/Properties/Resources.Designer.cs +++ b/Properties/Resources.Designer.cs @@ -615,6 +615,15 @@ internal static string IsFiltered { } } + /// + /// Looks up a localized string similar to IsFKs. + /// + internal static string IsFKs { + get { + return ResourceManager.GetString("IsFKs", resourceCulture); + } + } + /// /// Looks up a localized string similar to IsLob. /// diff --git a/Properties/Resources.resx b/Properties/Resources.resx index 85ff687..e0068dc 100644 --- a/Properties/Resources.resx +++ b/Properties/Resources.resx @@ -409,4 +409,7 @@ IsTable + + IsFKs + \ No newline at end of file diff --git a/Server/Index.cs b/Server/Index.cs index b64e379..e23e9e8 100644 --- a/Server/Index.cs +++ b/Server/Index.cs @@ -50,6 +50,7 @@ public class Index { public bool IsAllowOnlineRebuild { get; set; } public bool IsAllowCompression { get; set; } public bool IsTable { get; set; } + public bool IsFKs { get; set; } public bool IsColumnstore => (IndexType == IndexType.CLUSTERED_COLUMNSTORE || IndexType == IndexType.NONCLUSTERED_COLUMNSTORE); public string Error { get; set; } @@ -172,7 +173,9 @@ public string GetQuery() { break; case IndexOp.TRUNCATE_TABLE: - sql = $"TRUNCATE TABLE {objectName};"; + sql = IsPartitioned + ? $"TRUNCATE TABLE {objectName} WITH (PARTITIONS ({partition}));" + : $"TRUNCATE TABLE {objectName};"; break; case IndexOp.UPDATE_STATISTICS_SAMPLE: diff --git a/Server/Query.cs b/Server/Query.cs index aa592ff..9e43ae8 100644 --- a/Server/Query.cs +++ b/Server/Query.cs @@ -255,6 +255,22 @@ ObjectID INT NOT NULL DECLARE @MINUTE INT SET @MINUTE = DATEDIFF(MINUTE, GETUTCDATE(), GETDATE()) +IF OBJECT_ID('tempdb.dbo.#FKs') IS NOT NULL + DROP TABLE #FKs + +CREATE TABLE #FKs (ObjectID INT PRIMARY KEY) +INSERT INTO #FKs +SELECT i.ObjectID +FROM ( + SELECT DISTINCT ObjectID + FROM #Indexes +) i +WHERE EXISTS( + SELECT * + FROM sys.foreign_keys f WITH(NOLOCK) + WHERE f.[referenced_object_id] = i.ObjectID + ) + SELECT i.ObjectID , i.IndexID , i.IndexName @@ -284,6 +300,7 @@ SELECT i.ObjectID , CreateDate = DATEADD(MINUTE, -@MINUTE, o.[create_date]) , ModifyDate = DATEADD(MINUTE, -@MINUTE, o.[modify_date]) , IsTable = CAST(CASE WHEN o.[type] = 'U' THEN 1 ELSE 0 END AS BIT) + , IsFKs = CAST(CASE WHEN fk.ObjectID IS NULL THEN 0 ELSE 1 END AS BIT) , i.IsUnique , i.IsPK , i.FillFactorValue @@ -296,6 +313,7 @@ SELECT i.ObjectID FROM #Indexes i JOIN sys.objects o WITH(NOLOCK) ON o.[object_id] = i.ObjectID JOIN sys.schemas s WITH(NOLOCK) ON s.[schema_id] = o.[schema_id] +LEFT JOIN #FKs fk ON fk.ObjectID = i.ObjectID LEFT JOIN #Stats ss ON ss.ObjectID = i.ObjectID AND ss.IndexID = i.IndexID LEFT JOIN #AggColumns a ON a.ObjectID = i.ObjectID AND a.IndexID = i.IndexID LEFT JOIN #Sparse p ON p.ObjectID = i.ObjectID diff --git a/Server/QueryEngine.cs b/Server/QueryEngine.cs index 91177c5..010c7b4 100644 --- a/Server/QueryEngine.cs +++ b/Server/QueryEngine.cs @@ -238,6 +238,7 @@ public static List GetIndexes(SqlConnection connection) { Fragmentation = _.Field(Resources.Fragmentation), PageSpaceUsed = _.Field(Resources.PageSpaceUsed), IsTable = _.Field(Resources.IsTable), + IsFKs = _.Field(Resources.IsFKs), IsAllowReorganize = _.Field(Resources.IsAllowPageLocks) && indexType != IndexType.HEAP, IsAllowOnlineRebuild = isOnlineRebuild, IsAllowCompression = Settings.ServerInfo.IsCompressionAvailable && !_.Field(Resources.IsSparse),