sql—在比较gridview行和数据库时,如何使for循环更快?

drkbr07n  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(507)

我正在获取一组超过4万张的记录。要求在表中突出显示数据库中存在或不存在的记录。
因此,我们所做的是选取每个记录,并使用一个过程与数据库表进行比较。

If exists(Select FunctionalLocation from EngineeringData where
                 FunctionalLocation = @Asset_Number 
)
Begin
                 Select 1
End

                 Else
Begin                

                 Select 0
End

END

但这需要很多时间,有时会超时。
有什么方法可以优化这个代码吗?

For k = 0 To gv_InfoFunctionalLocation.Rows.Count - 1

    If k < 50000 Then    ' to test only

        Dim row = gv_InfoFunctionalLocation.Rows(k)

        Dim RowEffect As Integer
        Dim dSetReturn As New DataSet
        ParamValue(0) = row.Cells(1).Text
        ParamValue(1) = row.Cells(4).Text
        dSetReturn = Func.SP_ReturnDataset(con, "[dbo].[USP_Check_FunctionalLocations]", True, ParamName, ParamType, ParamValue)
        RowEffect = dSetReturn.Tables(0).Rows(0).Item(0)
        Dim gRow As GridViewRow = gv_InfoFunctionalLocation.Rows(k)
        Dim cBox As CheckBox = CType(gRow.FindControl("chkSelect"), CheckBox)

        If RowEffect = 0 Then
            'dtFL.Rows(k).Cells("Status").Value = True
            'dtFL.Rows(k).ReadOnly = True
            gv_InfoFunctionalLocation.Rows(k).BackColor = System.Drawing.Color.Tomato
            cBox.Checked = True

        Else

            gv_InfoFunctionalLocation.Rows(k).BackColor = System.Drawing.Color.YellowGreen
            cBox.Checked = False

        End If

        If cBox.Checked = True Then
            btnImportFL.Enabled = True
            btnImportFL.BackColor = Drawing.Color.Navy
        End If
    End If

Next

由于我们时间和资源短缺,是否有快速解决办法?

bybem2ql

bybem2ql1#

如果数据库中的数据集远远大于40k,则将数据的id值大容量加载到db(temp表)中,然后对其进行内部连接,以便仅下载数据库中已经存在的id集。将查询结果下载到哈希集中
否则,如果db数据集的数量级不大于40k,只需下载db中的每个id并将其加载到hashset中,
在这两种情况下,您现在都可以迭代数据,询问hashset是否存在id

相关问题