|
86 | 86 | $columns = $columns + " f.$($pk.Name) = p.Key$i "
|
87 | 87 | $i += 1
|
88 | 88 | }
|
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 " |
90 | 90 |
|
91 |
| - # from |
92 |
| - $join = " INNER JOIN $processing s ON " |
93 | 91 | $i = 0
|
94 |
| - |
95 | 92 | foreach ($fkColumn in $fk.FkColumns)
|
96 | 93 | {
|
97 | 94 | if ($i -gt 0)
|
98 | 95 | {
|
99 |
| - $join += " AND " |
| 96 | + $where += " AND " |
100 | 97 | }
|
101 | 98 |
|
102 |
| - $join += " s.Key$i = f.$($fkColumn.Name)" |
| 99 | + $where += " s.Key$i = f.$($fkColumn.Name)" |
103 | 100 | $i += 1
|
104 | 101 | }
|
105 | 102 |
|
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 " |
108 | 105 |
|
109 | 106 | # select
|
110 | 107 | $columns = ""
|
|
131 | 128 | $topPhrase = " TOP $($top) "
|
132 | 129 | }
|
133 | 130 |
|
134 |
| - $select = "SELECT DISTINCT " + $topPhrase + $columns + ", s.Depth" |
| 131 | + $select = "SELECT " + $topPhrase + $columns |
135 | 132 | $sql = $select + $from + $where
|
136 | 133 |
|
137 | 134 | $columns = ""
|
|
145 | 142 | $insert = "DECLARE @SqlSizerCount INT = 0
|
146 | 143 | "
|
147 | 144 | }
|
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 " |
149 | 146 | $insert += " INSERT INTO $fkProcessing SELECT * FROM #tmp "
|
150 | 147 | if ($MaxBatchSize -ne -1)
|
151 | 148 | {
|
|
155 | 152 | UPDATE SqlSizer.Operations SET [Status] = NULL WHERE [SessionId] = '$SessionId' AND [Status] = 0 AND [Table] = $($table.Id)
|
156 | 153 | END"
|
157 | 154 | }
|
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" |
159 | 156 | $insert += " DROP TABLE #tmp "
|
160 | 157 |
|
161 | 158 | if ($ConnectionInfo.IsSynapse -eq $false)
|
|
177 | 174 | (
|
178 | 175 | [TableInfo]$table,
|
179 | 176 | [int]$color,
|
180 |
| - [int]$iteration |
| 177 | + [int]$iteration, |
| 178 | + [int]$depth |
181 | 179 | )
|
182 | 180 |
|
183 | 181 | $key = "$($table.SchemaName)_$($table.TableName)_$($color)"
|
|
195 | 193 | if ($query -ne "")
|
196 | 194 | {
|
197 | 195 | $query = $query.Replace("##iteration##", $iteration)
|
| 196 | + $query = $query.Replace("##depth##", $depth) |
198 | 197 | $null = Invoke-SqlcmdEx -Sql $query -Database $Database -ConnectionInfo $ConnectionInfo
|
199 | 198 | }
|
200 | 199 | }
|
|
298 | 297 | $signature = $structure.Tables[$table]
|
299 | 298 | $processing = $structure.GetProcessingName($signature, $SessionId)
|
300 | 299 |
|
301 |
| - HandleIncoming -table $table -color $color -iteration $iteration |
| 300 | + HandleIncoming -table $table -color $color -iteration $iteration -depth $depth |
302 | 301 |
|
303 | 302 | # mark operations as processed
|
304 | 303 | $q = "UPDATE SqlSizer.Operations SET Status = NULL WHERE Status = 0 AND ToProcess <> Processed AND [SessionId] = '$SessionId'"
|
|
0 commit comments