重写代码以获得更好,更快的性能

时间:2018-02-13 07:13:11

标签: sql-server-2008 vb6

我对以下代码有一个大问题,需要帮助。这是我几周来主要头痛中的一半。 当我为具有20GB数据库的客户端运行以下代码时,需要很长时间才能完成。 我现在的代码编写得很糟糕,我需要帮助才能修复它。

代码如下:

Set rstCount = MyConn.Execute("Select count(*) as CNT from tblLoanTable1")
Set rst = MyConn.Execute("select * from tblLoanTable1 where Acctno <> '' order by AcctNo")
If rst.BOF Then
Else

PBar.Max = Val(rstCount!CNT) + 1
PBar.Value = 0
Do Until rst.EOF

ODCAUGHT = False

''CHECK IF THE LOAN IS PERFORMING

Set rst1 = MyConn.Execute("select * from tblOverdraftTable1 where acctno = '" & rst!acctno & "' and expirydate >= '" & Date & "'  ")
If rst1.BOF Then
Else

If Val(rst1!OBal) >= Val(rst!NonVal) Then
   GoTo BADA
Else
   ODCAUGHT = True
   A4 = Round(Val(rst!NonVal) - Val(rst1!OBal), 2)
End If

End If

CONST4 = "NON PERFORMING OVERDRAFTS"

If Option3.Value = True Then
GD = rst!EntryDate
ElseIf Option4.Value = True Then
GD = rst!TransDate
Else
End If

STDate = GD
A2 = dtDate1.Value - GD

If Val(A2) < 0 Then
   GoTo BADA
End If



If MCLASS = False Then

If Val(A2) >= 0 And Val(A2) <= 30 Then
   CONST = "CATEGORY1"
   CD1 = 0.05
   CD3 = 0.05
ElseIf Val(A2) > 30 And Val(A2) <= 90 Then
   CONST = "CATEGORY2"
   CD1 = 0.2
   CD3 = 0.2
ElseIf Val(A2) > 90 And Val(A2) <= 180 Then
   CONST = "CATEGORY3"
   CD1 = 0.5
   CD3 = 0.5
ElseIf Val(A2) > 180 Then
   CONST = "CATEGORY4"
   CD1 = 1
   CD3 = 1
Else
   GoTo BADA
End If

End If



A3 = Round(Val(rst!NonVal), 2)

If Val(A2) = 1 Then
A6 = Val(A2) & " Day"
Else
A6 = Val(A2) & " Days"
End If

If ODCAUGHT = True Then
A3 = Val(A4)
Else
A3 = Round(Val(rst!NonVal), 2)
End If

AB1 = ""
AB2 = ""
AB3 = ""
AB4 = ""
AB5 = "OVERDRAFT"

Set rst6 = MyConn.Execute("select * from tblCustomersTable1 where acctno = '" & rst!acctno & "'")
If rst6.BOF Then
Else
CustBal1 = Round(Val(rst6!Balance), 2)
CustBlock = rst6!Blocked
AB1 = rst6!AcctD
AB2 = rst6!AcctO
AB3 = rst6!Ccode
AB4 = BranchName(rst6!Ccode)
AB5 = "OVERDRAFT"
end if

CD4 = Round(Val(CD3) * Val(A3), 2)
BAL1 = Val(CustBal1)


If CustBlock = "Y" Then
GoTo OGD
End If

ValB = Round(-1 * Val(BAL1), 2)
Bal = Val(Mafi6)
BAL2 = Round((ValB * Bal) / 36500, 2)

OGD:
Sno = Val(Sno) + 1



'CHECK EXPIRY DATE

Set rst5 = MyConn.Execute("Select * from tblLoanRecord2 where AccountID = '" & rst!acctno & "' and Finish = 'Y'")
If rst5.BOF Then
ExpDate = dtDate1.Value
GoTo MOVEON
Else

Set rst9 = MyConn.Execute("Select Max(Loandate) as Loandate from tblLoanRecord2 where AccountID = '" & rst!acctno & "' and Finish = 'Y'")
If rst9.BOF Then
ExpDate = dtDate1.Value
GoTo MOVEON
Else

Set Rst10 = MyConn.Execute("Select * from tblLoanRecord2 where AccountID = '" & rst!acctno & "' and Loandate = '" & rst9!Loandate & "' and Finish = 'Y'")
If Rst10.BOF Then
Else

RepMETHOD = Rst10!Paymentopt

If RepMETHOD = "Daily" Then
   ExpDate = Rst10!Loandate + Val(Rst10!duration)
ElseIf RepMETHOD = "Weekly" Then
   ExpDate = Rst10!Loandate + (7 * Val(Rst10!duration))
ElseIf RepMETHOD = "Monthly" Then
   ExpDate = Rst10!Loandate + (30 * Val(Rst10!duration))
ElseIf RepMETHOD = "Semi - Monthly" Then
   ExpDate = Rst10!Loandate + (15 * Val(Rst10!duration))
ElseIf RepMETHOD = "Quarterly" Then
   ExpDate = Rst10!Loandate + (90 * Val(Rst10!duration))
ElseIf RepMETHOD = "Bi - Annually" Then
   ExpDate = Rst10!Loandate + (180 * Val(Rst10!duration))
End If
End If
End If
End If

MOVEON:

If STDate > ExpDate Then
   ExpDate = STDate + 30
End If

If CONST = "CATEGORY1" Then
MyConn.Execute ("Insert into tblLoanTable1Temp3 values ('" & rst!acctno & "','" & Replace(rst!ACCTName, "'", "''") & "','" & Replace(rst!Accttype, "'", "''") & "','" & 

Replace(rst!AcctO, "'", "''") & "','" & Val(A3) & "','" & Val(A3) & "','" & Val(BAL2) & "','" & Val(BAL2) & "','" & A6 & "','N/A','N/A','" & rst!TType & "','" & STDate 

& "','" & ExpDate & "','OVERDRAFT','" & CONST4 & "','0','" & Val(A3) & "','" & Val(A3) & "','0','0','0','" & Val(CD4) & "','" & Val(BAL1) & "','" & dtDate1.Value & "','" 

& CONST & "','" & AB1 & "','" & AB2 & "','" & AB3 & "','" & AB4 & "','" & AB5 & "','" & PostUser & "','" & Val(Sno) & "')")
ElseIf CONST = "CATEGORY2" Then
MyConn.Execute ("Insert into tblLoanTable1Temp3 values ('" & rst!acctno & "','" & Replace(rst!ACCTName, "'", "''") & "','" & Replace(rst!Accttype, "'", "''") & "','" & 

Replace(rst!AcctO, "'", "''") & "','" & Val(A3) & "','" & Val(A3) & "','" & Val(BAL2) & "','" & Val(BAL2) & "','" & A6 & "','N/A','N/A','" & rst!TType & "','" & STDate 

& "','" & ExpDate & "','OVERDRAFT','" & CONST4 & "','0','" & Val(A3) & "','0','" & Val(A3) & "','0','0','" & Val(CD4) & "','" & Val(BAL1) & "','" & dtDate1.Value & "','" 

& CONST & "','" & AB1 & "','" & AB2 & "','" & AB3 & "','" & AB4 & "','" & AB5 & "','" & PostUser & "','" & Val(Sno) & "')")
ElseIf CONST = "CATEGORY3" Then
MyConn.Execute ("Insert into tblLoanTable1Temp3 values ('" & rst!acctno & "','" & Replace(rst!ACCTName, "'", "''") & "','" & Replace(rst!Accttype, "'", "''") & "','" & 

Replace(rst!AcctO, "'", "''") & "','" & Val(A3) & "','" & Val(A3) & "','" & Val(BAL2) & "','" & Val(BAL2) & "','" & A6 & "','N/A','N/A','" & rst!TType & "','" & STDate 

& "','" & ExpDate & "','OVERDRAFT','" & CONST4 & "','0','" & Val(A3) & "','0','0','" & Val(A3) & "','0','" & Val(CD4) & "','" & Val(BAL1) & "','" & dtDate1.Value & "','" 

& CONST & "','" & AB1 & "','" & AB2 & "','" & AB3 & "','" & AB4 & "','" & AB5 & "','" & PostUser & "','" & Val(Sno) & "')")
ElseIf CONST = "CATEGORY4" Then
MyConn.Execute ("Insert into tblLoanTable1Temp3 values ('" & rst!acctno & "','" & Replace(rst!ACCTName, "'", "''") & "','" & Replace(rst!Accttype, "'", "''") & "','" & 

Replace(rst!AcctO, "'", "''") & "','" & Val(A3) & "','" & Val(A3) & "','" & Val(BAL2) & "','" & Val(BAL2) & "','" & A6 & "','N/A','N/A','" & rst!TType & "','" & STDate 

& "','" & ExpDate & "','OVERDRAFT','" & CONST4 & "','0','" & Val(A3) & "','0','0','0','" & Val(A3) & "','" & Val(CD4) & "','" & Val(BAL1) & "','" & dtDate1.Value & "','" 

& CONST & "','" & AB1 & "','" & AB2 & "','" & AB3 & "','" & AB4 & "','" & AB5 & "','" & PostUser & "','" & Val(Sno) & "')")
Else
End If

