SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Generating a Distinct Delimited List Using XML


Generating a Distinct Delimited List Using XML

Author
Message
chris.fauvel
chris.fauvel
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 15
Bradley Deem (6/29/2010)
I recommend using a CLR for this. Microsoft has a great example here http://msdn.microsoft.com/en-us/library/ms131056.aspx. I'm curious what the performance differences are. Admittedly, I have not compared the two because I've never had the CLR function not perform adequately.

You can then use this as an aggregate, for example.

SELECT dbo.List(myColumn)
FROM myTable
GROUP BY SomeOtherColumn



What version of SQLSERVER are you running with CLR? Any issues with the server crashing? Could the server crash if the CLR has an unhandled exception?

I see the power of CLR, but the boss and I are scared for the database.

Thanks

Chris
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)

Group: General Forum Members
Points: 86874 Visits: 41104
Mike C (6/29/2010)
How's this for data? Smile


CREATE TABLE #Role
(
RoleName VARCHAR(100)
);
GO

INSERT INTO #Role
(
RoleName
)
VALUES ('Accounting'), ('Approver'), ('Developer'), ('International Sales Manager'), ('Marketing'), ('System Administrator'),
('Technical Customer'), ('Technical Director'), ('Training');
GO

CREATE TABLE #UserRole
(
UserID INT NOT NULL,
RoleName VARCHAR(100) NOT NULL,
ProjectID INT NOT NULL,
RoleAssignedDate DATETIME NOT NULL,
PRIMARY KEY
(
UserID,
RoleName,
ProjectID,
RoleAssignedDate
)
);
GO

CREATE TABLE #Numbers
(
Num INT NOT NULL PRIMARY KEY
);
GO

WITH DIGITS
AS
(
SELECT 0 AS Num
UNION ALL
SELECT Num + 1
FROM DIGITS
WHERE Num < 10
)
INSERT INTO #Numbers
(
Num
)
SELECT Num
FROM DIGITS;
GO

WITH NUMBERS
AS
(
SELECT HundredThousand.Num * 100000 + TenThousand.Num * 10000 +
Thousand.Num * 1000 + Hundred.Num * 100 +
Ten.Num + One.Num AS Num
FROM #Numbers HundredThousand
CROSS JOIN #Numbers TenThousand
CROSS JOIN #Numbers Thousand
CROSS JOIN #Numbers Hundred
CROSS JOIN #Numbers Ten
CROSS JOIN #Numbers One
),
RANDOMDATA
AS
(
SELECT Num AS UserID,
RoleName,
ABS(CHECKSUM(NEWID()) % 8000) + 1000 AS ProjectID,
DATEADD(DAY, -ABS(CHECKSUM(NEWID()) % 1000), GETDATE()) AS RoleAssignedDate,
NEWID() AS SortOrder
FROM NUMBERS
CROSS JOIN #Role
)
INSERT INTO #UserRole
(
UserID,
RoleName,
ProjectID,
RoleAssignedDate
)
SELECT TOP(1000000) UserID,
RoleName,
ProjectID,
RoleAssignedDate
FROM RANDOMDATA
ORDER BY SortOrder;
GO



Nice to see someone else ramp it up for a change. Nicely done Michael. :-D

The next thing we need to teach is that returning results to the screen is a performance testing error known as "The Great Equalizer". Hehe

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Bradley Deem
Bradley Deem
Say Hey Kid
Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)

Group: General Forum Members
Points: 671 Visits: 1248
chris.fauvel (6/30/2010)
Bradley Deem (6/29/2010)
I recommend using a CLR for this. Microsoft has a great example here http://msdn.microsoft.com/en-us/library/ms131056.aspx. I'm curious what the performance differences are. Admittedly, I have not compared the two because I've never had the CLR function not perform adequately.

You can then use this as an aggregate, for example.

SELECT dbo.List(myColumn)
FROM myTable
GROUP BY SomeOtherColumn



What version of SQLSERVER are you running with CLR? Any issues with the server crashing? Could the server crash if the CLR has an unhandled exception?

I see the power of CLR, but the boss and I are scared for the database.

Thanks

Chris


CLR is just like any other feature. Spend some time researching the in and outs so you will be fine. In regards to your questions, this CLR function was implemented into 2005 and we recently upgraded to 2008 without any problems (in regards to the CLR). If your CLR throws an unhandled exception it will be returned to SQL; it will not crash your server. Of course, there are caveats, you could always build a CLR that could bring down a server, but I would find it hard to believe you'd knowingly put such volatile code in a CLR untested for production.

