记录集中循环的最快方法

发布于 2024-11-15 07:55:38 字数 97 浏览 7 评论 0原文

我有一个 VB6 应用程序。我有 2 个记录集具有相同数量的记录(几乎 50k)。现在我必须循环 50k*50k *(字段数)。谁能告诉我最快的方法吗?

提前致谢。

I have a VB6 application. I have 2 recordsets having the same number of records (almost 50k). Now I have to loop 50k*50k *(number of fields). Can any one tell me the fastest way to do so?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

权谋诡计 2024-11-22 07:55:38

正如其他人指出的那样,在客户端执行此操作并不是一个好主意,但这里有一些加快记录集访问速度的指示。

  • 以仅向前 (adOpenForwardOnly) 方式打开记录集,如果您不写入,则也只能读取。

  • 按数字引用字段,因此您可以使用 rs.Fields(0),而不是执行 rs.Fields(“MyField”)

  • 如果你要写回数据库,那么考虑将事情包装在事务中以加快速度。

  • 循环时使用“Do Until rs.EOF”而不是计数记录。

这就是我现在能想到的,但他们应该有一点帮助

As others have pointed out it is not a good idea to do this client side but here are a few pointers for speeding up recordset access.

  • Open the recordset as forward only (adOpenForwardOnly) and if you are not writing then read only also.

  • Refer to the fields by number so instead of doing rs.Fields(“MyField”) you would use rs.Fields(0)

  • If you are writing back to the database then consider wrapping things up in a transaction to speed things up.

  • When looping use “Do until rs.EOF” instead of counting records.

Thats all I can think of for now but they should help a bit

夜巴黎 2024-11-22 07:55:38

在 vb6 中确实没有最快的方法来做到这一点。
您将使用 2 个 for 循环。

您可能想向我们提供更多详细信息,说明为什么您要下拉 50k 条记录(两次)并比较每个字段。这通常表明需要在数据库端完成某些操作,但已被带到客户端来解决。

There really is no fastest way to do this in vb6.
You'd use 2 for loops.

You might want to give us more details as to why you are pulling down 50k records (twice) and comparing each field. This is usually an indication that something needs to be done on the database side but was brought in to the client side to be solved.

清泪尽 2024-11-22 07:55:38

如果您的两个数据库相同并且唯一的区别是数据,您可以执行以下操作(伪代码):

SELECT t1.A, t2.A, t2.B, t2.B, ...
FROM t1
INNER JOIN t2 on t1.id = t2.id
WHERE (t1.A <> t2.A) OR (t1.B <> t2.B) OR ...

t1 和 t2 将是您的两个表。这不是最有效的,但它可以让您非常轻松地进行比较。此外,您在 SELECT 语句中显示的内容可能会变得更加复杂。目前它只是两列并排的列表。

If your two databases are the same and the only difference is the data, you could do something like this (pseudo-code):

SELECT t1.A, t2.A, t2.B, t2.B, ...
FROM t1
INNER JOIN t2 on t1.id = t2.id
WHERE (t1.A <> t2.A) OR (t1.B <> t2.B) OR ...

t1 and t2 would be your two tables. This isn't the most efficient but it would allow you to do the comparisons very easily. Also, you could get more complicated with what you show in the SELECT statement. Currently it is just a listing of both columns side by side.

笛声青案梦长安 2024-11-22 07:55:38

澄清您有两个需要比较的 Access 数据库后,最简单的方法是将两个数据库链接在一起,然后通过查询进行比较。

右键单击白色空白区域,链接表
找到第二个数据库,选择其中的表。

现在您可以编写一个查询来比较两者。通过链接拉取时,所比较的数据始终是最新的。

Having clarified that you've got two Access databases you need to compare, the easiest is to link the two together and then compare with a query.

Right-click the white empty space, Link tables.
Locate you second DB, select the table in it.

Now you can write a query to compare the two. The data being compared is always up to date as it's being pulled through the link.

背叛残局 2024-11-22 07:55:38

