我有以下算法
For R = FirstRowDBABACUS To LastRowDBABACUS ' R = Remboursement
LastInterestDate = ""
Do While Sheets("CI").Cells(R, 10).Value <> 0
For Inte = FirstRowDBABACUS To LastRowDBABACUS
'On commence par l'intérêt
If Sheets("CI").Cells(Inte, 3).Value <= DateArrete Then
LastInterestDate = "R" & Inte & "C" & 3
If Sheets("CI").Cells(Inte, 8).Value <> 0 Then
If Sheets("CI").Cells(R, 10).Value > Sheets("CI").Cells(Inte, 8).Value Then
'Modification du montant de Remboursement residuel
Sheets("CI").Cells(R, 10).Value = Sheets("CI").Cells(R, 10).Value - Sheets("CI").Cells(Inte, 8)
Sheets(RapproSheetName).Cells(Inte, 12).Value = Sheets(RapproSheetName).Cells(Inte, 8).Value
Sheets("CI").Cells(Inte, 8).Value = 0
Else
'Le montant ne suffit pas pour les intérêts
Sheets(RapproSheetName).Cells(Inte, 12).Value = Sheets(RapproSheetName).Cells(Inte, 12).Value + Sheets("CI").Cells(R, 10).Value
Sheets("CI").Cells(Inte, 8).Value = Sheets("CI").Cells(Inte, 8).Value - Sheets("CI").Cells(R, 10).Value
Sheets("CI").Cells(R, 10).Value = 0
Exit For
End If
End If
'Principal s'il reste quelque chose
If Sheets("CI").Cells(R, 10).Value <> 0 Then
If Sheets("CI").Cells(Inte, 9) <> 0 Then
If Sheets("CI").Cells(R, 10).Value > Sheets("CI").Cells(Inte, 9).Value Then
'Modification du montant de Remboursement residuel
Sheets("CI").Cells(R, 10).Value = Sheets("CI").Cells(R, 10).Value - Sheets("CI").Cells(Inte, 9)
Sheets(RapproSheetName).Cells(Inte, 11).Value = Sheets(RapproSheetName).Cells(Inte, 9).Value
Sheets("CI").Cells(Inte, 9).Value = 0
Else
'Le montant ne suffit pas pour le remboursmeent du principal
Sheets(RapproSheetName).Cells(Inte, 11).Value = Sheets(RapproSheetName).Cells(Inte, 11).Value + Sheets("CI").Cells(R, 10).Value
Sheets("CI").Cells(Inte, 9).Value = Sheets("CI").Cells(Inte, 9).Value - Sheets("CI").Cells(R, 10).Value
Sheets("CI").Cells(R, 10).Value = 0
Exit For
End If
End If
End If
Else
End If
Next Inte
我正在尝试编写代码sql,这需要太长的时间和错误的结果。
> declare @RefAbacus as nvarchar(100)
> set @RefAbacus ='002-00000001'
> SELECT
> RowNum = ROW_NUMBER() OVER (
> ORDER BY [RefAbacus]),
> * INTO #CI
> FROM [CI]
> WHERE [RefAbacus] = @RefAbacus
> order by date, TypeOperation
> SELECT
> RowNum = ROW_NUMBER() OVER (
> ORDER BY [RefAbacus]),
> * INTO #DBRapproEchRemb
> FROM [DBTmpRapproEchRemb]
> WHERE [RefAbacus] =@RefAbacus
> order by date, TypeOperation
>
> CREATE UNIQUE CLUSTERED INDEX IX_1 on #CI ([RefAbacus], RowNum)
> CREATE UNIQUE CLUSTERED INDEX IX_2 on #DBRapproEchRemb ([RefAbacus], RowNum)
> --declaration des variables
> DECLARE @MaxRownum int
> SET @MaxRownum = (SELECT
> MAX(RowNum)
> FROM #CI) -----------------------
> DECLARE @MinRownum int
> SET @MinRownum = (SELECT
> MIN(RowNum)
> FROM #CI)
>
> DECLARE @FR int;
> DECLARE @LR int;
> DECLARE @inte int;
> DECLARE @R int;
> DECLARE @remboursement AS float
> DECLARE @LastInterestDate AS nvarchar(50);
> DECLARE @i int;
> DECLARE @TolerancePart int;
>
> SET @FR = @MinRownum
> SET @LR = @MaxRownum
> SET @TolerancePart = 5;
> ----------------------------------------------------
>
> SET @R = (SELECT
> MIN(RowNum)
> FROM #CI)
> SET @inte = (SELECT
> MIN(RowNum)
> FROM #CI)
>
>
> WHILE @R <= @MaxRownum
> BEGIN
>
> while (SELECT [Remboursement] FROM #CI WHERE RowNum = @R ) <>0 and (SELECT [Remboursement] FROM #CI WHERE RowNum = @R ) IS NOT
> NULL
> begin
> SET @LastInterestDate = ''
> SET @inte = (SELECT
> MIN(RowNum)
> FROM #CI)-1
> WHILE @inte <= @MaxRownum
> BEGIN -- run your operation here
> --print @inte
> -- Condition 1
> SET @inte = @inte + 1;
> IF (SELECT Date FROM #CI WHERE RowNum = @inte)<= CONVERT(date, GETDATE())
>
> BEGIN
> SET @LastInterestDate = 'R' + CONVERT(nvarchar(50), @inte) + 'C3'
>
> -- Condition 2
> IF ((SELECT EchInteret FROM #CI
> WHERE RowNum = @inte) <> 0)
> BEGIN
>
> -- Condition 3
> IF ((SELECT [Remboursement] FROM #CI
> WHERE RowNum = @R) > (SELECT EchInteret FROM #CI
> WHERE RowNum = @inte))
> BEGIN ---------------------------------------
> -- Update 1
> print 'Update 1'
> UPDATE #CI
> SET [Remboursement] = R.[Remboursement] - isnull(INTE.EchInteret,0)
> FROM (SELECT * FROM #CI
> WHERE RowNum = @inte) INTE INNER JOIN (SELECT * FROM #CI
> WHERE RowNum = @R) R
> ON R.RefAbacus = INTE.RefAbacus WHERE #CI.RowNum = @R;
>
> -- Update 2
> UPDATE #CI
> SET EchInteret = 0
> FROM (SELECT * FROM #CI
> WHERE RowNum = @inte) INTE INNER JOIN (SELECT * FROM #CI
> WHERE RowNum = @R) R ON R.RefAbacus = INTE.RefAbacus
> WHERE #CI.RowNum = @inte;
>
> UPDATE #DBRapproEchRemb
> SET [PartEnInterêt] = isnull(EchInteret,0)
> WHERE RowNum = @inte;
>
>
> --SET @inte = @inte + 1;
> END
> ELSE -------------------------
> -- Update 3
> BEGIN
>
> UPDATE #DBRapproEchRemb
> SET [PartEnInterêt] = isnull(INTE.[PartEnInterêt],0) + R.Remboursement
> FROM (SELECT * FROM #CI
> WHERE RowNum = @R) R
> INNER JOIN (SELECT * FROM #DBRapproEchRemb
> WHERE RowNum = @inte) INTE ON R.RefAbacus = INTE.RefAbacus
> WHERE #DBRapproEchRemb.RowNum = @inte;
>
>
> UPDATE #CI
> SET EchInteret = isnull(INTE.EchInteret,0) - R.Remboursement
> FROM (SELECT * FROM #CI
> WHERE RowNum = @R) R INNER JOIN (SELECT * FROM #CI
> WHERE RowNum = @inte) INTE ON R.RefAbacus = INTE.RefAbacus
> WHERE #CI.RowNum = @inte;
>
> print 'ok'
> UPDATE #CI
> SET Remboursement = 0
> WHERE #CI.RowNum = @R;
>
> --------------------------------------------
> BREAK
> END
> END
>
> -- Condition 4
> IF ((SELECT [Remboursement]FROM #CI
> WHERE RowNum = @R) <> 0)
>
> BEGIN
> -- Condition 5
> IF ((SELECT EchPrincial FROM #CI
> WHERE RowNum = @inte) <> 0)
>
> BEGIN
>
> -- Condition 6
> IF ((SELECT [Remboursement] FROM #CI
> WHERE RowNum = @R) > (SELECT EchPrincial FROM #CI
> WHERE RowNum = @inte))
>
> -- Update 4
>
> BEGIN
> ----------------------------
>
> UPDATE #CI
> SET [Remboursement] = R.[Remboursement] - isnull(INTE.EchPrincial,0)
> FROM (SELECT*FROM #CI
> WHERE RowNum = @inte) INTE INNER JOIN (SELECT * FROM #CI
> WHERE RowNum = @R) R ON R.RefAbacus = INTE.RefAbacus
> WHERE #CI.RowNum = @R;
>
> UPDATE #DBRapproEchRemb
> SET PartEnPrincipal =isnull( EchPrincial,0)
> WHERE RowNum = @inte;
>
>
> UPDATE #CI
> SET EchPrincial = 0
> WHERE #CI.RowNum = @inte;
> print' avant break'
>
> END
> -----------
> ELSE
> -- Update 5
> BEGIN
>
>
> UPDATE INTE
> SET PartEnPrincipal = isnull(inte.PartEnPrincipal,0) + R.Remboursement
> FROM (SELECT * from #DBRapproEchRemb
> WHERE RowNum = @inte)INTE INNER JOIN (SELECT * FROM #CI
> WHERE RowNum = @R) R ON R.RefAbacus = INTE.RefAbacus
> WHERE INTE.RowNum = @inte;
>
>
> UPDATE INTE
> SET EchPrincial = inte.EchPrincial + R.Remboursement
> FROM #CI
> INTE INNER JOIN (SELECT * FROM #CI
> WHERE RowNum = @R) R ON R.RefAbacus = INTE.RefAbacus
> WHERE INTE.RowNum = @inte;
>
>
> UPDATE #CI
> SET Remboursement = 0
> WHERE #CI.RowNum = @R;
> print 'apres break'
>
> BREAK
> END
> END
>
> END
>
> END
>
> end
>
> END
> SET @R = @R + 1;
> END
。 。 。
答案 0 :(得分:0)
您要做的第一件事就是让您的SQL获得正确的结果。优化不能产生正确结果的东西是没有意义的。这里不幸的消息是,在您完成此操作之前,我们无法真正帮助您进行特定的优化,因为我们需要知道正确的查询是什么样的。
完成后,下一步是查看执行计划并查看您的查询正在执行的操作。您需要弄清楚查询的哪一部分是最昂贵的部分,并首先开始优化该部分。好的工具不仅会向您显示计划的每个部分,还会显示这些部分相对于计划的其余部分需要多长时间。如果查询或批处理查询内部有多个不同的语句,它还应该显示哪些语句最昂贵。如果您使用的是SQL Server,则SQL Server Management Studio工具附带一个可接受的查询计划查看器,它提供此类信息,如果您缺少索引,甚至可以帮助建议索引。例如,如果您正在进行表扫描(获取全部或大部分)表,则数百万行只是为了找到一些值,这将导致不太理想的性能。或者,如果您对非常大的数据集进行排序,则也会导致性能下降。您的查询也是显式循环的。如果它在一大堆行上循环,那将是昂贵的。如果您可以尝试摆脱显式循环以支持基于集合的操作,那么查询优化器可能能够简化您的一些步骤并极大地加快速度。但这都是猜测,因为我现在没有足够的信息来帮助你。
除此之外,除非我们知道工作查询(产生正确结果的查询),您正在使用的特定SQL数据库,您的模式的样子以及各种表的大小,否则它赢了我们很容易帮助您提出具体建议。