Well, several days have passed but for a some reason I've started to feel uncomfortable about Numbers function. It's all because of poor recursive CTE implementation. I have decided to unroll the cycle. The new version hovewer isn't a beautiful but is providing much more superior performance comparing with previous implementation:
/* Returns numbers table. Table has a following structure: table(value int not null); value is an integer number that contains numbers from 1 to a specified value.*/create function dbo.Numbers( /* Number of rows to return. */ @count int)returns table asreturn with Number4(Value) as ( select 0 union all select 0 union all select 0 union all select 0 union all select 0 union all select 0 union all select 0 union all select 0 union all select 0 union all select 0 union all select 0 union all select 0 union all select 0 union all select 0 union all select 0 union all select 0 ), Number8(Value) as ( select 0 from Number4 union all select 0 from Number4 union all select 0 from Number4 union all select 0 from Number4 union all select 0 from Number4 union all select 0 from Number4 union all select 0 from Number4 union all select 0 from Number4 union all select 0 from Number4 union all select 0 from Number4 union all select 0 from Number4 union all select 0 from Number4 union all select 0 from Number4 union all select 0 from Number4 union all select 0 from Number4 union all select 0 from Number4 ), Number32(Value) as ( select 0 from Number8 N1, Number8 N2, Number8 N3, Number8 N4 ) select top(@count) row_number() over(order by Value) Value from Number32;
The performance achieved is on pair with numbers table. Estimated number of rows is precise whenever we pass constant as parameter.
What is the moral? - There is a space for the enhancements in the recursive CTE.
Next day
Guess what? - Yes! there is also the CLR, which allows to create one more implementation of the numbers and split functions. In the next entry I'll show it, and performance comparison of different approaches.
Remember Me
a@href@title, b, blockquote@cite, em, i, strike, strong, sub, super, u