Site icon The Parenting

Numbers table vs recursive CTE to generate a range of numbers

Why is using a numbers table so much faster than using a recursive CTE to generate them on the fly?

On my machine, given a table numbers with a single column n (primary key) containing the numbers from 1 to 100000, the following query:

select n from numbers;

Takes around 400 ms to finish.

Using a recursive CTE to generate the numbers 1 to 100000:

with u as (
    select 1 as n
    union all
    select n + 1
    from u
    where n < 100000
)
select n
from u
option(maxrecursion 0);

Takes about 900ms to finish, both on SQL Server 2019.

My question is, why is the second option so much slower than the first one? Isn’t the first one fetching results from disk, and should therefore be slower?

Otherwise, is there any way to make the CTE run faster? Because it seems to me it’s a more elegant solution than storing a list of numbers in a database.

Exit mobile version