WHERE子句中的函数 - 处理性能不佳

时间:2018-02-27 14:32:37

标签: sql sql-server function tsql sql-tuning

我有一张超过100000条记录的大表。

  

我需要在用于评估totalSum的where子句中再添加一个条件,并仅返回带有totalSum<>的记录   0

此处使用了大量的连接和临时标签,我不打算将其全部发布。

这是我的功能:

CREATE FUNCTION returnTotalSum(@clientID VARCHAR(20),@type INT,@currency VARCHAR(20),@date VARCHAR())

RETURNS INT
AS
BEGIN

DECLARE @totalSum BIGINT;

SET @totalSum = ( SELECT SUM (CONVERT(DECIMAL(18,4),P.iznos*(1-P.dp)/2))
                   FROM pts as p
                     INNER JOIN tippart t on p.tip = @type
                     INNER JOIN its i on p.partija = @clientID                      
                        WHERE p.currency = @currency and pts.dknizenja < @date
                GROUP BY p.partija )


RETURN @totalSum
END

我在这里使用它:( WHERE子句中的最后一个AND)

... 
880001,
NULL, 
NULL,
NULL, 
NULL,
CONVERT(INT,REPLACE('2017.12.31','.',''))                 
            FROM ITS I WITH(NOLOCK)      
              JOIN TIPPART R WITH(NOLOCK) ON I.TIP = R.TIP       
              LEFT JOIN #UNPVT_TIPSTOPE_TS T (NOLOCK) ON I.KAMGRUPA = T.GRUPA AND I.TIP = T.TIP AND I.VALUTA = T.SIFVAL   
              LEFT JOIN #UNPVT_TIPSTOPE_TS T1 (NOLOCK) ON I.KAMGRUPA = T1.GRUPA AND I.TIP = T1.TIP AND I.VALUTA = T1.SIFVAL AND T.GRUPA IS NULL         
              LEFT JOIN #TMP_DODATNA_KS DS (NOLOCK) ON I.PARTIJA = DS.PARTIJA AND I.VALUTA = DS.SIFVAL AND I.KAMGRUPA = DS.GRUPA
              LEFT JOIN #NML_RATE N (NOLOCK)  ON I.TIP = N.TIP AND N.SIFVAL = I.VALUTA AND N.GRUPA = I.KAMGRUPA
             -- LEFT JOIN TIPSTOPE TS (NOLOCK) ON I.TIP = TS.TIP AND TS.GRUPA = I.KAMGRUPA AND TS.SIFVAL = I.VALUTA
              LEFT JOIN #NML_RATE_PERIOD NML (NOLOCK) ON I.TIP = NML.TIP AND I.VALUTA = NML.SIFVAL AND NML.GRUPA = I.KAMGRUPA AND NML.SIFVAL = I.VALUTA
             --WHERE NOT EXISTS (SELECT * FROM [dbo].[IC_INPT_AR_X_INT_RATE_SNPST] WHERE PARTIJA = I.PARTIJA AND VALUTA = I.VALUTA AND APP = 'ST')  
                  WHERE I.DOTVARANJE <= '2017.12.31'
                  AND (T.TIP IS NOT NULL 
                        OR T1.TIP IS NOT NULL 
                        OR DS.PARTIJA IS NOT NULL)
                        AND dbo.returnTotalSum(i.partija,r.tip,t.sifval,i.dotvaranje) <> 0

我认为这个问题是它必须遍历每条记录,比较,评估条件。 考虑到表中没有索引(我无法添加索引,因为我没有权限),它往往会永远运行。

  

我能做些什么来提高性能   功能,你有什么建议在旁边使用别的东西   功能和什么?

2 个答案:

答案 0 :(得分:1)

你的函数转换为内联表值函数应该接近于此。

CREATE FUNCTION returnTotalSum
(
    @clientID VARCHAR(20)
    ,@type INT
    ,@currency VARCHAR(20)
    ,@date VARCHAR(10) --Don't store dates as strings...
)
RETURNS TABLE AS RETURN

