I've written several functions similar to the one below, which work quickly and efficently. However, this particular function runs incredibly slow when a large set of data is thrown against it. There are indexes on all of the keyed columns that I am working with. Is there something here that I am missing that is causing this fucntion to perform terribly?
Code:
create function fn_node_address (@machine_sk int)
returns varchar(4000)
as
begin
declare @f_node_address varchar(50),
@node_address_list varchar(4000)
select @node_address_list = '' -- initialize the variable to something other than null
declare node_address_cursor cursor for
select c.node_address
from machine a,
net_interface b,
node_address c
where a.machine_sk = @machine_sk
and a.machine_sk = b.machine_sk
and b.net_interface_sk = c.net_interface_sk
order by 1
open node_address_cursor
fetch node_address_cursor
into @f_node_address
while (@@fetch_status <> -1)
begin
set @node_address_list = @node_address_list + rtrim(@f_node_address) + ', '
fetch node_address_cursor
into @f_node_address
end
close node_address_cursor
deallocate node_address_cursor
if len(@node_address_list) > 0
return substring(@node_address_list,1,len(@node_address_list)-1) -- remove the trailing comma
return @node_address_list
end