Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 Fibonacci numbers using a function. Rate Topic Display Mode Topic Options
Author
 Message
 Posted Sunday, February 17, 2013 6:08 PM
 Hall of Fame Group: General Forum Members Last Login: Wednesday, February 24, 2016 6:28 AM Points: 3,977, Visits: 6,431
 You can also do this with a recursive CTE, which can be put into an iTVF.See the first example, in the third article in my signature links. My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!My thought question: Have you ever been told that your query runs too fast?My advice:INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.Need to UNPIVOT? Why not CROSS APPLY VALUES instead?Since random numbers are too important to be left to chance, let's generate some!Learn to understand recursive CTEs by example.Splitting strings based on patterns can be fast!My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Post #1421017
 Posted Monday, February 18, 2013 12:32 PM
 SSC-Forever Group: General Forum Members Last Login: Yesterday @ 3:23 PM Points: 42,036, Visits: 39,415
 dwain.c (2/17/2013)You can also do this with a recursive CTE, which can be put into an iTVF.See the first example, in the third article in my signature links.Yes, you could. But even Peter Larsson's super trimmed down rCTE has a "counting rCTE" as a base. It may not matter much for single use but the formula method runs in sub-millisecond times whereas the rCTE takes 38 milliseconds (on my ever so humble desktop) for elements 0 through 70. You should check out the rads, as well. Recursion just ins't the answer for things like this unless it's to build a lookup table just one time. --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." Helpful Links:How to post code problemsHow to post performance problems
Post #1421326
 Posted Monday, February 18, 2013 5:31 PM
 Hall of Fame Group: General Forum Members Last Login: Wednesday, February 24, 2016 6:28 AM Points: 3,977, Visits: 6,431
 Jeff Moden (2/18/2013)dwain.c (2/17/2013)You can also do this with a recursive CTE, which can be put into an iTVF.See the first example, in the third article in my signature links.Yes, you could. But even Peter Larsson's super trimmed down rCTE has a "counting rCTE" as a base. It may not matter much for single use but the formula method runs in sub-millisecond times whereas the rCTE takes 38 milliseconds (on my ever so humble desktop) for elements 0 through 70. You should check out the rads, as well. Recursion just ins't the answer for things like this unless it's to build a lookup table just one time.I agree that the formula method would be faster even without running the actual test. That was a very clever approach by the way Jeff.I was just offering an alternative that could be put into an iTVF (unlike the loop). My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!My thought question: Have you ever been told that your query runs too fast?My advice:INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.Need to UNPIVOT? Why not CROSS APPLY VALUES instead?Since random numbers are too important to be left to chance, let's generate some!Learn to understand recursive CTEs by example.Splitting strings based on patterns can be fast!My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Post #1421389
 Posted Tuesday, February 19, 2013 9:26 AM
 SSC-Forever Group: General Forum Members Last Login: Yesterday @ 3:23 PM Points: 42,036, Visits: 39,415
 Ah. Understood. It would be interesting to find out which is actually faster, though. The mTVF with a loop or the iTVF with the recursive CTE. --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." Helpful Links:How to post code problemsHow to post performance problems