If ODCAUGHT = True Then
   MyConn.Execute ("Update tblLoanTable1Temp set NonVal = '" & Val(A3) & "' where AcctNo = '" & rst!acctno & "' and RefNo2 = '" & rst!RefNo2 & "'")
End If

BADA:
PBar.Value = PBar.Value + 1
rst.MoveNext
Loop
End If

PBar.Value = Val(rstCount!CNT) + 1



'PART TWO 

Set rstCount = MyConn.Execute("Select count(*) as CNT from tblLoanTable10")
Set rst = MyConn.Execute("select * from tblLoanTable10  order by accountid")
If rst.BOF Then
Else

PBar.Max = Val(rstCount!CNT) + 1
PBar.Value = 0
Do Until rst.EOF
Set rst1 = MyConn.Execute("select * from tblLoanRecord2 where accountid = '" & rst!AccountID & "' and RefNo = '" & rst!RefNo & "'")
If rst1.BOF Then
   GoTo BADA2
Else
   RepMETHOD = rst1!Paymentopt
   CONST = Val(rst1!duration) * 30
   DD1 = rst!Loandate + Val(CONST)
   DD2 = rst!deductdate
   CONST2 = Round(Val(rst1!Fee1) + Val(rst1!Fee2) + Val(rst1!Fee3) + Val(rst1!hndfee), 2)
   CONST3 = Round(CBalance(rst!AccountID), 2)
   AB5 = rst1!LoanType2
End If


Set rst6 = MyConn.Execute("Select * from tblLoanInt where Acctno = '" & rst!AccountID & "' and RefNo = '" & rst!RefNo & "'")
If rst6.BOF Then
Else
If RepMETHOD = "Daily" Then
   ExpDate = rst6!trndate + Val(rst6!PERIOD)
   A6 = Val(rst6!PERIOD) & " Days"
ElseIf RepMETHOD = "Weekly" Then
   ExpDate = rst6!trndate + (7 * Val(rst6!PERIOD))
   A6 = Val(rst6!PERIOD) & " Weeks"
ElseIf RepMETHOD = "Monthly" Then
   ExpDate = rst6!trndate + (30 * Val(rst6!PERIOD))
   A6 = Val(rst6!PERIOD) & " Months"
ElseIf RepMETHOD = "Semi - Monthly" Then
   ExpDate = rst6!trndate + (15 * Val(rst6!PERIOD))
   A6 = Val(rst6!PERIOD) & " Semi-Months"
ElseIf RepMETHOD = "Quarterly" Then
   ExpDate = rst6!trndate + (90 * Val(rst6!PERIOD))
   A6 = Val(rst6!PERIOD) & " Quarters"
ElseIf RepMETHOD = "Bi - Annually" Then
   ExpDate = rst6!trndate + (180 * Val(rst6!PERIOD))
   A6 = Val(rst6!PERIOD) & " Bi-Annual"
End If

   A7 = Round(Val(rst6!Uint), 2)
End If

CONST4 = "PERFORMING"


AB1 = ""
AB2 = ""
AB3 = ""
AB4 = ""

Set rst6 = MyConn.Execute("select * from tblCustomersTable1 where acctno = '" & rst!acctno & "'")
If rst6.BOF Then
Else
CustBal1 = Round(Val(rst6!Balance), 2)
CustBlock = rst6!Blocked
AB1 = rst6!AcctD
AB2 = rst6!AcctO
AB3 = rst6!Ccode
AB4 = BranchName(rst6!Ccode)
end if



BAL1 = Val(CustBal1), 2)

CD3 = "0.01"
A3 = Val(rst6!Balance2)
CD4 = Round(Val(CD3) * Val(A3), 2)

Sno = Val(Sno) + 1


