简体   繁体   English

SP执行时间极慢

[英]SP execution time is extremely slow

I created a stored procedure that calculates a financial spreading based on a linear self adjusting rule and it takes more than 2 minutes to finish the calculations.我创建了一个基于线性自调整规则计算金融点差的存储过程,完成计算需要 2 多分钟。

The final value goes through multiple iterations in order to adjust and enhance it till finding the optimal optimized final value.最终值经过多次迭代以调整和增强它,直到找到最佳优化的最终值。 The parameters are the following:参数如下:

@input1 = 100000
@input2 = 40
@input3 = 106833


BEGIN
DECLARE @X decimal(22,6) = 0
DECLARE @Y decimal(22,6) = 0.001 
DECLARE @Z decimal(22,6)
DECLARE @r decimal(22,6)
DECLARE @v decimal(22,6) 

SET @v = POWER(1/(1+ (@Y/12)), @input2)
    SET @r = ((@Y/@input2) * input1) / (1-@v) 
    IF (@r < @input3)
        SET @Z = @Y + ABS((@X - @Y)/2)
    ELSE
        SET @Z = @Y - ABS((@X - @Y) /2)

    SET @X = @Y
    SET @Y = @Z 


WHILE (ABS(@r - @input3) > 0.001)
BEGIN
SET @v = POWER(1/(1+ (@Y/12)), @input2)
    SET @r = ((@Y/@input2) * @input1) / (1-@v) 
    IF (@r < @input3)
         SET @Z = @Y + ABS((@X - @Y)/2)
    ELSE
         SET @Z = @Y - ABS((@X - @Y) /2)
    SET @X = @Y
    IF @Y = @Z
    BREAK
    SET @Y = @Z
END

RETURN (CAST(@Y AS decimal(22,6)) * 100)



END

run time = 2 mins and 20 seconds运行时间 = 2 分 20 秒

An alternative to your stored procedure written in TSQL might be a SQL CLR function written in C#.以 TSQL 编写的存储过程的替代方案可能是 SQL CLR function 以 ZD7EFA19FBE7D39372FD452 编写的。 You have to use Visual Studio and create a Database Project.您必须使用 Visual Studio 并创建一个数据库项目。

    public static decimal ConvertTo6(double d)
    {
        return Math.Round(Convert.ToDecimal(d), 6, MidpointRounding.AwayFromZero);
    }

    public static decimal ConvertTo6(decimal d)
    {
        return Math.Round(d, 6, MidpointRounding.AwayFromZero);
    }


    [Microsoft.SqlServer.Server.SqlFunction]
    [return: SqlFacet(Precision = 22, Scale = 6)]
    public static SqlDecimal CalcFinancialSpreading(int input1 = 100000, int input2 = 40, int input3 = 106833)
    {
        decimal x = 0.000000m;
        decimal y = 0.001000m;
        decimal z;
        decimal r;
        decimal v;

        v = ConvertTo6(Math.Pow(1 / (1 + (Convert.ToDouble(y) / 12d)), input2));

        r = ConvertTo6(((y / input2) * input1) / (1 - v));

        if (r < input3)
        {
            z = y + Math.Abs((x - y) / 2);
            z = ConvertTo6(z);
        }
        else
        {
            z = y - Math.Abs((x - y) / 2);
            z = ConvertTo6(z);
        }

        x = y;
        y = z;

        while (Math.Abs(r - input3) > 0.001m)
        {
            v = ConvertTo6((Math.Pow(Convert.ToDouble(1 / (1 + (y / 12))), Convert.ToDouble(input2))));

            r = ((y / input2) * input1) / (1 - v);
            r = ConvertTo6(r);

            if (r < input3)
            {
                z = y + Math.Abs((x - y) / 2);
                z = ConvertTo6(z);
            }
            else
            {
                z = y - Math.Abs((x - y) / 2);
                z = ConvertTo6(z);
            }
            x = y;
            if (y == z) break;
            y = z;
        }

        decimal result = y * 100;

        return new SqlDecimal(result);
    }

Executed as C# code the result is received in 45 seconds on my machine vs. TSQL in 1 Min 56 seconds.以 C# 代码执行,结果在我的机器上 45 秒内收到,而 TSQL 在 1 分 56 秒内收到。

Kudos to @wikiCan by answering this one ...通过回答这个来感谢@wikiCan...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM