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


Interesting problem for the "Running Total/Quirky Update" method of removing cursors


Interesting problem for the "Running Total/Quirky Update" method of removing cursors

Author
Message
Robb Melancon
Robb Melancon
SSC-Enthusiastic
SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)

Group: General Forum Members
Points: 109 Visits: 110
So I'm running into the following:
The statement terminated. The maximum recursion 100 has been exhausted before statement completion

And looks like the max is 32,000, did you run this on the million row table?
Garadin
Garadin
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2795 Visits: 4107
Although the max recursion is only a bit over 32K, any change in criteria will restart that recursion. You could easily run it on millions of rows... or it could crap out at 33k, it all depends on the data distribution.

Seth Phelabaum
Consistency is only a virtue if you're not a screwup. ;-)

Links: How to Post Sample Data :: Running Totals :: Tally Table :: Cross Tabs/Pivots :: String Concatenation
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16482 Visits: 19557
Robb Melancon (5/24/2010)
So I'm running into the following:
The statement terminated. The maximum recursion 100 has been exhausted before statement completion

And looks like the max is 32,000, did you run this on the million row table?


OPTION (MAXRECURSION 0)

I'm still looking for the thread, Robb...and yes, a million rows in about 9 seconds using a recursive CTE. BTW credit to Paul White for discovering the performance improvement returned by ensuring the clustered index is unique.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16482 Visits: 19557
Hi Robb, here's some stuff for you to play with. Note that the join to the recursive part in the rCTE is critical.

DROP TABLE #Numbers
SELECT TOP 1000000 --000
n = ROW_NUMBER() OVER (ORDER BY a.name),
CalcValue = CAST(NULL AS BIGINT)
INTO #Numbers
FROM master.dbo.syscolumns a, master.dbo.syscolumns b
CREATE UNIQUE CLUSTERED INDEX CIn ON #Numbers ([n] ASC)
-- (1,000,000 row(s) affected)

SET STATISTICS IO ON
SET STATISTICS TIME ON

-- 'Quirky' update
DECLARE @Lastval INT = 0, @CalcValue BIGINT
UPDATE #Numbers SET
@CalcValue = CalcValue = (@Lastval + n),
@Lastval = n
-- (1,000,000 row(s) affected) / CPU time = 2968 ms, elapsed time = 3079 ms.
-- Table #Numbers... Scan count 1, logical reads 3113, physical reads 6, read-ahead reads 3146, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

-- Recursive CTE
;WITH Calculator (n, CalcValue) AS (
SELECT n.n, CalcValue = CAST(n.n AS BIGINT)
FROM #Numbers n
WHERE n.n = 1
UNION ALL
SELECT n.n, CalcValue = n.n + c.n
FROM #Numbers n
INNER JOIN Calculator c ON n.n = c.n + 1 -- nice
--INNER JOIN Calculator c ON c.n + 1 = n.n -- nice
--INNER JOIN Calculator c ON c.n = n.n - 1 -- slow
--INNER JOIN Calculator c ON n.n - c.n = 1 -- slow
--INNER JOIN Calculator c ON c.n - n.n = -1 -- slow
)
SELECT n, CalcValue
FROM Calculator
OPTION (MAXRECURSION 0)
-- (1,000,000 row(s) affected) / CPU time = 33297 ms, elapsed time = 36161 ms.
-- Table 'Worktable'. Scan count 2, logical reads 6000001, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

SET STATISTICS IO Off
SET STATISTICS TIME Off



There's some more here.

Cheers

ChrisM

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
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