我已经编写了很多次代码来比较和同步两个数据表,并且我已经在 SO 上发布了相关内容。关键是使用 SQL 将结果限制为不匹配的记录,但主要的见解是一次编写一列的动态 SQL。在每个 SQL 语句上使用 WHERE 子句,这将比任何其他比较方法更有效。

在 Access 中,我编写了这段代码来从一个表更新另一个表。它假设两个表具有相同的字段,并且有一个 PK 字段实际上标识两个表中的相同记录。该代码依赖于我的 SQLRun() 函数来实际执行 SQL,但如果您不希望的话,可以用 CurrentDB.Execute 替换该行。

  Public Function UpdateTableData(ByVal strSourceTable As String, _
      ByVal strTargetTable As String, ByVal strJoinField As String, _
      ByRef db As DAO.Database, Optional ByVal strExcludeFieldsList As String, _
      Optional ByVal strUpdatedBy As String = "Auto Update", _
      Optional strAdditionalCriteria As String) As Boolean
    Dim strUpdate As String
    Dim rsFields As DAO.Recordset
    Dim fld As DAO.Field
    Dim strFieldName As String
    Dim strNZValue As String
    Dim strSet As String
    Dim strWhere As String

    strUpdate = "UPDATE " & strTargetTable & " INNER JOIN " & strSourceTable _
        & " ON " & strTargetTable & "." & strJoinField & " = " _
        & strSourceTable & "." & strJoinField
    ' if the fields don't have the same names in both tables,
    '   create a query that aliases the fields to have the names of the
    '   target table
    ' if the source table is in a different database and you don't
    '   want to create a linked table, create a query and specify
    '   the external database as the source of the table
    ' alternatively, for strTargetTable, supply a SQL string with
    '   the external connect string
    Set rsFields = db.OpenRecordset(strSourceTable)
    For Each fld In rsFields.Fields
      strFieldName = fld.Name
      If strFieldName <> strJoinField Or (InStr(", " & strExcludeFieldsList _
           & ",", strFieldName & ",") <> 0) Then
         Select Case fld.Type
           Case dbText, dbMemo
             strNZValue = "''"
           Case Else
             strNZValue = "0"
         End Select
         strSet = " SET " & strTargetTable & "." & strFieldName & " = _
             varZLSToNull(" & strSourceTable & "." & strFieldName & ")"
         strSet = strSet & ", " & strTargetTable & ".Updated = #" & Date & "#"
         strSet = strSet & ", " & strTargetTable & ".UpdatedBy = " _
             & STR_QUOTE & strUpdatedBy & STR_QUOTE
         strWhere = " WHERE Nz(" & strTargetTable & "." & strFieldName & ", " _
             & strNZValue & ") <> Nz(" & strSourceTable & "." & strFieldName _
             & ", " & strNZValue & ")"
         If db.TableDefs(strTargetTable).Fields(fld.Name).Required Then
            strWhere = strWhere & " AND " & strSourceTable & "." _
                & strFieldName & " Is Not Null"
         End If
         If Len(strAdditionalCriteria) > 0 Then
            strWhere = strWhere & " AND " & strAdditionalCriteria
         End If
         Debug.Print strUpdate & strSet & strWhere
         Debug.Print SQLRun(strUpdate & strSet & strWhere, dbLocal) & " " _
           & strFieldName & " updated."
      End If
    Next fld
    Debug.Print dbLocal.OpenRecordset("SELECT COUNT(*) FROM " _
       & strTargetTable & " WHERE Updated=#" & Date & "# AND UpdatedBy=" _
       & STR_QUOTE & strUpdatedBy & STR_QUOTE)(0) _
       & " total records updated in " & strTargetTable
    rsFields.Close
    Set rsFields = Nothing
    UpdateTableData = True
  End Function

I have written code to compare and synchronize two data tables many, many times, and I've posted about it here on SO. The key is to use SQL to limit the results to the records that don't match, but the chief insight is to write your on-the-fly SQL ONE COLUMN AT A TIME. With a WHERE clause on each of your SQL statements, this will be much more efficient than any other comparison method.

