Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6878761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:45:49+00:00 2026-05-27T04:45:49+00:00

Database: SQL Server 2005 I’m using a function that creates a comma separated list

  • 0

Database: SQL Server 2005

I’m using a function that creates a comma separated list using the COALESCE function.

ALTER FUNCTION [dbo].[func_Codes](@CustID int, @GroupID int)
returns varchar(1000) as
BEGIN
    DECLARE @List varchar(1000)

    SELECT @List = COALESCE(@List + ',', '') + Code
    FROM dbo.vw_CustBillingInfo 
    WHERE dbo.vw_CustBillingInfo.CustID = @CustID
        AND dbo.vw_CustBillingInfo.GroupID = @GroupID

    RETURN @List
END

The view it calls uses the following setup:

SELECT <columns>
FROM (SELECT <columns>
      FROM Customer
      INNER JOIN Codes ON dbo.GetRootCode(Customer.Code) = Codes.SpecialCode 
      OR (IsNumeric(Customer.Code) = 0 AND Substring(Customer.Code,2,3) = Codes.SpecialCode)

      UNION

      SELECT <columns>
      FROM Customer
      INNER JOIN Codes ON dbo.GetRootCode(Customer.Code2) = Codes.SpecialCode 
         OR (IsNumeric(Customer.Code2) = 0 AND Substring(Customer.Code2,2,3) = Codes.SpecialCode)

      UNION

      <Repeat a few more similar unions>

The dbo.GetRootCode(code) scalar function does some substring operations on the column to pull out a special part of the varchar column.

If I take the query out of the view I can improve performance by creating a temp table and storing the values of GetRootCode(Customer.Code), IsNumeric(Customer.Code), Substring(Customer.Code,2,3), etc for each code used. This doesn’t seem like the best approach but it shows that optimizations can be done. Also a view can’t use a temp table anyway.

Instead of the function to create the comma separated list I tried an xml variation I saw multiple places online.

STUFF((
    SELECT ','+Code FROM dbo.vw_CustBillingInfo WHERE dbo.vw_CustBillingInfo.CustID = C.CustID AND dbo.vw_CustBillingInfo.GroupID = C.GroupID FOR XML PATH('')
        ), 1, 1, '')

However, while this worked, performance was fairly awful. It added about 20 to 30 seconds to the execution time of the query

This leads me back to trying to optimize the view directly for better performance. IO statistics shows the first select of the view to be:

(20 row(s) affected)
Table 'Worktable'. Scan count 1, logical reads 42920, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Customer'. Scan count 1, logical reads 269, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Codes'. Scan count 1, logical reads 2, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

This takes over a second to execute. Add that up over all unions and then this view is called twice over the execution path, and the time greatly adds up.

I’m stuck on the best way to optimize the INNER JOIN of the view to increase the speed and lessen the IO required. Any suggestions would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:45:49+00:00Added an answer on May 27, 2026 at 4:45 am

    You are using TWO non-sargable JOIN conditions per JOIN.

    So you are getting at a minimum a table scan per JOIN, possible two depending on your implementation.

    The really short answer is:

    • Don’t JOIN on UDFs
    • Don’t JOIN using any other functions (like SUBSTRING).

    There is almost NO WAY to optimize this. SQL doesn’t know what the output will be til it runs the function, so it runs it against each and every row.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've ran some ALTER scripts on the database [SQL Server Server 2005], and overwrote
I've created a web enabled database using SQL Server 2005 to store the data
I have a column in the database (SQL Server 2005) that has data with
I need to drop a perticular database in SQL Server 2005 using T-SQL. I
Using SQL Server 2005 I attached the database in Windows authentication Mode, then it
I've got a database from SQL Server 2005 that I am programmatically attaching to
I am using visual studio 2008 with my database in sql server 2005. I
How to get detailed list of connections to database in sql server 2005?
Using Entity Designer in VS2010 to build a database for SQL Server 2005. Having
We have a database (SQL Server 2005) which we would like to get under

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.