I’m trying to optimize a query to remove the scalar valued function that returns a comma separated list. I’ve read online that making it inline should benefit performance. Could someone please help with making my UserDefinedFunction inline or recommend how to improve performance?
-- Description: Returns a comma separated list of Tags for a defined Course Enrolment
--
-- =============================================
/****** Object: UserDefinedFunction [dbo].[fn_ConcatenateCourseEnrolmentTags]
CREATE FUNCTION [dbo].[fn_ConcatenateCourseEnrolmentTags] (@ClientOfferRegID As int)
RETURNS nvarchar(3999) AS
BEGIN
DECLARE @Desc nvarchar(3999)
SELECT @Desc = IsNull(@Desc + ', ', '') + dbo.tbl_Tag_Shared.Name
FROM dbo.tbl_Tag_Shared INNER JOIN
dbo.tbl_Record_Tag ON dbo.tbl_Tag_Shared.TagId = dbo.tbl_Record_Tag.TagId INNER JOIN
dbo.tbl_Entity ON dbo.tbl_Tag_Shared.EntityId = dbo.tbl_Entity.EntityId
WHERE (dbo.tbl_Entity.ObjectId = '06') AND (dbo.tbl_Record_Tag.RecordId = @ClientOfferRegID)
ORDER BY dbo.tbl_Tag_Shared.Name
RETURN @Desc
END
GO
SELECT
tbl_Client.ClientID AS LearnerId,
tbl_Client.FirstName AS LearnerFirstName,
tbl_Client.LastName AS LearnerLastName,
dbo.fn_ConcatenateCourseEnrolmentTags(tbl__Client_Offer_Reg.ClientOfferRegID) AS Tags,
FROM
tbl_Client
...
GROUP BY
tbl_Client.ClientID,
tbl_Client.FirstName,
tbl_Client.LastName,
dbo.fn_ConcatenateCourseEnrolmentTags(tbl_Client_Offer_Reg.ClientOfferRegID),