a four page long query (that creates two temporal tables, and then joins them) in sql server runs in a dozen of seconds (thanks to parallel execution). The same query packed into a user defined function (with temp tables replaced by table-variables as those are not supported in UDF) takes a dozen of minutes to compute (thanks for no-parallelism).
If I inspect the query plans, they are exactly the same, except the UDF version does not do parallel execution with SQL Server mentioning (I omit all the details as the query plans barely fit into A0 size page):
<QueryPlan NonParallelPlanReason="CouldNotGenerateValidParallelPlan"
is there any "cheap" way to tell the sql server to try to parallelize the SQL Statements inside the UDFs?
The prototype of the function is:
CREATE FUNCTION import (@ts_start datetime2(3), @ts_end datetime2(3))
RETURNS @imports TABLE (id uniqueidentifier)
while previously those two parameters where given in a declare variable:
declare
@ts_start datetime2(3) = '2020-06-22 00:00:00'
, @ts_end datetime2(3) = '2020-06-23 00:00:00'
Any way to parallelize the sqls inside a udf in sql server?
RETURNS @imports TABLE (id uniqueidentifier)
) why not use an inline TVF (RETURNS TABLE
)?