Skip to content

Commit

Permalink
Bugfix during truncate table execution
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiisyrovatchenko committed Jun 27, 2021
1 parent ef46aa3 commit cbcbca0
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Forms/MainBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ private List<IndexOp> 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);
}

Expand Down
9 changes: 9 additions & 0 deletions Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -409,4 +409,7 @@
<data name="IsTable" xml:space="preserve">
<value>IsTable</value>
</data>
<data name="IsFKs" xml:space="preserve">
<value>IsFKs</value>
</data>
</root>
5 changes: 4 additions & 1 deletion Server/Index.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -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:
Expand Down
18 changes: 18 additions & 0 deletions Server/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions Server/QueryEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ public static List<Index> GetIndexes(SqlConnection connection) {
Fragmentation = _.Field<double?>(Resources.Fragmentation),
PageSpaceUsed = _.Field<double?>(Resources.PageSpaceUsed),
IsTable = _.Field<bool>(Resources.IsTable),
IsFKs = _.Field<bool>(Resources.IsFKs),
IsAllowReorganize = _.Field<bool>(Resources.IsAllowPageLocks) && indexType != IndexType.HEAP,
IsAllowOnlineRebuild = isOnlineRebuild,
IsAllowCompression = Settings.ServerInfo.IsCompressionAvailable && !_.Field<bool>(Resources.IsSparse),
Expand Down

0 comments on commit cbcbca0

Please sign in to comment.