Skip to content

Commit 1a2f1d6

Browse files
committed
Introduce new 'DeleteOrphanedLastModifiedAttributes` maintenance procedure
Current versions on the OnTopic Library continue to write new `LastModified` attribute records every time a topic is saved in the OnTopic Editor—_even if no other attributes have been modified_. That's because the `LastModified` date is always different, and thus triggers the OnTopic Library's `IsDirty` check, which otherwise reduces non-changes from being saved. Over time, these values can add clutter to the database, reduce performance of some queries, and introduce a lot of "ghost" versions which don't represent _real_ versions. A future version of the OnTopic Library should develop a smarter way of evaluating these and conditionally saving them. In the meanwhile, however, they persist. As such, the `DeleteOrphanedLastModifiedAttributes` maintenance procedure will identify any orphaned `LastModified` attributes—i.e., ones whose `Version` doesn't map to any other modifications to indexed or extended attributes—and delete them. This can significantly reduce the amount of clutter and overall number of indexed attributes in the `Attributes` table. This may need to be run occassionally to prevent these from reaccumulating.
1 parent 62589e2 commit 1a2f1d6

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
--------------------------------------------------------------------------------------------------------------------------------
2+
-- DELETE ORPHANED LAST MODIFIED 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 process doesn't work for <c>LastModified</c>, however, as that
6+
-- value changes every time a value is saved—at least via the OnTopic Editor. If you save a topic five times in the editor, it
7+
-- will generate five <c>LastModified</c> values, <i>even if no other attribute values changed</i>. Over time, this can create a
8+
-- lot of clutter in the database, and potentially slow down some queries. This script identifies <c>LastModified</c> attributes
9+
-- which don't correspond to any other updates of <i>indexed</i> attributes, and deletes them.
10+
--------------------------------------------------------------------------------------------------------------------------------
11+
-- NOTE: There are legitimate scenarios where a topic is updated but it doesn't show up in other attribute values, such as when
12+
-- relationships are updated. These situations are the exception, however, and it's usually not an issue to lose that level of
13+
-- granularity.
14+
--------------------------------------------------------------------------------------------------------------------------------
15+
16+
CREATE PROCEDURE [dbo].[DeleteOrphanedLastModifiedAttributes]
17+
AS
18+
19+
SET NOCOUNT ON;
20+
21+
--------------------------------------------------------------------------------------------------------------------------------
22+
-- CHECK INITIAL VALUES
23+
--------------------------------------------------------------------------------------------------------------------------------
24+
DECLARE @Count INT
25+
26+
SELECT @Count = Count(TopicID)
27+
FROM Attributes
28+
WHERE AttributeKey = 'LastModified'
29+
30+
Print('Initial Count: ' + CAST(@Count AS VARCHAR) + ' LastModified records in the database.');
31+
32+
--------------------------------------------------------------------------------------------------------------------------------
33+
-- DELETE ORPHANED LAST MODIFIED ATTRIBUTES
34+
--------------------------------------------------------------------------------------------------------------------------------
35+
DELETE Attributes
36+
FROM Attributes
37+
LEFT JOIN Attributes Unmatched
38+
ON Attributes.TopicID = Unmatched.TopicID
39+
AND Attributes.Version = Unmatched.Version
40+
AND Attributes.AttributeKey != Unmatched.AttributeKey
41+
LEFT JOIN ExtendedAttributes UnmatchedExtended
42+
ON Attributes.TopicID = UnmatchedExtended.TopicID
43+
AND Attributes.Version = UnmatchedExtended.Version
44+
WHERE Unmatched.AttributeKey is null
45+
AND UnmatchedExtended.TopicID is null
46+
AND Attributes.AttributeKey = 'LastModified'
47+
48+
--------------------------------------------------------------------------------------------------------------------------------
49+
-- CHECK FINAL VALUES
50+
--------------------------------------------------------------------------------------------------------------------------------
51+
SELECT @Count = @Count - Count(TopicID)
52+
FROM Attributes
53+
WHERE AttributeKey = 'LastModified'
54+
55+
Print('Final Count: ' + CAST(@Count AS VARCHAR) + ' orphaned LastModified records were identified and deleted.')

OnTopic.Data.Sql.Database/OnTopic.Data.Sql.Database.sqlproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
<Build Include="Types\TopicList.sql" />
102102
<Build Include="Views\AttributeIndex.sql" />
103103
<Build Include="Maintenance\DeleteConsecutiveExtendedAttributes.sql" />
104+
<Build Include="Maintenance\DeleteOrphanedLastModifiedAttributes.sql" />
104105
<Build Include="Maintenance\DeleteConsecutiveAttributes.sql" />
105106
</ItemGroup>
106107
<ItemGroup>

0 commit comments

Comments
 (0)