Post #1421720
 Posted Tuesday, February 19, 2013 5:12 PM
 Hall of Fame Group: General Forum Members Last Login: Wednesday, February 24, 2016 6:28 AM Points: 3,977, Visits: 6,431
 Jeff Moden (2/19/2013)Ah. Understood. It would be interesting to find out which is actually faster, though. The mTVF with a loop or the iTVF with the recursive CTE.Hehe. Normally I would take that challenge but I'm a bit swamped with other stuff going on right now. I agree that it would be interesting. My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!My thought question: Have you ever been told that your query runs too fast?My advice:INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.Need to UNPIVOT? Why not CROSS APPLY VALUES instead?Since random numbers are too important to be left to chance, let's generate some!Learn to understand recursive CTEs by example.Splitting strings based on patterns can be fast!My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Post #1421897
 Posted Wednesday, February 20, 2013 3:20 AM
 SSCrazy Group: General Forum Members Last Login: Yesterday @ 8:23 AM Points: 2,014, Visits: 22,604
 Using DECIMALs with Binet's formula you can get the first 92 in the series`WITH Constants(one,half,root5,phi,phi2,phi4,phi8,phi16,phi32,phi64) AS (SELECT CAST(1.0 AS DECIMAL(38,20)), CAST(0.5 AS DECIMAL(38,20)), CAST(2.2360679774997896964091736687313 AS DECIMAL(38,20)), CAST(1.6180339887498948482045868343656 AS DECIMAL(38,20)), CAST(2.6180339887498948482045868343656 AS DECIMAL(38,20)), CAST(6.8541019662496845446137605030969 AS DECIMAL(38,20)), CAST(46.978713763747791812296323521678 AS DECIMAL(38,20)), CAST(2206.9995468961462151779272055189 AS DECIMAL(38,20)), CAST(4870846.9999997946968976853425802 AS DECIMAL(38,20)), CAST(23725150497406.999999999999957851 AS DECIMAL(38,20)))SELECT n.Number, CAST(CAST((CASE WHEN n.Number & 64 = 0 THEN c.one ELSE c.phi64 END * CASE WHEN n.Number & 32 = 0 THEN c.one ELSE c.phi32 END * CASE WHEN n.Number & 16 = 0 THEN c.one ELSE c.phi16 END * CASE WHEN n.Number & 8 = 0 THEN c.one ELSE c.phi8 END * CASE WHEN n.Number & 4 = 0 THEN c.one ELSE c.phi4 END * CASE WHEN n.Number & 2 = 0 THEN c.one ELSE c.phi2 END * CASE WHEN n.Number & 1 = 0 THEN c.one ELSE c.phi END) / c.root5 + c.half AS BIGINT) AS VARCHAR(20)) AS FibNumberFROM master.dbo.spt_values nCROSS JOIN Constants cWHERE n.type='P' AND n.Number BETWEEN 1 AND 92;` ____________________________________________________Deja View - The strange feeling that somewhere, sometime you've optimised this query beforeHow to get the best help on a forumhttp://www.sqlservercentral.com/articles/Best+Practices/61537
Post #1422007
 Posted Monday, February 25, 2013 2:35 PM
 SSC Eights! Group: General Forum Members Last Login: Wednesday, October 26, 2016 2:18 PM Points: 917, Visits: 2,673
 Jeff Moden (2/16/2013)If we're going to do this, let's knock the problem right out of the park....We also come to the understanding that we can't directly calculate more than the 70th Fib number using this method because the FLOAT data-type returned by the SQRT function induces rounding errors after the precision of the return reaches just 15 digits....Agreed! Out of the park 1) Blazing speed, small maximum - Jeff did that!Out of the park 2) Hardware resource limited maximumWho's got a CLR arbitrary precision library for SQL Server?ETA: A .NET arbitrary precision library, though in alpha stage.And an integer only .NET arbitrary precision libraryMaybe even the .NET 4.0+ native System.Numerics.BigInteger
Post #1423778
 Posted Monday, February 25, 2013 5:13 PM
 SSC-Forever Group: General Forum Members Last Login: Yesterday @ 3:23 PM Points: 42,036, Visits: 39,415
 Who's got a CLR arbitrary precision library for SQL Server?BWAAAA-HAAAA!!! SQL Server is a very small park for these types of things. That brings me back to what I said earlier... I believe that if you need Fibonacce numbers larger than 70 or even the max in SQL Server, then you're probably using the wrong tool. --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." Helpful Links:How to post code problemsHow to post performance problems
Post #1423825
 Posted Tuesday, February 26, 2013 8:42 AM
 SSC Eights! Group: General Forum Members Last Login: Wednesday, October 26, 2016 2:18 PM Points: 917, Visits: 2,673
 Jeff Moden (2/25/2013)Who's got a CLR arbitrary precision library for SQL Server?BWAAAA-HAAAA!!! SQL Server is a very small park for these types of things. That brings me back to what I said earlier... I believe that if you need Fibonacce numbers larger than 70 or even the max in SQL Server, then you're probably using the wrong tool. If you need any Fibonacci numbers, SQL is the wrong tool. It's the wrong tool for 1, it's the wrong tool for 10, it's the wrong tool for 100. It's always the wrong tool.That's not the point - the point* is whether or not we can do it, and do it as efficiently as possible, as you already did for your constrained set :).I just want to remove the arbitrary limit! Regrettably, I've never so much as tried any CLR.*With a secondary point of poking fun at homework questions in a fun and constructive manner.
Post #1424116

 Permissions