Skip to content
Advertisement

Generate sequence in SQL Server, poor performance with cross apply

I obtained the following code from the web many years ago and it has served my very well. It is simply a function that generates a sequence of numbers from 1 to whatever you pass in.

Basically it’s a way of doing a for loop in a SQL statement.

This generally works very well and is fast but I have found its performance to be very poor when using a cross apply statement, eg

This is very slow on my machine. Does anyone know why it works fine when executed once but runs very badly when executed 3 times via cross apply? Even if the @T table contains only 1 row the performance is still terrible. Is there a better way to write this?

Thanks in advance, Michael

Advertisement

Answer

The query optimizer figures it is better to only execute the function once and then use the result in a join to get the rows you want. It is done like that because your function is an inline table valued function. If you instead make your function a multi-statement valued function it will execute the function once for each row in your source table. However I would recommend that you create a numbers table instead as suggested by Igor.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement