以文本方式查看主题 - Foxtable(狐表) (http://foxtable.com/bbs/index.asp) -- 专家坐堂 (http://foxtable.com/bbs/list.asp?boardid=2) ---- 代码效率优化 (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=133902) |
-- 作者:bobolan521 -- 发布时间:2019/4/23 13:33:00 -- 代码效率优化 以下代码执行完毕,目前需要4分钟左右 请老师帮忙看一下,执行速度还可以再优化一下吗? Dim gs As String ,dw As String , bb As String ,dfje As Decimal,je As Decimal,mxzje As Decimal \'\'\'\'\'\'\'\'\'查询需要计算请款总金额的清单 Dim cmd As New SQLCommand Dim dt As DataTable cmd.C cmd.CommandText = "SELEC T 公司,单位,币别,sum(请款金额) as 请款总金额 from 请款单 group by 公司,单位,币别 order by 公司" dt = cmd.ExecuteReader() \'\'\'\'\'\'\'\'\'查询需要计算请款总金额的清单 If dt.DataRows.Count > 0 Then For Each drb As DataRow In dt.DataRows bb = drb("币别") gs = drb("公司") dw = drb("单位") je = drb("请款总金额") Dim cmd1 As New SQLCommand Dim dt1 As DataTable cmd1.C cmd1.CommandText = "Selec t _identify as 序号, 公司,单位,本期贷方发生额,发生期间 ,应支付期间,付款总金额,请款总金额,币别 FROM 科目余额表 where 公司 = \'" & gs & "\' " & _ " And 单位 =\'" & dw & "\' and 币别=\'" & bb & "\'order by 应支付期间 " dt1 = cmd1.ExecuteReader() If dt1.DataRows.Count > 0 Then mxzje = dt1.Compute("sum(本期贷方发生额)","") dfje=je - mxzje For Each dra As DataRow In dt1.DataRows If dra("公司")=gs And dra("单位")=dw And dra("币别")=bb Then If je>= dra("本期贷方发生额") Then dra("请款总金额") = dra("本期贷方发生额") je = je - dra("本期贷方发生额") Else dra("请款总金额") = je je = 0 End If End If \'\'\'\'\'\'\'\'反写请款总金额 Dim cmd2 As New SQLCommand Dim dt2 As DataTable cmd2.C cmd2.CommandText = "updat e 科目余额表 set 请款总金额 = \'" & dra("请款总金额") & "\' where _identify = \'" & dra("序号") & "\'" dt2 = cmd2.ExecuteReader() \'\'\'\'\'\'\'\'反写请款总金额 Next End If Next End If |
-- 作者:有点甜 -- 发布时间:2019/4/23 15:22:00 --
每执行一次 ExecuteReader 耗时 0.5 秒。如果确实只能这样,加入事务
http://www.foxtable.com/webhelp/scr/2933.htm
|