Within Access, I've written this code to update one table from another. It assumes the tables have the same fields and that there's a PK field that actually identifies the same record in both tables. The code has a dependency on my SQLRun() function to actually execute the SQL, but that line could be replaced with CurrentDB.Execute if you don't want that.

  Public Function UpdateTableData(ByVal strSourceTable As String, _
      ByVal strTargetTable As String, ByVal strJoinField As String, _
      ByRef db As DAO.Database, Optional ByVal strExcludeFieldsList As String, _
      Optional ByVal strUpdatedBy As String = "Auto Update", _
      Optional strAdditionalCriteria As String) As Boolean
    Dim strUpdate As String
    Dim rsFields As DAO.Recordset
    Dim fld As DAO.Field
    Dim strFieldName As String
    Dim strNZValue As String
    Dim strSet As String
    Dim strWhere As String

    strUpdate = "UPDATE " & strTargetTable & " INNER JOIN " & strSourceTable _
        & " ON " & strTargetTable & "." & strJoinField & " = " _
        & strSourceTable & "." & strJoinField
    ' if the fields don't have the same names in both tables,
    '   create a query that aliases the fields to have the names of the
    '   target table
    ' if the source table is in a different database and you don't
    '   want to create a linked table, create a query and specify
    '   the external database as the source of the table
    ' alternatively, for strTargetTable, supply a SQL string with
    '   the external connect string
    Set rsFields = db.OpenRecordset(strSourceTable)
    For Each fld In rsFields.Fields
      strFieldName = fld.Name
      If strFieldName <> strJoinField Or (InStr(", " & strExcludeFieldsList _
           & ",", strFieldName & ",") <> 0) Then
         Select Case fld.Type
           Case dbText, dbMemo
             strNZValue = "''"
           Case Else
             strNZValue = "0"
         End Select
         strSet = " SET " & strTargetTable & "." & strFieldName & " = _
             varZLSToNull(" & strSourceTable & "." & strFieldName & ")"
         strSet = strSet & ", " & strTargetTable & ".Updated = #" & Date & "#"
         strSet = strSet & ", " & strTargetTable & ".UpdatedBy = " _
             & STR_QUOTE & strUpdatedBy & STR_QUOTE
         strWhere = " WHERE Nz(" & strTargetTable & "." & strFieldName & ", " _
             & strNZValue & ") <> Nz(" & strSourceTable & "." & strFieldName _
             & ", " & strNZValue & ")"
         If db.TableDefs(strTargetTable).Fields(fld.Name).Required Then
            strWhere = strWhere & " AND " & strSourceTable & "." _
                & strFieldName & " Is Not Null"
         End If
         If Len(strAdditionalCriteria) > 0 Then
            strWhere = strWhere & " AND " & strAdditionalCriteria
         End If
         Debug.Print strUpdate & strSet & strWhere
         Debug.Print SQLRun(strUpdate & strSet & strWhere, dbLocal) & " " _
           & strFieldName & " updated."
      End If
    Next fld
    Debug.Print dbLocal.OpenRecordset("SELECT COUNT(*) FROM " _
       & strTargetTable & " WHERE Updated=#" & Date & "# AND UpdatedBy=" _
       & STR_QUOTE & strUpdatedBy & STR_QUOTE)(0) _
       & " total records updated in " & strTargetTable
    rsFields.Close
    Set rsFields = Nothing
    UpdateTableData = True
  End Function
归途 2024-11-22 07:55:38

尝试使用sql的算法,左连接和右连接,然后在vb中应用它..,

我也和你有同样的问题,但我尝试了这个解决方案,它有效..,首先,它需要几乎3完成查询需要几个小时,但是当我应用 sql 算法时,只需要几分钟

try to use the algorithm of sql., the left and right join., then apply it in vb..,

i also have the same problem with you but i try that solution and it works.., on first, it takes almost 3 hours the complete the query but when i apply the sql algo, it just takes few minis

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文