|
| 1 | +-------------------------------------------------------------------------------------------------------------------------------- |
| 2 | +-- DELETE CONSECUTIVE ATTRIBUTES |
| 3 | +-------------------------------------------------------------------------------------------------------------------------------- |
| 4 | +-- Current versions of the OnTopic Library evaluate whether or not an attribute value has changed since the previous version, |
| 5 | +-- and doesn't create a new attribute version if it has. This wasn't true in previous versions, however. As a result, there are |
| 6 | +-- some cases, and especially in older databases, where unnecessary duplicates occur for attribute values. This script will |
| 7 | +-- detect concurrent duplicates and remove them from the database. This reduces the size of the database, without interfering |
| 8 | +-- with the data integrity. If attribute values are not consecutive, the duplicates aren't deleted; e.g., if the value of |
| 9 | +-- <c>Title</c> gets changed, then gets reverted, all three versions will be retained in the database. |
| 10 | +-------------------------------------------------------------------------------------------------------------------------------- |
| 11 | + |
| 12 | +CREATE PROCEDURE [dbo].[DeleteConsecutiveAttributes] |
| 13 | +AS |
| 14 | + |
| 15 | +SET NOCOUNT ON; |
| 16 | + |
| 17 | +-------------------------------------------------------------------------------------------------------------------------------- |
| 18 | +-- CHECK INITIAL VALUES |
| 19 | +-------------------------------------------------------------------------------------------------------------------------------- |
| 20 | +DECLARE @Count INT |
| 21 | + |
| 22 | +SELECT @Count = Count(TopicID) |
| 23 | +FROM Attributes |
| 24 | + |
| 25 | +Print('Initial Count: ' + CAST(@Count AS VARCHAR) + ' Attributes in the database.'); |
| 26 | + |
| 27 | +-------------------------------------------------------------------------------------------------------------------------------- |
| 28 | +-- IDENTIFY GROUPS OF CONCURRENT DUPLICATES |
| 29 | +-------------------------------------------------------------------------------------------------------------------------------- |
| 30 | +WITH GroupedValues AS ( |
| 31 | + SELECT TopicID, |
| 32 | + AttributeKey, |
| 33 | + AttributeValue, |
| 34 | + DateModified, |
| 35 | + Version, |
| 36 | + ValueGroup = ROW_NUMBER() OVER(PARTITION BY TopicID, AttributeKey ORDER BY TopicID, AttributeKey, Version) |
| 37 | + - ROW_NUMBER() OVER(PARTITION BY TopicID, AttributeKey, AttributeValue ORDER BY TopicID, AttributeKey, Version) |
| 38 | + FROM Attributes |
| 39 | +), |
| 40 | + |
| 41 | +-------------------------------------------------------------------------------------------------------------------------------- |
| 42 | +-- RANK DUPLICATES BY DATE |
| 43 | +-------------------------------------------------------------------------------------------------------------------------------- |
| 44 | +RankedValues AS ( |
| 45 | + SELECT TopicID, |
| 46 | + AttributeKey, |
| 47 | + AttributeValue, |
| 48 | + DateModified, |
| 49 | + Version, |
| 50 | + ValueGroup, |
| 51 | + ValueRank = ROW_NUMBER() OVER(PARTITION BY ValueGroup, TopicID, AttributeKey, AttributeValue ORDER BY TopicID, AttributeKey, Version) |
| 52 | + FROM GroupedValues |
| 53 | +) |
| 54 | + |
| 55 | +-------------------------------------------------------------------------------------------------------------------------------- |
| 56 | +-- DELETE NEWER DUPLICATES |
| 57 | +-------------------------------------------------------------------------------------------------------------------------------- |
| 58 | +DELETE |
| 59 | +FROM RankedValues |
| 60 | +WHERE ValueRank > 1; |
| 61 | + |
| 62 | +-------------------------------------------------------------------------------------------------------------------------------- |
| 63 | +-- CHECK FINAL VALUES |
| 64 | +-------------------------------------------------------------------------------------------------------------------------------- |
| 65 | +SELECT @Count = @Count - Count(TopicID) |
| 66 | +FROM Attributes |
| 67 | + |
| 68 | +Print('Final Count: ' + CAST(@Count AS VARCHAR) + ' Attributes were identified and deleted.') |
| 69 | + |
0 commit comments