-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDeleteConsecutiveExtendedAttributes.sql
More file actions
69 lines (58 loc) · 3.76 KB
/
DeleteConsecutiveExtendedAttributes.sql
File metadata and controls
69 lines (58 loc) · 3.76 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
--------------------------------------------------------------------------------------------------------------------------------
-- DELETE CONSECUTIVE ATTRIBUTES
--------------------------------------------------------------------------------------------------------------------------------
-- Current versions of the OnTopic Library evaluate whether or not the composite XML for the extended attribute values has
-- changed since the previous version, and only creates a new version if it has. This wasn't true in previous versions, however.
-- As a result, there are some cases, and especially in older databases, where unnecessary duplicates occur for attribute
-- values. These dramatically increase the size of the database and can slow down the processing time of certain queries. This
-- procedure will detect concurrent duplicates and remove them from the database. This reduces the size of the database, without
-- interfering with the data integrity.
--------------------------------------------------------------------------------------------------------------------------------
-- NOTE: Because this query must cast the XML values as VARCHAR in order to compare them, it takes a LONG time to run. Please
-- be patient!
--------------------------------------------------------------------------------------------------------------------------------
CREATE PROCEDURE [Utilities].[DeleteConsecutiveExtendedAttributes]
AS
SET NOCOUNT ON;
--------------------------------------------------------------------------------------------------------------------------------
-- CHECK INITIAL VALUES
--------------------------------------------------------------------------------------------------------------------------------
DECLARE @Count INT
SELECT @Count = COUNT(TopicID)
FROM ExtendedAttributes
Print('Initial Count: ' + CAST(@Count AS VARCHAR) + ' Extended Attributes in the database.');
--------------------------------------------------------------------------------------------------------------------------------
-- IDENTIFY GROUPS OF CONCURRENT DUPLICATES
--------------------------------------------------------------------------------------------------------------------------------
WITH GroupedValues AS (
SELECT TopicID,
AttributesXml,
Version,
ValueGroup = ROW_NUMBER() OVER(PARTITION BY TopicID ORDER BY TopicID, Version)
- ROW_NUMBER() OVER(PARTITION BY TopicID, CAST(AttributesXml AS NVARCHAR(MAX)) ORDER BY TopicID, Version)
FROM ExtendedAttributes
),
--------------------------------------------------------------------------------------------------------------------------------
-- RANK DUPLICATES BY DATE
--------------------------------------------------------------------------------------------------------------------------------
RankedValues AS (
SELECT TopicID,
AttributesXml,
Version,
ValueGroup,
ValueRank = ROW_NUMBER() OVER(PARTITION BY ValueGroup, TopicID, CAST(AttributesXml AS NVARCHAR(MAX)) ORDER BY TopicID, Version)
FROM GroupedValues
)
--------------------------------------------------------------------------------------------------------------------------------
-- DELETE NEWER DUPLICATES
--------------------------------------------------------------------------------------------------------------------------------
DELETE
FROM RankedValues
WHERE ValueRank > 1;
PRINT('Concurrent duplicates have been deleted.')
--------------------------------------------------------------------------------------------------------------------------------
-- CHECK FINAL VALUES
--------------------------------------------------------------------------------------------------------------------------------
SELECT @Count = @Count - COUNT(TopicID)
FROM ExtendedAttributes
Print('Final Count: ' + CAST(@Count AS VARCHAR) + ' duplicate Extended Attributes were identified and deleted.')