Skip to content

Commit e725077

Browse files
committed
Optimize deduplication join in UpdateRelationships
In a previous commit, I introduced the ability to prevent `UpdateRelationships` from trying to insert duplicate `Target_TopicID`s. This isn't currently necessary since `SqlTopicRepository` always deletes the relationships (via `UpdateTopic`'s `@DeleteRelationships` flag) before calling `UpdateRelationships`. Nevertheless, in the case that this wasn't done, this avoids a primary key constraint violation if existing keys are sent via `@RelatedTopics`. Unfortunately, the process by which this was done has proven very expensive, due to the performance of user-defined, table-valued types. While those work fine for data transfer objects _to_ stored procedures, they are suboptimal for internal storage _within_ stored procedures. In this case, it's also completely unnecessary. To mitigate this, the local `@Existing_TopicIDs` storage is replaced with a new `LEFT JOIN` against the _actual_ `Relationships` table. This proves dramatically faster in testing (representing ≈12% of the test case's execution plan, as opposed to ≈38% with the previous model). Note that the test case includes inserting sample data into the `@RelatedTopics` parameter, so that ends up taking up _most_ of the time.
1 parent 5583d22 commit e725077

1 file changed

Lines changed: 5 additions & 21 deletions

File tree

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

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,6 @@ CREATE PROCEDURE [dbo].[UpdateRelationships]
1010
@RelatedTopics TopicList READONLY
1111
AS
1212

13-
--------------------------------------------------------------------------------------------------------------------------------
14-
-- DECLARE AND SET VARIABLES
15-
16-
DECLARE @Existing_TopicIDs TopicList
17-
18-
--------------------------------------------------------------------------------------------------------------------------------
19-
-- IDENTIFY EXISTING VALUES
20-
--------------------------------------------------------------------------------------------------------------------------------
21-
INSERT
22-
INTO @Existing_TopicIDs (
23-
TopicID
24-
)
25-
SELECT Target_TopicID
26-
FROM Relationships
27-
WHERE Source_TopicID = @TopicID
28-
AND RelationshipKey = @RelationshipKey
29-
3013
--------------------------------------------------------------------------------------------------------------------------------
3114
-- INSERT NOVEL VALUES
3215
--------------------------------------------------------------------------------------------------------------------------------
@@ -38,11 +21,12 @@ INTO Relationships (
3821
)
3922
SELECT @TopicId,
4023
@RelationshipKey,
41-
Target.TopicID
24+
TopicID
4225
FROM @RelatedTopics Target
43-
FULL JOIN @Existing_TopicIDs Existing
44-
ON Existing.TopicID = Target.TopicID
45-
WHERE Existing.TopicID is null
26+
LEFT JOIN Relationships Existing
27+
ON Target_TopicID = TopicId
28+
AND Source_TopicID = @TopicID
29+
WHERE Target_TopicID IS NULL
4630

4731
--------------------------------------------------------------------------------------------------------------------------------
4832
-- RETURN TOPIC ID

0 commit comments

Comments
 (0)