Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL Server Multi-statement UDF - way to store data temporarily required
    primarykey
    data
    text
    <p>I have a relatively complex query, with several self joins, which works on a rather large table. For that query to perform faster, I thus need to only work with a subset of the data. Said subset of data can range between 12 000 and 120 000 rows depending on the parameters passed.</p> <p>More details can be found here: <a href="https://stackoverflow.com/questions/3054843/sql-server-cte-referred-in-self-joins-slow">SQL Server CTE referred in self joins slow</a></p> <p>As you can see, I was using a CTE to return the data subset before, which caused some performance problems as SQL Server was re-running the Select statement in the CTE for every join instead of simply being run once and reusing its data set.</p> <p>The alternative, using temporary tables worked much faster (while testing the query in a separate window outside the UDF body). However, when I tried to implement this in a multi-statement UDF, I was harshly reminded by SQL Server that multi-statement UDFs do not support temporary tables for some reason...</p> <p>UDFs do allow table variables however, so I tried that, but the performance is absolutely horrible as it takes 1m40 for my query to complete whereas the CTE version <em>only</em> took 40 seconds. I believe the table variables is slow for reasons listed in this thread: <a href="https://stackoverflow.com/questions/1643687/table-variable-poor-performance-on-insert-in-sql-server-stored-procedure">Table variable poor performance on insert in SQL Server Stored Procedure</a></p> <p>Temporary table version takes around 1 seconds, but I can't make it into a function due to the SQL Server restrictions, and I <em>have</em> to return a table back to the caller.</p> <p>Considering that CTE and table variables are both too slow, and that temporary tables are rejected in UDFs, What are my options in order for my UDF to perform quickly?</p> <p>Thanks a lot in advance.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload