Query optimisation for comma separated output.
CREATE Table #Final
(
    form_id                bigint,
    msg_id                bigint,
    instance_group_id    bigint,
    user_id                bigint
)--this table have 4688432 records
Create table #tmpNoAccess
(
    instance_group_id    bigint,
    user_id                bigint
)--this table have zero record


SELECT f1.form_id, f1.msg_id,
    REPLACE((    SELECT CONVERT(NVARCHAR, f2.user_id )+ ','
                FROM #Final f2
                WHERE f2.msg_id = f1.msg_id
                GROUP BY F2.user_id
                FOR XML PATH('')) + '$', ',$', ''
            ) as user_access_group,
    REPLACE((    SELECT CONVERT(NVARCHAR, f2.user_id )+ ','
                FROM #tmpNoAccess f2
                WHERE f2.instance_group_id = f1.instance_group_id
                GROUP BY F2.user_id
                FOR XML PATH('')) + '$', ',$', ''
            ) as blockedusers
FROM #Final f1
GROUP BY f1.form_id, f1.msg_id, f1.instance_group_id

above Query take a 1 min and
February 24th, 2015 6:01am

Add an index on #Final.msg_id with user_id included:

CREATE INDEX ix ON #Final (msg_id) INCLUDE user_id)

Free Windows Admin Tool Kit Click here and download it now
February 24th, 2015 6:10am

Hello Erland,

Thank you very much for reply

is there any faster another way to get comma separated value?

Regard with

Vishnu.

February 24th, 2015 6:15am

You can check with CLR function:

Refer the below:

http://www.mssqltips.com/sqlservertip/1691/sql-server-clr-function-to-concatenate-values-in-a-column/

Free Windows Admin Tool Kit Click here and download it now
February 24th, 2015 6:47am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics