Skip to content

Commit 4035bcc

Browse files
committed
Refactored null attribute handling to use CROSS APPLY
There isn't any performance difference to using `CROSS APPLY`, but I find the syntax a bit cleaner than a nested query. It's worth noting that I tried caching the existing values in a local `AttributeValues` type as a performance improvement, but that was dramatically slower due to the cost of querying `AttributeIndex`, compared to the very fast subquery against `Attributes`.
1 parent b76ac69 commit 4035bcc

1 file changed

Lines changed: 10 additions & 9 deletions

File tree

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,17 @@ SELECT @TopicID,
7878
AttributeKey,
7979
'',
8080
@Version
81-
FROM @Attributes NullAttributes
81+
FROM @Attributes New
82+
CROSS APPLY (
83+
SELECT TOP 1
84+
AttributeValue AS ExistingValue
85+
FROM Attributes
86+
WHERE TopicID = @TopicID
87+
AND AttributeKey = New.AttributeKey
88+
ORDER BY Version DESC
89+
) Existing
8290
WHERE IsNull(AttributeValue, '') = ''
83-
AND (
84-
SELECT TOP 1
85-
AttributeValue
86-
FROM Attributes
87-
WHERE TopicID = @TopicID
88-
AND AttributeKey = NullAttributes.AttributeKey
89-
ORDER BY Version DESC
90-
) != ''
91+
AND ExistingValue != ''
9192

9293
--------------------------------------------------------------------------------------------------------------------------------
9394
-- REMOVE EXISTING RELATIONS

0 commit comments

Comments
 (0)