If I have the following table structure...
Table 1: BlogPost
PostId | Name | Text
Table 2: Tags
TagId | Tag
Table 3: BlogPostTag
PostId | TagId
And the following stored procedure...
CREATE PROCEDURE SearchBlogPosts
@tagstring nvarchar(max),
AS
BEGIN
DECLARE @searchTags TABLE (Tag varchar(50));
IF @tagstring IS NOT NULL AND @tagstring <> ''
BEGIN
INSERT INTO @tags SELECT s AS tag FROM dbo.Split(',',@tagstring);
END
SELECT * FROM BlogPost b
JOIN BlogPostTags bt on bt.PostId = b.PostId
JOIN Tags t on t.TagId = bt.TagId
JOIN @searchTags st ON st.Tag = t.Tag
...
(Other Joins and where clauses may exist below here)
END
...what is the most "performant" manner in which I could exclude the joins on the tag tables if @tagstring is null or blank?