提高Union All查询的性能

时间:2015-03-19 16:14:18

标签: sql sql-server

我有一个非常扩展的Union All查询,Union All使用了34次,如下所示:

INSERT INTO #Temp2  
(RowNumber,ValFromUser,ColumnName,ValFromFunc,FuncWeight,percentage) 
SELECT RowNumber,@firstname,'firstname',
       PercentMatch,fw1.FunctionWeight,
       PercentMatch * fw1.FunctionWeight
FROM   
       dbo.MatchFirstName(@firstname, @checkbool) mfn

       CROSS JOIN

       dbo.FunctionWeights fw1
WHERE
     @firstname is not null and fw1.FunctionId = 1 

UNION ALL
SELECT RowNumber, @MiddleName,'Middlename',
       PercentMatch, fw2.FunctionWeight,
       PercentMatch * fw2.FunctionWeight 
FROM   
       dbo.MatchMiddleName(@MiddleName, @checkbool) mmn
       CROSS JOIN
       dbo.FunctionWeights fw2

WHERE
@MiddleName IS NOT NULL AND
     fw2.FunctionId = 2 

UNION ALL
SELECT RowNumber, @LastName,'Lastname',
       PercentMatch, fw3.FunctionWeight,
       PercentMatch * fw3.FunctionWeight 
FROM   
       dbo.MatchLastName(@LastName, @checkbool) mln 
       CROSS JOIN
       dbo.FunctionWeights fw3
WHERE
@LastName IS NOT NULL AND
     fw3.FunctionId = 3 

UNION ALL...........
and so on.

dbo.MatchFirstName是一个内联TVF,如下所示:

CREATE FUNCTION MatchFirstName (@FirstNameFromUser nvarchar(20) , @checkbool int)
    RETURNS TABLE AS RETURN
    select RowId As RowNumber, PercentMatch from dbo.MATCH(@FirstNameFromUser, 'firstname' , @checkbool ) 

GO

它正在调用一个dbo.Match函数,它是一个多行UDF,并且包含一些逻辑,用于根据作为参数传递给它的字段名称计算百分比匹配。
34联盟所有意味着35 Inline TVF's的结果使用基于35个不同列的选择查询进行协作。所有这些都属于一个表。 现在因为我无法承受35个索引,但我想避免表扫描,因为这个查询需要很长时间。

是否有改善绩效的工作?

0 个答案:

没有答案