Skip to content

Commit bb84324

Browse files
Improve Find-RemovalSubset
1 parent 274b2f4 commit bb84324

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

SqlSizer-MSSQL/Public/Find-RemovalSubset.ps1

+12-13
Original file line numberDiff line numberDiff line change
@@ -86,25 +86,22 @@
8686
$columns = $columns + " f.$($pk.Name) = p.Key$i "
8787
$i += 1
8888
}
89-
$where = " WHERE " + $fk.FkColumns[0].Name + " IS NOT NULL AND NOT EXISTS(SELECT * FROM $fkProcessing p WHERE p.[Color] = $newColor AND $columns)"
89+
$where = " WHERE NOT EXISTS(SELECT 1 FROM $fkProcessing p WHERE $columns) AND EXISTS(SELECT 1 FROM $processing s WHERE "
9090

91-
# from
92-
$join = " INNER JOIN $processing s ON "
9391
$i = 0
94-
9592
foreach ($fkColumn in $fk.FkColumns)
9693
{
9794
if ($i -gt 0)
9895
{
99-
$join += " AND "
96+
$where += " AND "
10097
}
10198

102-
$join += " s.Key$i = f.$($fkColumn.Name)"
99+
$where += " s.Key$i = f.$($fkColumn.Name)"
103100
$i += 1
104101
}
105102

106-
$join += " AND s.Iteration IN (SELECT FoundIteration FROM SqlSizer.Operations o WHERE o.Status = 0 AND o.[SessionId] = '$SessionId') "
107-
$from = " FROM " + $referencedByTable.SchemaName + "." + $referencedByTable.TableName + " f " + $join
103+
$where += " AND s.Depth = ##depth## ) "
104+
$from = " FROM " + $referencedByTable.SchemaName + "." + $referencedByTable.TableName + " f "
108105

109106
# select
110107
$columns = ""
@@ -131,7 +128,7 @@
131128
$topPhrase = " TOP $($top) "
132129
}
133130

134-
$select = "SELECT DISTINCT " + $topPhrase + $columns + ", s.Depth"
131+
$select = "SELECT " + $topPhrase + $columns
135132
$sql = $select + $from + $where
136133

137134
$columns = ""
@@ -145,7 +142,7 @@
145142
$insert = "DECLARE @SqlSizerCount INT = 0
146143
"
147144
}
148-
$insert += " SELECT $columns " + $newColor + " as Color, $tableId as TableId, x.Depth + 1 as Depth, $fkId as FkId, ##iteration## as Iteration INTO #tmp FROM (" + $sql + ") x SET @SqlSizerCount = @@ROWCOUNT "
145+
$insert += " SELECT $columns " + $newColor + " as Color, $tableId as TableId, ##depth## + 1 as Depth, $fkId as FkId, ##iteration## as Iteration INTO #tmp FROM (" + $sql + ") x SET @SqlSizerCount = @@ROWCOUNT "
149146
$insert += " INSERT INTO $fkProcessing SELECT * FROM #tmp "
150147
if ($MaxBatchSize -ne -1)
151148
{
@@ -155,7 +152,7 @@
155152
UPDATE SqlSizer.Operations SET [Status] = NULL WHERE [SessionId] = '$SessionId' AND [Status] = 0 AND [Table] = $($table.Id)
156153
END"
157154
}
158-
$insert += " INSERT INTO SqlSizer.Operations SELECT $fkTableId, $newColor, t.[Count], 0, NULL, $tableId, $fkId, t.Depth, GETDATE(), NULL, '$SessionId', ##iteration##, NULL FROM (SELECT Depth, COUNT(*) as [Count] FROM #tmp GROUP BY Depth) t "
155+
$insert += " INSERT INTO SqlSizer.Operations SELECT $fkTableId, $newColor, @SqlSizerCount, 0, NULL, $tableId, $fkId, ##depth## + 1, GETDATE(), NULL, '$SessionId', ##iteration##, NULL"
159156
$insert += " DROP TABLE #tmp "
160157

161158
if ($ConnectionInfo.IsSynapse -eq $false)
@@ -177,7 +174,8 @@
177174
(
178175
[TableInfo]$table,
179176
[int]$color,
180-
[int]$iteration
177+
[int]$iteration,
178+
[int]$depth
181179
)
182180

183181
$key = "$($table.SchemaName)_$($table.TableName)_$($color)"
@@ -195,6 +193,7 @@
195193
if ($query -ne "")
196194
{
197195
$query = $query.Replace("##iteration##", $iteration)
196+
$query = $query.Replace("##depth##", $depth)
198197
$null = Invoke-SqlcmdEx -Sql $query -Database $Database -ConnectionInfo $ConnectionInfo
199198
}
200199
}
@@ -298,7 +297,7 @@
298297
$signature = $structure.Tables[$table]
299298
$processing = $structure.GetProcessingName($signature, $SessionId)
300299

301-
HandleIncoming -table $table -color $color -iteration $iteration
300+
HandleIncoming -table $table -color $color -iteration $iteration -depth $depth
302301

303302
# mark operations as processed
304303
$q = "UPDATE SqlSizer.Operations SET Status = NULL WHERE Status = 0 AND ToProcess <> Processed AND [SessionId] = '$SessionId'"

0 commit comments

Comments
 (0)