本文介绍了ODP.Net - OracleDataReader.Read很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多的OracleDataReader在ODP.Net麻烦。基本上,我有一个参数化查询需要1-5秒的任何地方运行(返回大约450个记录),然后在需要60-90秒,循环(无code,即使运行在循环,从字面上遍历记录和无所事事)。

当我从的Aqua Data Studio中运行它,它需要1-5秒。当我从净它需要1-5秒cmd.ExecuteReader运行()返回。当我遍历所有的450条记录与OracleDataReader.Read需要60-90秒才能完成。

我甚至拿出了所有的code中循环,刚刚有了一个空白虽然dr.Read,它仍然花了60到90秒遍历这些450记录(我用秒表来获得时间cmd.ExecuteReader,然后周围的空dr.Read循环)。

我已经尝试设置FETCHSIZE,一点用也没有(和,这是在我的测试情况下,只有450条记录)。我曾尝试与连接字符串转向自动调节关闭,其性能降低,甚至更多。

为什么OracleDataReader.Read这么长时间,当它返回(和其他工具返回相同的数据,相同的查询中的一小部分时间)的少量数据的?

 使用康恩作为新Oracle.DataAccess.Client.OracleConnection(System.Configuration.ConfigurationManager.ConnectionStrings("oracle_dss").ConnectionString)
    conn.Open()
    使用CMD作为的OracleCommand = conn.CreateCommand
        cmd.BindByName = TRUE
        cmd.CommandText ='删除SQL,使这个更具可读性

        月底
        昏暗paramMonthEndDate作为的OracleParameter = cmd.CreateParameter
        paramMonthEndDate.ParameterName =:month_end_date
        paramMonthEndDate.DbType = DbType.Date
        paramMonthEndDate.Value = monthEnd
        cmd.Parameters.Add(paramMonthEndDate)

        昏暗的SW作为新System.Diagnostics.Stopwatch
        sw.Start()

        cmd.FetchSize = 1000
        昏暗博士为OracleDataReader = cmd.ExecuteReader
        dr.FetchSize = dr.RowSize * 1000

        sw.Stop()
        Me.Log(的String.Format(月末查询:{0}的,sw.ElapsedMilliseconds / 1000))

        sw.Reset()
        sw.Start()

        虽然dr.Read

        结束在

        sw.Stop()

        Me.Log(的String.Format(月末通过查询记录:{0}的,sw.ElapsedMilliseconds / 1000))

        dr.Close()
            结束使用
    conn.Close()
结束使用
 

解决方案

可能是我错了,但实际上你获取数据此行中:虽然dr.Read ,而不是当你正在执行的读者。因此,这可以解释,为什么即使没有什么也不做, dr.Read 采取一切你的时间。

我想尝试你的命令,切换到

1)。运行纯SQL(不带参数)

2)。运行使用常规(非绑定变量)参数

3)。如果可能的话将SQL code存储过程

I'm having a lot of trouble with the OracleDataReader in ODP.Net. Basically, I have a parameterized query that takes anywhere from 1-5 seconds to run (returning around 450 records) and then takes 60-90 seconds to loop over (with no code even running in the loop, literally iterating over the recordset and doing nothing).

When I run it from Aqua Data Studio it takes 1-5 seconds.When I run it from .Net it takes 1-5 seconds for cmd.ExecuteReader() to return.When I loop over the 450 records with OracleDataReader.Read it takes 60-90 seconds to finish.

I even took out all of the code in the loop and just had a blank "While dr.Read" and it still took 60 to 90 seconds to loop over those 450 records (I used a Stopwatch to get the time for the cmd.ExecuteReader and then around the empty dr.Read loop).

I have tried setting the FetchSize, it didn't help (and, it's only 450 records in my test case).I have tried turning auto tuning off with the connection string, it degraded performance even more.

Why is the OracleDataReader.Read taking so long when it's a small amount of data being returned (and other tools return the same data for the same query in a fraction of the time)?

    Using conn As New Oracle.DataAccess.Client.OracleConnection(System.Configuration.ConfigurationManager.ConnectionStrings("oracle_dss").ConnectionString)
    conn.Open()
    Using cmd As OracleCommand = conn.CreateCommand
        cmd.BindByName = True
        cmd.CommandText = ""  ' removed SQL to make this more readable

        ' Month end
        Dim paramMonthEndDate As OracleParameter = cmd.CreateParameter
        paramMonthEndDate.ParameterName = ":month_end_date"
        paramMonthEndDate.DbType = DbType.Date
        paramMonthEndDate.Value = monthEnd
        cmd.Parameters.Add(paramMonthEndDate)

        Dim sw As New System.Diagnostics.Stopwatch
        sw.Start()

        cmd.FetchSize = 1000
        Dim dr As OracleDataReader = cmd.ExecuteReader
        dr.FetchSize = dr.RowSize * 1000

        sw.Stop()
        Me.Log(String.Format("Month End Query: {0}s", sw.ElapsedMilliseconds / 1000))

        sw.Reset()
        sw.Start()

        While dr.Read

        End While

        sw.Stop()

        Me.Log(String.Format("Month End Query through recordset: {0}s", sw.ElapsedMilliseconds / 1000))

        dr.Close()
            End Using
    conn.Close()
End Using
解决方案

May be I am wrong, but you actually fetch the data in this row: While dr.Read, and not when you are executing the reader. So this can explain why even without doing nothing, dr.Read take all your time.

I'd try to change your command to

1). Run plain sql (without parameters)

2). Run using regular (not binding variable) parameter

3). Move the sql code to Stored Procedure if possible

这篇关于ODP.Net - OracleDataReader.Read很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 13:15