MyConn.Execute ("Insert into tblLoanTable1Temp3 values ('" & rst!AccountID & "','" & Replace(rst!CustomerName, "'", "''") & "','" & Replace(rst!Accttype, "'", "''") & 

"','" & Replace(AccountOff(rst!AccountID), "'", "''") & "','" & Val(rst1!LOAN) & "','" & Val(rst!LBalance) & "','" & Val(rst1!INTEREST) & "','" & Val(A7) & "','" & A6 & 

"','" & Replace(rst1!Purpose, "'", "''") & "','" & Replace(rst1!Colateral, "'", "''") & "','" & rst!RefNo & "','" & rst6!trndate & "','" & ExpDate & "','" & rst1!

LoanType2 & "','" & CONST4 & "','" & Val(rst!LBalance) & "','0','0','0','0','0','" & Val(CD4) & "','" & Val(BAL1) & "','" & dtDate1.Value & "','" & CONST4 & "','" & AB1 & 

"','" & AB2 & "','" & AB3 & "','" & AB4 & "','" & AB5 & "','" & PostUser & "','" & Val(Sno) & "')")

BADA2:
PBar.Value = PBar.Value + 1
rst.MoveNext
Loop
End If

PBar.Value = Val(rstCount!CNT) + 1




' PART THREE - OVERDRAFT

Set rstCount = MyConn.Execute("Select count(*) as CNT from tblOverdraftTable1")
Set rst = MyConn.Execute("Select * from tblOverdraftTable1 order by acctno ")
If rst.BOF Then
Else
PBar.Max = Val(rstCount!CNT) + 1
PBar.Value = 0
Do Until rst.EOF

   CONST = Val(rst!Operiod)
   CONST2 = Val(rst!OAmt)

    If Val(A2) = 1 Then
    A6 = Val(rst!Operiod) & " Day"
    Else
    A6 = Val(rst!Operiod) & " Days"
    End If


CONST4 = "PERFORMING ADVANCES"

CD3 = "0.01"
A3 = Val(rst!OAmt)
CD4 = Round(Val(CD3) * Val(A3), 2)


AB1 = ""
AB2 = ""
AB3 = ""
AB4 = ""
AB5 = ""
Set rst6 = MyConn.Execute("select * from tblCustomersTable1 where acctno = '" & rst!acctno & "'")
If rst6.BOF Then
Else
CustBal1 = Round(Val(rst6!Balance), 2)
CustBlock = rst6!Blocked
AB1 = rst6!AcctD
AB2 = rst6!AcctO
AB3 = rst6!Ccode
AB4 = BranchName(rst6!Ccode)
AB5 = "OVERDRAFT"
end if


If Val(CustBal1) >= 0 Then
   GoTo BADA3
End If

If Val(CustBal1) * -1 >= Val(rst!OBal) Then
   A7 = Val(rst!OBal)
Else
   A7 = Val(CustBal1) * -1
End If

Sno = Val(Sno) + 1


MyConn.Execute ("Insert into tblLoanTable1Temp3 values ('" & rst!acctno & "','" & Replace(rst!ACCTName, "'", "''") & "','" & Replace(rst!Accttype, "'", "''") & "','" & 

Replace(AccountOff(rst!acctno), "'", "''") & "','" & Val(rst!OBal) & "','" & Val(A7) & "','0','0','" & A6 & "','" & rst!Purpose & "','" & rst!Colateral & "','" & rst!

RefNo & "','" & rst!Startdate & "','" & rst!Enddate.Value & "','OVERDRAFT','" & CONST4 & "','" & Val(rst!OAmt) & "','0','0','0','0','0','" & Val(CD4) & "','" & Val(BAL1) 

& "','" & dtDate1.Value & "','" & CONST4 & "','" & AB1 & "','" & AB2 & "','" & AB3 & "','" & AB4 & "','" & AB5 & "','" & PostUser & "','" & Val(Sno) & "')")

BADA3:
PBar.Value = PBar.Value + 1
rst.MoveNext
Loop
End If

期待在这里提供一些帮助

1 个答案:

答案 0 :(得分:1)

我能想到两件事......

  1. 通过将至少第一个Connection.Execute更改为Recordset.Open - ReadOnly ForwardOnly
  2. ,您可能会看到性能提升

    而不是:

    Set rst = MyConn.Execute("select * from tblLoanTable1 where Acctno <> '' order by AcctNo")
    

    执行此操作:

    Dim strSQL as String 
    strSQL = "select * from tblLoanTable1 where Acctno <> '' order by AcctNo"
    
    Dim rst as new ADODB.Recordset
    rst.Open strSQL, MyConn, adOpenForwardOnly, adLockReadOnly
    
    1. 您确定在tblLoanTable1,tblOverdraftTable1和tblCustomersTable1
    2. 上有正确的索引吗?

      请务必在这些列上添加索引:

      tblLoanTable1 - Acctno

      tbloverdrafttable1 - Acctno,expirydate

      tblCustomersTable1 - Acctno