SELECT TotalSum = SUM(CONVERT(DECIMAL(18,4), P.iznos * (1 - P.dp) / 2))
FROM pts as p
INNER JOIN tippart t on p.tip = @type
INNER JOIN its i on p.partija = @clientID                      
WHERE p.currency = @currency 
    and pts.dknizenja < @date
GROUP BY p.partija

答案 1 :(得分:1)

在您的实现中,调用该函数作为过滤的一部分,导致巨大的性能消耗,可能是由于多个索引扫描。

作为一般指导,如果你没有这个功能就可以做到这一点,你应该能够减少它。

如果没有数据结构和样本数据,很难为您提供准确的解决方案。

请尝试以下代码:

    ... 
    880001,
    NULL, 
    NULL,
    NULL, 
    NULL,
    CONVERT(INT,REPLACE('2017.12.31','.','')) 
    FROM ITS I WITH(NOLOCK)      
        JOIN TIPPART R WITH(NOLOCK) ON I.TIP = R.TIP       
        LEFT JOIN #UNPVT_TIPSTOPE_TS T (NOLOCK) ON I.KAMGRUPA = T.GRUPA AND I.TIP = T.TIP AND I.VALUTA = T.SIFVAL   
        LEFT JOIN #UNPVT_TIPSTOPE_TS T1 (NOLOCK) ON I.KAMGRUPA = T1.GRUPA AND I.TIP = T1.TIP AND I.VALUTA = T1.SIFVAL AND T.GRUPA IS NULL 
        LEFT JOIN #TMP_DODATNA_KS DS (NOLOCK) ON I.PARTIJA = DS.PARTIJA AND I.VALUTA = DS.SIFVAL AND I.KAMGRUPA = DS.GRUPA
        LEFT JOIN #NML_RATE N (NOLOCK)  ON I.TIP = N.TIP AND N.SIFVAL = I.VALUTA AND N.GRUPA = I.KAMGRUPA
        -- LEFT JOIN TIPSTOPE TS (NOLOCK) ON I.TIP = TS.TIP AND TS.GRUPA = I.KAMGRUPA AND TS.SIFVAL = I.VALUTA
        LEFT JOIN #NML_RATE_PERIOD NML (NOLOCK) ON I.TIP = NML.TIP AND I.VALUTA = NML.SIFVAL AND NML.GRUPA = I.KAMGRUPA AND NML.SIFVAL = I.VALUTA
        --WHERE NOT EXISTS (SELECT * FROM [dbo].[IC_INPT_AR_X_INT_RATE_SNPST] WHERE PARTIJA = I.PARTIJA AND VALUTA = I.VALUTA AND APP = 'ST')  
    LEFT OUTER JOIN -- Added this join with same logic from function rather than calling a function. 
    (
        SELECT SUM (CONVERT(DECIMAL(18,4),P.iznos*(1-P.dp)/2)) TotalSum
    FROM pts as p
        INNER JOIN tippart t on p.tip = r.tip
        INNER JOIN its i on p.partija = i.partija     
        WHERE p.currency = t.sifval and pts.dknizenja < i.dotvaranje
        GROUP BY p.partija 
    ) SumTable
    WHERE I.DOTVARANJE <= '2017.12.31'
    AND (T.TIP IS NOT NULL 
        OR T1.TIP IS NOT NULL 
        OR DS.PARTIJA IS NOT NULL)
        AND SumTable.TotalSum <> 0  -- This is similar to your old logic where you were comparing with function output. 

查询说明:

  • 我在SumTable中添加了haivng left outer join和您现有的查询逻辑。
  • 由于附加的左外部联接具有p.partija分组,因此不会让您的结果集陷入困境。
  • 旧函数的所有输入都已替换为相关值,因为我们在此处进行内联查询。
  • 最后,表示作为性能消耗的罪魁祸首的部分得到改善并且不会调用函数,而是使用SumTable.TotalSum并将其与0进行比较。