【问题标题】:Loop Running VERY Slow循环运行非常慢
【发布时间】:2011-08-09 12:45:10
【问题描述】:

请帮我让我的应用程序更快一点,它需要很长时间才能循环并立即给我结果。

这是我做的: 1.从上传的excel文件中加载gridview(这可能是大约300条左右的记录) 2. 将制造商、型号和序列号与我的 MS SQL 数据库(大约 20K 条记录)进行比较,看看是否匹配。

'find source ID based on make/model/serial No combination.
        Dim cSource As New clsSource()
        Dim ds As DataSet = cSource.GetSources()
        Dim found As Boolean = False

        'populate db datatables
        Dim dt As DataTable = ds.Tables(0)
        Dim rows As Integer = gwResults.Rows.Count()
        For Each row As GridViewRow In gwResults.Rows
            'move through rows and check data in each row against the dataset
            '1 - make
            For Each dataRow As DataRow In dt.Rows
                found = False
                If dataRow("manufacturerName") = row.Cells(1).Text Then
                    If dataRow("modelName") = row.Cells(2).Text Then
                        If dataRow("serialNo") = row.Cells(3).Text Then
                            found = True
                        End If
                    End If
                End If

                'display results
                If found Then
                    lblResults.Text += row.Cells(1).Text & "/" & row.Cells(2).Text & "/" & row.Cells(3).Text & " found"
                Else
                    lblResults.Text += row.Cells(1).Text & "/" & row.Cells(2).Text & "/" & row.Cells(3).Text & " not found "
                End If

            Next
        Next

有没有更好的方法来找到两者之间的匹配?我要死在这里了。

【问题讨论】:

    标签: sql-server vb.net gridview loops


    【解决方案1】:

    对于 300 个 gridview 行中的每一行,您都在循环遍历所有 20k 数据行。这使得 300 * 20k = 600 万次循环迭代。难怪你的循环很慢。 :-)

    让我建议以下算法(伪代码):

    For Each gridviewrow
        Execute a SELECT statement on your DB with a WHERE clause that compares all three components
        If the SELECT statement returns a row
            --> found
        Else
            --> not found
        End If
    Next
    

    使用此解决方案,您只有 300 次循环迭代。在每个循环迭代中,您对数据库进行 SELECT。如果您已正确索引您的数据库(即,如果您在字段manufacturerNamemodelNameserialNo 上有一个复合索引),那么这个SELECT 应该非常快——比遍历所有20k 数据行要快得多。

    从数学的角度来看,这会将算法的时间复杂度从O(n * m) 降低到O(n * log m),其中n 表示gridview 中的行数,m 表示您的gridview 中的记录数数据库。

    【讨论】:

    • 我认为在循环中使用 sql 不好?我试试看!谢谢!
    • @xrum:一如既往:这取决于。当然,对内存中的 DataTable 进行一 (1) 次访问比对数据库执行一 (1) 个 SQL 更快。但是访问一个 DataTable 20k 次可能比执行一条 SQL 慢得多。
    【解决方案2】:

    虽然 Heinzi 的回答是正确的;在循环之前执行昂贵的 SQL 查询并使用数据视图进行过滤可能更有益,这样您就不会访问数据库 300 次

    Execute a SELECT statement on your DB 
    For Each gridviewrow
        if my datagridview.Select(String.format("manufacturerName={0}", row.item("ManufacturerName"))
        If the dataview has a row
            --> found
        Else
            --> not found
        End If
    Next
    

    注意:我只比较了一个标准来说明这一点,您可以在这里过滤所有三个

    【讨论】:

      【解决方案3】:

      嗯...如何将电子表格中的数据加载到 tempdb 中的表中,然后编写一个 select 以您想要比较的方式比较行?这样,所有数据比较都在服务器端进行,您将能够利用 SQL 实例的所有功能。

      【讨论】:

        猜你喜欢
        • 2018-11-11
        • 2021-08-19
        • 1970-01-01
        • 2017-05-12
        • 2014-03-19
        • 1970-01-01
        • 2016-09-07
        • 2016-11-23
        • 1970-01-01
        相关资源
        最近更新 更多