SC读取数据库的效率和速度问题
(创建时间:2010年03月17日 23:33:00)
Jangogo :
SC读取数据库用的是ADO对象,效率和ADO有直接的关系。
但是我们应该注意的事情是在SC里面一定要尽量少用Do/Loop 或者 for next 来循环数据集在拼凑字符串返回结果。
这样的效率是非常低的,最好的方法是使用ADO.Recorset对象的GetString方法,一次性生成字符串返回,在前台的JS里面再使用Split分割成数组。
结合JS的对象和动态载入表格的方法可以极大地加快程序的处理速度。
如 固定资产台帐 功能,原来有位同事做的代码如下:
代码上我们可以看到readAccountInfo函数里面用了Do/Loop循环,并且多次打开数据库,多次查询,里面还调用一个子函数GetAddUpDep再次多次打开数据库。
这个过程当数据库仅仅是30条数据的时候已经需要差不多10分钟的处理时间。效率非常的低,而且会导致代码处理超时,页面显示出错。
更改如下:
但是我们应该注意的事情是在SC里面一定要尽量少用Do/Loop 或者 for next 来循环数据集在拼凑字符串返回结果。
这样的效率是非常低的,最好的方法是使用ADO.Recorset对象的GetString方法,一次性生成字符串返回,在前台的JS里面再使用Split分割成数组。
结合JS的对象和动态载入表格的方法可以极大地加快程序的处理速度。
如 固定资产台帐 功能,原来有位同事做的代码如下:
- function readAccountInfo(startTime,endTime,dbtype)
- on error resume next
- dim cn,rsG,rs,rs1,rs2,ss
- dim ccode,cname,cplace,cuser,depy,depm,balance,cunit,cdate
- dim staffid,pid,acid,debitqty,debit,price,deped
- '变动
- dim qty,dep,yzh,nvalue
- '折旧
- dim dep1,cdep1
- set cn=GetCN_()
- set rsG = GetRS_()
- cn.open CNStr_
- ss=""
- set rs2=GetRS_()
- set rs=GetRS_()
- set rs1=GetRS_()
- 'sql = "select ccode from fasub where fatype=2"
- sql = "select ccode from fa where fa.ccode <> ' ' order by ccode"
- rsG.open sql,cn,1,3
- do while not rsG.eof
- if err then exit do
- '判断固定资产是否有子信息
- sql = "select * from fa where ccode='" & rsG("ccode") & "'"
- rs.open sql,cn,1,3
- if not rs.eof then
- cname = rs("cname")
- cplace = rs("cplace")
- cuser = rs("cuser")
- depy = rs("depy")
- depm = rs("depm")
- balance = rs("balance")
- cunit = rs("cunit")
- cdate = rs("cdate")
- acid = rs("acid")
- staffid = ""
- pid = ""
- debitqty = 1
- debit = 0
- price = 0
- deped = 0
- end if
- rs.close
- '查询统计变动数据
- sql = "select fasub.ccode,fasub.DeptStaffID,fasub.PrjID,fasub.acid_fee,SUM(fasub.debitQty-fasub.creditQty) as qty,AVG(fasub.price) as aprice,SUM(fasub.debit-fasub.credit) as sdebit from fasub,accperiod where (fatype=1 or fatype=0) and (fasub.fadate between accperiod.startdate and accperiod.enddate) and fasub.ccode='" & rsG("ccode") & "' and accperiod.period >= '" & startTime & "' and accperiod.period <= '" & endTime & "' group by fasub.ccode,fasub.DeptStaffID,fasub.PrjID,fasub.acid_fee"
- rs.open sql,cn,1,3
- '查询统计折旧数据
- sql="select fasub.ccode,fasub.DeptStaffID,fasub.PrjID,SUM(fasub.creditDep) as sDep from fa,fasub,accperiod where fa.ccode=fasub.ccode and fasub.fatype=3 and (fasub.fadate between accperiod.startdate and accperiod.enddate) and fasub.ccode='" & rsG("ccode") & "' and accperiod.period >= '" & startTime & "' and accperiod.period <= '" & endTime & "' group by fasub.ccode,fasub.DeptStaffID,fasub.PrjID"
- 'msgbox sql
- rs1.open sql,cn,1,3
- 'sql="select fa.ccode,fa.cname,fa.cplace,fa.cuser,fa.cdate,fa.depm,fa.depy,fa.balance,fa.cunit,fasub.debitQty,fasub.price,fasub.debit,fasub.deped,fasub.acid_fee,fasub.DeptStaffID,fasub.PrjID from fa,fasub where fa.ccode=fasub.ccode and fasub.ccode='" & rsG("ccode") & "' and fasub.fatype=2"
- sql = "select fa.ccode,fa.cname,fa.cplace,fa.cuser,fa.cdate,fa.depm,fa.depy,fa.balance,fa.cunit,fasub.debitQty,fasub.price,fasub.debit,fasub.deped,fasub.acid_fee,deptstaff.DeptStaffName,prj.PrjName from fa,fasub,deptstaff,prj where fa.ccode=fasub.ccode and fasub.DeptStaffID=deptstaff.DeptStaffID and fasub.PrjID=prj.PrjID and fasub.ccode='" & rsG("ccode") & "' and fasub.fatype=2"
- rs2.open sql,cn,1,3
- if not rs2.eof then
- cname = rs2("cname")
- cplace = rs2("cplace")
- cuser = rs2("cuser")
- depy = rs2("depy")
- depm = rs2("depm")
- balance = rs2("balance")
- cunit = rs2("cunit")
- cdate = rs2("cdate")
- staffid = rs2("DeptStaffName")
- pid = rs2("PrjName")
- acid = rs2("acid_fee")
- debitqty = rs2("debitQty")
- debit = rs2("debit")
- price = rs2("price")
- deped = rs2("deped")
- end if
- if rs.eof then
- qty = 0
- yzh = 0
- nvalue = 0
- else
- qty = CInt(rs("qty"))
- yzh = rs("sdebit")
- nvalue = rs("sdebit")
- end if
- if rs1.eof then
- dep = 0 + deped
- cdep = 0
- else
- dep = rs1("sDep") + deped
- cdep = rs1("sDep")
- end if
- ss=ss & "<P>"
- ss=ss & "<CODE>" & CD(rsG("ccode")) & "</CODE>"
- ss=ss & "<NAME>" & CD(cname) & "</NAME>"
- ss=ss & "<DEPID>" & CD(staffid) & "</DEPID>"
- ss=ss & "<PRJID>" & CD(pid) & "</PRJID>"
- ss=ss & "<PLACE>" & CD(cplace) & "</PLACE>"
- ss=ss & "<USER>" & CD(cuser) & "</USER>"
- ss=ss & "<DATE>" & CD(DFF_(cdate)) & "</DATE>"
- ss=ss & "<DEPM>" & CD(depm) & "</DEPM>"
- ss=ss & "<FEE>" & CD(acid) & "</FEE>"
- ss=ss & "<DEPY>" & CD(depy) & "</DEPY>"
- ss=ss & "<BALANCE>" & CD(balance) & "</BALANCE>"
- ss=ss & "<UNIT>" & CD(cunit) & "</UNIT>"
- ss=ss & "<QTY>" & CD(CInt(debitqty) + qty) & "</QTY>"
- ss=ss & "<PRICE>" & CD(FormatNumber(price,2)) & "</PRICE>"
- 'ss=ss & "<YZH>" & CD(debit + yzh) & "</YZH>"
- ss=ss & "<YZH>" & CD(FormatNumber((debit + yzh),2)) & "</YZH>"
- 'ss=ss & "<DEP>" & CD(FormatNumber((deped + dep),3)) & "</DEP>"
- ss=ss & "<DEP>" & CD(FormatNumber(GetAddUpDep(dbtype,rsG("ccode"),startTime,endtime) + deped,3)) & "</DEP>"
- ss=ss & "<CDEP>" & CD(FormatNumber(cdep,3)) & "</CDEP>"
- '原来是净值减去本期折旧
- 'ss=ss & "<NVALUE>" & CD(Abs(FormatNumber((debit + nvalue - dep),3))) & "</NVALUE>"
- '下面是净值减去累计折旧
- ss=ss & "<NVALUE>" & CD(Abs(FormatNumber((debit + nvalue - (GetAddUpDep(dbtype,rsG("ccode"),startTime,endtime) + deped)),3))) & "</NVALUE>"
- ss=ss & "</P>"
- rs.close
- rs1.close
- rs2.close
- 'msgbox ss
- rsG.MoveNext
- loop
- ' msgbox err.description
- readAccountInfo=XML_("<PS>" & ss & "</PS>" & errXML_(err))
- close_ rs
- close_ rs1
- close_ rs2
- close_ cn
- end function
- '计算并获取累计折旧
- function GetAddUpDep(dbtype,ccode,startperiod,endperiod)
- on error resume next
- dim cn,rs,sql,rtn
- dim fadate,isGoon,speriod,eperiod
- dim sp,ep 'period
- set cn = GetCN_()
- cn.open CNStr_
- set rs = GetRS_()
- isGoon = true
- rtn = 0
- dim c
- c = "#"
- if dbtype = "sqlserver" then
- c = "'"
- end if
- sql = "select count(*) from fasub where fatype=3"
- rs.open sql,cn,1,3
- if rs.eof then
- isGoon = false
- end if
- rs.close
- if isGoon then
- sql = "select top 1 * from fasub where fasub.ccode <> ' ' order by fadate asc"
- rs.open sql,cn,1,3
- if not rs.eof then
- fadate = rs("fadate")
- end if
- rs.close
- sql = "select period from accperiod where (" & c & fadate & c & " between startdate and enddate)"
- rs.open sql,cn,1,3
- if not rs.eof then
- speriod = rs("period")
- end if
- rs.close
- sql = "select top 1 * from fasub where fasub.ccode <> ' ' order by fadate desc"
- rs.open sql,cn,1,3
- if not rs.eof then
- fadate = rs("fadate")
- end if
- rs.close
- sql = "select period from accperiod where (" & c & fadate & c & " between startdate and enddate)"
- rs.open sql,cn,1,3
- if not rs.eof then
- eperiod = rs("period")
- end if
- rs.close
- sp = speriod
- ep = endperiod
- 'msgbox eperiod
- '查询统计累计折旧数据
- sql="select fasub.ccode,fasub.DeptStaffID,fasub.PrjID,SUM(fasub.creditDep) as sDep from fa,fasub,accperiod where fa.ccode=fasub.ccode and fasub.fatype=3 and (fasub.fadate between accperiod.startdate and accperiod.enddate) and fasub.ccode='" & ccode & "' and accperiod.period >= '" & sp & "' and accperiod.period <= '" & ep & "' group by fasub.ccode,fasub.DeptStaffID,fasub.PrjID"
- rs.open sql,cn,1,3
- if not rs.eof then
- rtn = rs("sDep")
- end if
- end if
- GetAddUpDep = rtn
- set rs = nothing
- set cn = nothing
- end function
- function CD(s1)
- CD="<![CDATA[" & s1 & "]]>"
- end function
代码上我们可以看到readAccountInfo函数里面用了Do/Loop循环,并且多次打开数据库,多次查询,里面还调用一个子函数GetAddUpDep再次多次打开数据库。
这个过程当数据库仅仅是30条数据的时候已经需要差不多10分钟的处理时间。效率非常的低,而且会导致代码处理超时,页面显示出错。
更改如下:
文档中心