Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
kneekill
Yak Posting Veteran
76 Posts |
Posted - 2007-11-15 : 01:42:02
|
hi,i am having a performance issue using a scalar-valued functionmy table structure EmployeesID Employee code-- -------- ----1 XXX a1 XXX b2 YYY a2 YYY b2 YYY c3 ZZZ aMy select statementselect ID,Employee,dbo.fnConcateEmployeeCode(ID) from Employeesscalar-valued functionALTER FUNCTION [dbo].[fnConcateEmployeeCode](@ID int)RETURNS VARCHAR(8000)ASBEGIN DECLARE @Output VARCHAR(8000) SELECT @Output = COALESCE(@Output+',', '') + CONVERT(varchar(20),E.Code) FROM dbo.Employees WHERE E.[ID]=@OrderID ORDER BY E.ID RETURN @OutputENDthe result would be1 XXX a,b2 YYY a,b,c3 ZZZ athe query is working fine, if the where condition in select statement is narrow (i.e select on specific rows)in some cases i need to select the whole table using this query (the table has thousands of records).the select is taking very long to execute.i get a connection time out erroris there any other way around to achieve this functionality,or how can i optimize the function to get faster results.Note:The query gives the results very fast on whole table, below 1 secif i make the select directly on the table without the function |
|
kneekill
Yak Posting Veteran
76 Posts |
Posted - 2007-11-15 : 03:23:43
|
i tried it even this way belowDECLARE @listStr VARCHAR(MAX)SET @listStr = ''SELECT @listStr = @listStr + E.Code+ ',' FROM dbo.Employees E Where E.ID=@ID ORDER BY E.ID SELECT @listStr = SUBSTRING(@listStr , 1, LEN(@listStr)-1)RETURN @listStri still have the same problem......... |
 |
|
lampan
Starting Member
4 Posts |
Posted - 2007-11-15 : 04:36:01
|
Since the function you have written will get called per row, it will obviously kill performance.You can keep a table with Id & concated codes. But then you will need to update this table everytime Employees table gets updated.. OR you can add a column to you Employee master table [where Id will be PK], & this col will always have updated concated codes.Hope this helps.. |
 |
|
kneekill
Yak Posting Veteran
76 Posts |
Posted - 2007-11-15 : 04:53:15
|
i have already tried the above way which you have mentioned,I have created the table with ID and Concatenated CodesBut filling the new table with Concatenated Codes is taking more than 30 minsAnd the second approach Which you have mentioned cant be feasible,because i cant have ID as primary key,ID gets repeated in the table....can we have any other approach, other than using functions ..........i even tried writing a cursor but the same results....DECLARE @Code_UniqueID Varchar(20),@CodeList varchar(100)SET @PCodeList = ''DECLARE crs_Code CURSORFOR SELECT E.CodeFROM Employees E WHERE E.[ID]=@IDORDER BY E.ID OPEN crs_CodeFETCH NEXT FROM crs_Code INTO @Code_UniqueIDWHILE @@FETCH_STATUS = 0BEGIN SELECT @CodeList = @CodeList+CAST(@Code_UniqueID AS varchar(20))+ '; ' FETCH NEXT FROM crs_Code INTO @Code_UniqueIDENDSET @CodeList = SUBSTRING(@CodeList,1,DATALENGTH(@CodeList)-2)CLOSE crs_CodeDEALLOCATE crs_Code--SELECT @CodeListReturn @CodeListEND |
 |
|
|
|
|
|
|