Table Valued Function Killing My Query Performance

后端 未结 5 1271
盖世英雄少女心
盖世英雄少女心 2020-12-04 19:54

I was having a horrible time today trying to get a query to perform the way I would expect. I had to make a slight change to a table valued function that lives in the query

5条回答
  •  鱼传尺愫
    2020-12-04 20:18

    On the SQL Server 2014 we were able to solve our issue by inserting table value function data into temp table and then doing join on it. Instead of doing a join directly to table value function.

    This improved our execution time from 2 min to 4 secs.

    Here is an example that worked for our team:

    --SLOW QUERY (2 min):

    DECLARE @id INT = 1;
    
    SELECT * 
    FROM [data].[someTable] T
    INNER JOIN [data].[tableValueFunction](@id) TVF ON TVF.id = T.id;
    

    --FAST QUERY (4 sec):

    DECLARE @id INT = 1;
    
    SELECT * 
    INTO #tableValueFunction
    FROM [data].[tableValueFunction](@id) TVF
    
    SELECT * 
    FROM [data].[someTable] T
    INNER JOIN #tableValueFunction TVF ON TVF.id = T.id;
    

提交回复
热议问题