Skip to content

Commit fae0e8d

Browse files
committed
Merge branch 'feature/UpdateTopic-performance' into develop
While the `feature/track-dirty-relationships` (afa1e9b) greatly improved the performance of recursively saving a large topic hierarchy by avoiding unnecessary and expensive calls to the `UpdateTopic` stored procedure, it did nothing to make that call itself less expensive. This update fixes that by reviewing and optimizing the `UpdateTopic` stored procedure, while simultaneously including additional functionality. In testing, the stored procedure is about twice as fast as it was previously, taking 18 seconds to update ≈250 topics, instead of the previous 38 seconds. And that's _with_ a new subquery to ensure that none of the indexed attributes are duplicating their previous value—something we currently do in `SqlTopicRepository`, but which is preferably enforced by the stored procedure itself. This update includes: - Lookup `@PreviousExtendedAttributes` directly from `ExtendedAttributes` instead of going through `ExtendedAttributeIndex` (b76ac69) - Explored various performance improvements to nested query for null attributes, but found the current query to be optimal (4035bcc) - Applied nested query to indexed attributes handling in order to prevent duplicates from being created (78dd6cd)
2 parents afa1e9b + 78dd6cd commit fae0e8d

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

OnTopic.Data.Sql.Database/Stored Procedures/UpdateTopic.sql

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,29 @@ SELECT @TopicID,
3232
AttributeKey,
3333
AttributeValue,
3434
@Version
35-
FROM @Attributes
35+
FROM @Attributes New
36+
OUTER APPLY (
37+
SELECT TOP 1
38+
AttributeValue AS ExistingValue
39+
FROM Attributes
40+
WHERE TopicID = @TopicID
41+
AND AttributeKey = New.AttributeKey
42+
ORDER BY Version DESC
43+
) Existing
3644
WHERE AttributeKey != 'ParentId'
37-
AND IsNull(AttributeValue, '') != ''
45+
AND ISNULL(AttributeValue, '') != ''
46+
AND ISNULL(ExistingValue, '') != AttributeValue
3847

3948
--------------------------------------------------------------------------------------------------------------------------------
4049
-- PULL PREVIOUS EXTENDED ATTRIBUTES
4150
--------------------------------------------------------------------------------------------------------------------------------
4251
DECLARE @PreviousExtendedAttributes XML
4352

44-
SELECT @PreviousExtendedAttributes = AttributesXml
45-
FROM ExtendedAttributeIndex
53+
SELECT TOP 1
54+
@PreviousExtendedAttributes = AttributesXml
55+
FROM ExtendedAttributes
4656
WHERE TopicID = @TopicID
57+
ORDER BY Version DESC
4758

4859
--------------------------------------------------------------------------------------------------------------------------------
4960
-- ADD EXTENDED ATTRIBUTES, IF CHANGED
@@ -76,16 +87,17 @@ SELECT @TopicID,
7687
AttributeKey,
7788
'',
7889
@Version
79-
FROM @Attributes NullAttributes
90+
FROM @Attributes New
91+
CROSS APPLY (
92+
SELECT TOP 1
93+
AttributeValue AS ExistingValue
94+
FROM Attributes
95+
WHERE TopicID = @TopicID
96+
AND AttributeKey = New.AttributeKey
97+
ORDER BY Version DESC
98+
) Existing
8099
WHERE IsNull(AttributeValue, '') = ''
81-
AND (
82-
SELECT TOP 1
83-
AttributeValue
84-
FROM Attributes
85-
WHERE TopicID = @TopicID
86-
AND AttributeKey = NullAttributes.AttributeKey
87-
ORDER BY Version DESC
88-
) != ''
100+
AND ExistingValue != ''
89101

90102
--------------------------------------------------------------------------------------------------------------------------------
91103
-- REMOVE EXISTING RELATIONS

0 commit comments

Comments
 (0)