This is because you are calling the function for each row of Table1, so that kills your performance.
The second point is that you are calling table1 many times with joins, that add some time.
I don't know if @strList = COALESCE(@strList + ' ', '') + CAST(Table3.Name AS VARCHAR)
is less efficient than FOR XML PATH
and STUFF
to concatenate the multiple rows Name into a single row, but u can try it.
Also, I suggest you to do all your stuff in a unique query without using function (not the best for performance).
Try this :
SELECT
t.ID
,STUFF((
SELECT ' ,' + CAST(Table3.Name AS VARCHAR)
FROM
Table1 as t1
JOIN Table2
ON Table2.Table1_ID = Table1.ID
JOIN Table3
ON Table3.ID = Table2.Table3_ID
WHERE t1.ID = t.ID
FOR XML PATH('')
), 1, 1, '') as "cc"
FROM
Table1 as t
Leave me a comment.