0

我创建了一个网页,员工可以在其中上传 Excel xlsx 文档进行处理,如果满足要求,则返回相同的文档并填写某些关键单元格。它可以工作,但我对它的运行速度不满意。通过 2,500 行需要几分钟。

它运行如下:用户上传文档->服务器端打开excel文档并逐行执行->如果当前行的第31列!= 10然后连接到SQL并根据第5列执行查询。->查询结果进入该行第 35 列。-> 下一行。冲洗并重复。-> 完成后,将文件返回给用户。

虽然这有效,但它也相当慢。有人对解决此问题的更好方法有任何想法吗?

 For n = 2 To excelWorkSheet.Rows.Count

            Dim thing As String = excelWorkSheet.Cells(n, 5).Value
            'only until empty row 
            If excelWorkSheet.Cells(n, 5).Value = "" Then
                Exit For
            End If


            If excelWorkSheet.Cells(n, 31).Value <> "10" Then
                strSQL = "EXEC sp_Lookup_Type " & thing
                cmd.CommandText = strSQL
                cmd.Connection = cn
                rdrSQL = cmd.ExecuteReader()


                While rdrSQL.Read()
                    If Not IsDBNull(rdrSQL("TYPE")) Then
                        excelWorkSheet.Cells(n, 35).Value = rdrSQL("TYPE")
                    Else
                        excelWorkSheet.Cells(n, 35).Value = "NO DATA"
                    End If
                End While

                cmd.Dispose()
                rdrSQL.Close()
            End If
        Next

        cn.Close()
4

1 回答 1

1

我的目标是减少 SQL 调用的数量。

Option #1: If the number of items in your lookup table is small enough...

  • Download all of the lookup table once at the beginning of your loop
  • Within the loop, check this local copy of the database table instead of going to the database each time.

Option #2: If you can't cache all of the items up front...

  • Loop through your spreadsheet once and gather a list of items to lookup.
  • Make a single call to the database, sending all of the items at once.
  • The database returns a small list of values that are needed.
  • Use the small list as your database cache and follow the steps in "Option #1"
于 2013-10-01T20:46:52.493 回答