|
|
|
Forum 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
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Monday, May 06, 2013 1:09 PM
Points: 15,439,
Visits: 9,569
|
|
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
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 6:23 AM
Points: 32,903,
Visits: 26,784
|
|
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."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 6:23 AM
Points: 32,903,
Visits: 26,784
|
|
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."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Monday, March 25, 2013 8:37 AM
Points: 163,
Visits: 275
|
|
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.
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Monday, May 06, 2013 1:09 PM
Points: 15,439,
Visits: 9,569
|
|
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
|
|
|
|
|
Forum 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. ====================================================================================================
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Yesterday @ 11:36 PM
Points: 85,
Visits: 252
|
|
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
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 6:23 AM
Points: 32,903,
Visits: 26,784
|
|
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."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 6:23 AM
Points: 32,903,
Visits: 26,784
|
|
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."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|