Slow performance of SqlDataReader

不想你离开。 提交于 2019-11-29 04:13:32

I would set up a trace in SQL Server Profiler to see what SET options settings the connection is using when connecting from .NET code, and what settings are being used in SSMS. By SET options settings, I mean

ARITHABORT
ANSI_NULLS
CONCAT_NULL_YIELDS_NULL
//etc

Take a look at MSDN for a table of options

I have seen the problem before where the options were different (in that case, ARITHABORT) and the performance difference was huge.

I had that problem to. Tick the "arithmetic abort" setting in the Connection Settings of the DB server.

Also, query analyzer does not download the full contents of the large text or large binary fields. Your SqlDataReader could take longer because it does download the full contents.

I would check to see how long the actual retrieval is taking.

for instance:

  Private Sub timeCheck()
    'NOTE: Assuming you have a sqlconnection object named conn

    'Create stopwatch
    Dim sw As New System.Diagnostics.Stopwatch

    'Setup query
    Dim com As New SqlClient.SqlCommand("QUERY GOES HERE", conn)

    sw.Start()

    'Run query
    Dim dr As SqlClient.SqlDataReader = com.ExecuteReader()

    sw.Stop()

    'Check the time
    Dim sql_query_time As String = CStr((sw.ElapsedMilliseconds / 1000)) & " seconds"
  End Sub

This will allow you to see whether the hold-up is in the retrieval, or in the execution of the reader.

If you ar executing the reader in a loop, where it executes many times, then make sure you are using CommandBehavior.CloseConnection

    SqlCommand cmd = new SqlCommand();
    SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)

If you don't, each time the loop processes the line, when it finishes and the rdr and the connection object drops out of scope, the connection object will not be explicitly closed, so it will only get closed and released back to the pool when the Garbage Collector finally gets around to finalizing it...

Then, if your loop is fast enough, (which is very likely), you will run out of connections. (The pool has a maximum limit it can generate)

This will cause extra latency and delays as the code keeps creating extra unnecessary connections, (up to the maximum) and waiting for the GC to "catch up" with the loop that is using them...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!