On a side note, one of the changes to CLR in 2008 is removing the restriction of 8000 byte max. This means you could return a list of elements of size varchar(max).
chris.fauvel
chris.fauvel
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 15
Bradley Deem (6/30/2010)
chris.fauvel (6/30/2010)
Bradley Deem (6/29/2010)
I recommend using a CLR for this. Microsoft has a great example here http://msdn.microsoft.com/en-us/library/ms131056.aspx. I'm curious what the performance differences are. Admittedly, I have not compared the two because I've never had the CLR function not perform adequately.

You can then use this as an aggregate, for example.

SELECT dbo.List(myColumn)
FROM myTable
GROUP BY SomeOtherColumn



What version of SQLSERVER are you running with CLR? Any issues with the server crashing? Could the server crash if the CLR has an unhandled exception?

I see the power of CLR, but the boss and I are scared for the database.

Thanks

Chris


CLR is just like any other feature. Spend some time researching the in and outs so you will be fine. In regards to your questions, this CLR function was implemented into 2005 and we recently upgraded to 2008 without any problems (in regards to the CLR). If your CLR throws an unhandled exception it will be returned to SQL; it will not crash your server. Of course, there are caveats, you could always build a CLR that could bring down a server, but I would find it hard to believe you'd knowingly put such volatile code in a CLR untested for production.

On a side note, one of the changes to CLR in 2008 is removing the restriction of 8000 byte max. This means you could return a list of elements of size varchar(max).


Unfortunately we are still at 2005, we have plans to go to 2008, but there is SOOOOO MUCH regression testing that it would preclude enhancements to the apps for several months. :-( ugh
Mike C
Mike C
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2655 Visits: 1168
Jeff Moden (6/30/2010)

Nice to see someone else ramp it up for a change. Nicely done Michael. :-D

The next thing we need to teach is that returning results to the screen is a performance testing error known as "The Great Equalizer". Hehe


As usual you're 2 steps ahead of me Jeff :smile:

The only thing I wanted to add is that a performance test should be run multiple times to get a good read, they need to be sure to clean buffers and drop cache, and the difference between 2 and 4 ms in performance tuning terms is literally a rounding error Smile

Mike C
Mike C
Mike C
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2655 Visits: 1168
chris.fauvel (6/30/2010)
Bradley Deem (6/29/2010)
I recommend using a CLR for this. Microsoft has a great example here http://msdn.microsoft.com/en-us/library/ms131056.aspx. I'm curious what the performance differences are. Admittedly, I have not compared the two because I've never had the CLR function not perform adequately.

You can then use this as an aggregate, for example.

SELECT dbo.List(myColumn)
FROM myTable
GROUP BY SomeOtherColumn



What version of SQLSERVER are you running with CLR? Any issues with the server crashing? Could the server crash if the CLR has an unhandled exception?

I see the power of CLR, but the boss and I are scared for the database.

Thanks

Chris


For this task you could build a SQL CLR assembly using the strictest permission set (SAFE), which indicates all managed code and no access to any external resources. It's highly unlikely you could write an assembly that would corrupt the memory space with SAFE permissions, but even if you could it would be limited to just the managed memory space and wouldn't bring down the server.

There are some things to know about using SQL CLR:

* SQL Server caches SQL CLR assemblies after it loads them the first time, for efficiency. In the case of memory pressure, SQL CLR assemblies are one of the first things to get unloaded. If you are on a machine with a lot of memory pressure SQL Server will need to reload your assemblies every time, adding overhead and cutting into any efficiency gains from using SQL CLR.

* When you're doing string concatenation operations like this you probably want to use an efficient .NET StringBuilder object to maximize efficiency. Normal string concatenation is notoriously slow because strings are immutable (a copy of the string is made everytime it is modified).

* SQL Server can't accurately cost a SQL CLR assembly in a query plan since it has no idea what you're doing in there.

* In some cases SQL CLR can prevent parallelization in query plans. When you pass in LOB (varchar(max), etc.) parameters to it, for instance. That may not be an issue in this case, but is something to be aware of.

* You need to explicitly handle NULLs on the .NET side when you use SQL CLR. One of the biggest mistakes I've seen is SQL CLR code that pretends NULLs don't exist. That usually results in hard-to-troubleshoot exceptions from the .NET side.

If you keep these things in mind, you shouldn't have any problems with SQL CLR. This particular exercise would actually make a very nice introduction to SQL CLR.

Mike C
Puja Shah
Puja Shah
Old Hand
Old Hand (318 reputation)Old Hand (318 reputation)Old Hand (318 reputation)Old Hand (318 reputation)Old Hand (318 reputation)Old Hand (318 reputation)Old Hand (318 reputation)Old Hand (318 reputation)

Group: General Forum Members
Points: 318 Visits: 237
Hi all,

Thank you very much. I liked your suggestions & different but better solutions.


Regards,
Puja
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search