简单的select语句消耗SQL Server中的服务器内存或超时

时间:2014-10-09 09:54:19

标签: sql .net sql-server memory out-of-memory

我正在运行一个非常简单的select语句,它从一个大表(超过1000万行)返回一个varchar(50)列。该表已有pk

示例:

select employee_name from tblEmployees

在.net中,我使用SqlDataReader来回读数据

  1. 有时查询超时
  2. SQL Server内存使用率上升,直到消耗所有服务器资源,并且在SqlDataReader和/或连接关闭时不会释放
  3. 请问有解决方法吗?

    感谢。

    代码:

        Dim M1 As String() = {"234701", "234703", "234704", "234705", "234706", "234707", "234708", "234709", "234802", "234803", "234804", "234805", "234806", "234807", "234808", "234809", "234810", "234811", "234812", "234813", "234814", "234815", "234816", "234817", "234818", "234819", "234909", "234902", "234903", "234905"}
        Dim M2 As String() = {"2347025", "2347026", "2347027", "2347028", "2347029"}
        Dim M3 As String() = {"23490", "23491"}
    
        Dim N1 As New Dictionary(Of Long, Byte) 'ok
        Dim N2 As New Dictionary(Of Long, Byte) 'maybe
    
        Function GetMobile(v As String) As Mobile
            If v.Length < 10 Then
                Return Nothing
            End If
    
            Dim sb = New Text.StringBuilder
    
            For i = 0 To v.Length - 1
                Select Case v(i)
                    Case "0"c To "9"c
                        sb.Append(v(i))
                End Select
            Next
    
            Dim m = "234" + Right(sb.ToString, 10)
    
            If m.Length = 13 Then
                If M1.Contains(Left(m, 6)) OrElse M2.Contains(Left(m, 7)) Then
                    Return New Mobile With {.number = CLng(m), .ok = OK.Yes}
                ElseIf Left(m, 6) = "234702" OrElse M3.Contains(Left(m, 5)) Then
                    Return New Mobile With {.number = CLng(m), .ok = OK.Maybe}
                Else
                    Return Nothing
                End If
            Else
                Return Nothing
            End If
        End Function
    
    Using dr = **ExecuteReader**("select col1 from myTable")
      While dr.Read
          If dr.IsDBNull(0) Then Continue While
    
          Dim m = GetMobile(CStr(dr(0)))
    
          If IsNothing(m) Then Continue While
    
          Select Case m.ok
              Case OK.Yes : N1(m.number) = 1
              Case OK.Maybe : N2(m.number) = 2
          End Select
      End While
    End Using
    
    Public Function **ExecuteReader**(sql As String) As SqlDataReader
        Dim conn = New SqlConnection(connString1)
        Dim cmd = New SqlCommand(sql, conn)
        conn.Open()
        Return cmd.ExecuteReader(CommandBehavior.CloseConnection)
    End Function
    

0 个答案:

没有答案