Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««123

Generating a Distinct Delimited List Using XML Expand / Collapse
Author
Message
Posted Wednesday, June 30, 2010 6:44 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, December 4, 2013 12:41 PM
Points: 5, Visits: 13
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
Post #945425
Posted Wednesday, June 30, 2010 6:53 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 3:39 PM
Points: 37,101, Visits: 31,653
Mike C (6/29/2010)
How's this for data? :)

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.

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


--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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #945427
Posted Wednesday, June 30, 2010 8:11 AM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Yesterday @ 5:54 AM
Points: 554, Visits: 1,197
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).
Post #945506
Posted Wednesday, June 30, 2010 8:19 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, December 4, 2013 12:41 PM
Points: 5, Visits: 13
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

Post #945515
Posted Wednesday, June 30, 2010 9:20 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, September 15, 2014 5:20 PM
Points: 1,276, Visits: 1,134
Jeff Moden (6/30/2010)

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

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


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 :)

Mike C
Post #945593
Posted Wednesday, June 30, 2010 9:50 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, September 15, 2014 5:20 PM
Points: 1,276, Visits: 1,134
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
Post #945618
Posted Friday, July 2, 2010 12:01 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Thursday, July 17, 2014 2:00 PM
Points: 230, Visits: 237
Hi all,

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


Regards,
Puja
Post #946596
« Prev Topic | Next Topic »

Add to briefcase «««123

Permissions Expand / Collapse