-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPurgeUnusedBlobs.sql
More file actions
48 lines (34 loc) · 1.44 KB
/
PurgeUnusedBlobs.sql
File metadata and controls
48 lines (34 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
* NOTE: This script defaults to rolling back changes.
* To commit changes, set @saveChanges = 1.
*/
declare @saveChanges bit; --set @saveChanges = 1
set nocount on
create table #inuse (ID int not null primary key)
declare @sql varchar(8000); select @sql='insert #inuse(ID)'
select @sql = @sql + ' select ' + QUOTENAME(d.Name) + ' from dbo.' + QUOTENAME(t.Name) + ' where ' + QUOTENAME(d.Name) + ' is not null union'
from AttributeDefinition_Now d
join AssetType_Now t on t.ID=d.AssetID
where d.AttributeType='Blob'
select @sql = @sql + ' select HeadersId from dbo.WebhookReceipt where HeadersId is not null union select ResponseId from dbo.WebhookReceipt where ResponseId is not null'
--print @sql
exec (@sql)
select count(*) InUse from #inuse
select count(*) Before from dbo.Blob
declare @error int, @rowcount int
begin tran; save tran TX
delete Blob
from dbo.Blob
left join #inuse on #inuse.ID=Blob.ID
where Blob.Hash is null and #inuse.ID is null
--select Blob.ID, Hash from dbo.Blob left join #inuse on #inuse.ID=Blob.ID where #inuse.ID is null
select @rowcount=@@ROWCOUNT, @error=@@ERROR
if @error<>0 goto ERR
raiserror('%d unused Blobs dropped', 0, 1, @rowcount) with nowait
if (@saveChanges = 1) begin raiserror('Committing changes', 0, 254); goto OK end
raiserror('To commit changes, set @saveChanges=1',16,254)
ERR: raiserror('Rolling back changes', 0, 255); rollback tran TX
OK: commit
DONE:
select count(*) After from dbo.Blob
drop table #inuse