繁体   English   中英

vb.net缓存数据库表

[英]vb.net caching database tables

我有一个运行良好的程序。 但是我想加快程序的运行速度。

旧代码是这样的:

Public Funtion CalcSomething(Byval IncID as int16) as double
    Dim rs as adodb.recordset()=cn.execute("SELECT A,B,C,D,.....(around 10 values) FROM table1 t1 LEFT JOIN table2 t2 ON t1.A=t2.A LEFT JOIN ………………(around 10 tables join together WHERE t1.ID=" & IncID)
    Dim Cost1 as double = rs.fields.item("B").value
    Dim AryCost2(19) as double
    For i as int16=0 to 19
        AryCost2(I)=GetCost2(rsX,i,rs.fields.item("A").value,rs.fields.item("C").value)            
    Next I
    Dim Cost2 as double = GetMinValue(AryCost2)

    Return Cost1 + Cost2 
End function

Public Function GetCost2(ByVal I as int16,ByVal A as int16,ByVal B as int16) as double
    Value=DoSomeCalculate(I,A,B)
    Dim rs as adodb.recordset()=cn.execute("SELECT X FROM tableX WHERE Value=" & Value)
    Do while Not rs.EOF
        if FindSuitableOne(rs.fields.item("X").value) then
            Return rs.fields.item("X").value
        End if
    rs.movenext()
    Loop
    return -1
end function

我认为从tableX获得价值20次将是浪费时间,因此我尝试缓存整个表并使用Filter查找相关行:

Public Funtion CalcSomething(Byval IncID as int16) as double
    Dim rs as adodb.recordset()=cn.execute("SELECT A,B,C,D,.....(around 10 values) FROM table1 t1 LEFT JOIN table2 t2 ON t1.A=t2.A LEFT JOIN ………………(around 10 tables join together WHERE t1.ID=" & IncID)
    \\Cache whole table into rsX
    Dim rsX as adodb.recordset()=cn.execute("SELECT X FROM tableX")
    Dim Cost1 as double = rs.fields.item("B").value
    Dim AryCost2(19) as double
    For i as int16=0 to 19
        AryCost2(I)=GetCost2(rsX,i,rs.fields.item("A").value,rs.fields.item("C").value)            
    Next I
    Dim Cost2 as double = GetMinValue(AryCost2)
    Return Cost1 + Cost2 
End function

Public Function GetCost2(byval rsX as adodb.recordset,ByVal I as int16,ByVal A as int16,ByVal B as int16) as double
    Value=DoSomeCalculate(I,A,B)
    rsX.Filter="Value=" & Value) <----this step takes even longer than a new query, I want to ask if there 
    Do while Not rsX.EOF
        if FindSuitableOne(rsX.fields.item("X").value) then
            Return rsX.fields.item("X").value
        End if
    rsX.movenext()
    Loop
    return -1
end function

但是我发现Filter方法比新查询花费的时间更长。 有没有更好的方法可以让我使用指定值键遍历表?

非常感谢你。

您可以执行一个联合查询,如下所示:

Public Sub DoSomething(Byval ClassGroup as int16)
    Dim rsStudent as adodb.recordset()

    rsStudent=cn.execute("SELECT tblStudent.StudentID FROM tblStudent INNER JOIN tblClass ON tblStudent.ClassID = tblClass.ClassID WHERE tblClass.ClassGroup = " & ClassGroup)
    Do While Not rsStudent.EOF
        DoSomethingOnStudent(rsStudent.fields.item("StudentID").value)
        rsStudent.movenext
    Loop
End Sub

这样,只查询一次数据库,您的执行速度就会更快。 如果可能,还避免在select子句中使用* ,而仅检索所需的列(我看到您仅使用表中的StudentID列)。

暂无
暂无

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

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