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»»

Uncommon Table Expressions Expand / Collapse
Author
Message
Posted Monday, August 11, 2008 10:50 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, October 10, 2012 4:48 PM
Points: 4, Visits: 10
Does anyone consider the use of a CLR function for this type of processing?

For example:

[Microsoft.SqlServer.Server.SqlFunction]
public static int clr_occurs2(string target, char lookFor)
{
int occurs = 0;
for (int i = 0; i < target.Length; i++)
{
if (target[i] == lookFor)
{
occurs++;
}
}
return occurs;
}

seems to be about 5 times faster than the CTE version.

Greg
Post #550444
Posted Monday, August 11, 2008 3:13 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Thursday, December 18, 2014 9:58 AM
Points: 13,872, Visits: 9,600
On CLR, yes, it will be faster than the CTE on that.

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #550630
Posted Monday, August 11, 2008 5:20 PM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Today @ 6:23 AM
Points: 35,772, Visits: 32,445
GSquared (8/11/2008)
On CLR, yes, it will be faster than the CTE on that.


Does anyone want to race? ;)


--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 #550663
Posted Monday, August 11, 2008 5:40 PM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Today @ 6:23 AM
Points: 35,772, Visits: 32,445
Good article so far as the format, readability, and revelation go. The thing I'm having a problem with is the use of recursion of any kind to create a pseudo Tally table in the form of a CTE...

Let's strip everything away except creating the count and have a race between the recursive method and just a flat out lookup... the @BitBucket variable is used to have a place to "dump" the count to without having the IO system or the display system get involved...

    SET NOCOUNT ON
DECLARE @BitBucket INT
DECLARE @Top INT
SET @Top = 100000
--=============================================================================
-- Recursive CTE does the count
--=============================================================================
PRINT '========== Recursive CTE =========='
SET STATISTICS IO ON
SET STATISTICS TIME ON

;WITH cteTally
AS (
SELECT 1 AS N
UNION ALL
SELECT N+1 FROM cteTally WHERE N < @Top
)
SELECT @BitBucket = N
FROM cteTally
OPTION (MAXRECURSION 0)

SET STATISTICS TIME OFF
SET STATISTICS IO OFF
PRINT REPLICATE('=',100)

--=============================================================================
-- ROW_NUMBER CTE does the count
--=============================================================================
PRINT '========== ROW_NUMBER CTE =========='
SET STATISTICS IO ON
SET STATISTICS TIME ON

;WITH cteTally
AS (
SELECT TOP(@Top) ROW_NUMBER() OVER(ORDER BY sc1.Type) AS N
FROM Master.dbo.spt_Values sc1,
Master.dbo.spt_Values sc2
)
SELECT @BitBucket = N
FROM cteTally

SET STATISTICS TIME OFF
SET STATISTICS IO OFF
PRINT REPLICATE('=',100)

On my humble 6 year old 1.8Ghz P5, that code returns the following results...

========== Recursive CTE ==========
Table 'Worktable'. Scan count 2, logical reads 600001, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

SQL Server Execution Times:
CPU time = 3375 ms, elapsed time = 4132 ms.
====================================================================================================
========== ROW_NUMBER CTE ==========
Table 'spt_values'. Scan count 2, logical reads 18, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

SQL Server Execution Times:
CPU time = 63 ms, elapsed time = 67 ms.
====================================================================================================

Based on that, I'd recommend not using the RBAR found in recursion for anything except the occasional hierarchy problem. :D



--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 #550666
Posted Tuesday, August 12, 2008 8:17 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, November 25, 2014 9:36 AM
Points: 190, Visits: 442
Wow, some of these threads live a longer life than the merits of their content would seem to dictate. I posted his article to show how a new feature could be used in an unconventional way, not to present a new paradigm for TSQL programming

I agree with Jeff. Every technique should be tested before implementation, and tested again before the next implementation...

If there is an art to programming then this forum is our canvas!




PeteK
I have CDO. It's like OCD but all the letters are in alphabetical order... as they should be.
Post #551040
Posted Tuesday, August 12, 2008 8:43 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Thursday, December 18, 2014 9:58 AM
Points: 13,872, Visits: 9,600
Jeff Moden (8/11/2008)
GSquared (8/11/2008)
On CLR, yes, it will be faster than the CTE on that.


Does anyone want to race? ;)


Matt's already tested that on a prior thread. A recursive CTE is slower than a good CLR on that one. Numbers table is faster than both.


- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #551072
Posted Tuesday, August 12, 2008 9:13 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, October 10, 2012 4:48 PM
Points: 4, Visits: 10
And then there is the CLR version that comes in with the following results on my machine:

========== Recursive CTE ==========
Table 'Worktable'. Scan count 2, logical reads 600002, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

SQL Server Execution Times:
CPU time = 1375 ms, elapsed time = 1576 ms.
====================================================================================================
========== ROW_NUMBER CTE ==========
Table 'spt_values'. Scan count 2, logical reads 18, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

SQL Server Execution Times:
CPU time = 15 ms, elapsed time = 26 ms.
====================================================================================================
========== CLR Recursion ==========

SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 5 ms.
====================================================================================================
Post #551100
Posted Tuesday, August 12, 2008 6:25 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, November 5, 2014 11:24 PM
Points: 85, Visits: 262
Greg.Gum (8/12/2008)[Snip]
CLR Recursion SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 5 ms.


any chance of querying the cost column in the sys.dm_clr_appdomains
before and after the clr task ?

As It doesn't aggregate into statistics time CPU.

I have trouble believing that interpreting MSIL and recursion could be less effort than a pre bounded CTE with no virtual method calls.

see http://msdn.microsoft.com/en-us/library/ms187720.aspx
Post #551509
Posted Tuesday, August 12, 2008 6:26 PM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Today @ 6:23 AM
Points: 35,772, Visits: 32,445
Greg.Gum (8/12/2008)
And then there is the CLR version that comes in with the following results on my machine:


Cool! Would you mind posting the code?


--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 #551511
Posted Tuesday, August 12, 2008 7:12 PM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Today @ 6:23 AM
Points: 35,772, Visits: 32,445
Peter E. Kierstead (8/12/2008)
not to present a new paradigm for TSQL programming


Heh... sorry Peter... I just wanted to make sure no one adopted recursion as a new paradigm because of your good article. :)


--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 #551523
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse