4

我正在使用一个非常复杂的查询从我们的一个计费数据库中检索一些数据。

我遇到了一个问题,当使用 SQL Developer 执行查询时,查询似乎完成得相当快,但在使用该OracleDataAdapter.Fill()方法时似乎从未完成。

我只是想读取大约 1000 行,查询在 SQL Developer 中完成大约 20 秒。

什么可能导致性能出现如此巨大的差异?我有大量使用相同功能快速运行的其他查询。


这是我用来执行查询的代码:

using Oracle.DataAccess.Client;

...

public DataTable ExecuteExternalQuery(string connectionString, string providerName, string queryText)
{
    DbConnection connection = null;
    DbCommand selectCommand = null;
    DbDataAdapter adapter = null;

    switch (providerName)
    {
        case "System.Data.OracleClient":
        case "Oracle.DataAccess.Client":
            connection = new OracleConnection(connectionString);
            selectCommand = connection.CreateCommand();
            adapter = new OracleDataAdapter((OracleCommand)selectCommand);
            break;
        ...
    }

    DataTable table = null;
    try
    {
        connection.Open();

        selectCommand.CommandText = queryText;
        selectCommand.CommandTimeout = 300000;
        selectCommand.CommandType = CommandType.Text;

        table = new DataTable("result");
        table.Locale = CultureInfo.CurrentCulture;
        adapter.Fill(table);
    }
    finally
    {
        adapter.Dispose();

        if (connection.State != ConnectionState.Closed)
        {
            connection.Close();
        }
    }

    return table;
}

这是我正在使用的 SQL 的概要:

with
  trouble_calls as
  (
    select
      work_order_number,
      account_number,
      date_entered
    from
      work_orders
    where
      date_entered >= sysdate - (15 + 31)  -- Use the index to limit the number of rows scanned
     and
      wo_status not in ('Cancelled')
     and
      wo_type = 'Trouble Call'
  )
select
  account_number,
  work_order_number,
  date_entered
from
  trouble_calls wo
where
  wo.icoms_date >= sysdate - 15
 and
  (
    select
      count(*)
    from
      trouble_calls repeat
    where
      wo.account_number = repeat.account_number
     and
      wo.work_order_number <> repeat.work_order_number
     and
      wo.date_entered - repeat.date_entered between 0 and 30
  ) >= 1
4

3 回答 3

4

这段代码帮助了我,试试吧:

using (OracleConnection conn = new OracleConnection())
{
     OracleCommand comm = new OracleCommand();
     comm.Connection = conn;
     comm.FetchSize = comm.FetchSize * 16;
     comm.CommandText = "select * from some_table";

     try
     {
          conn.Open();
          OracleDataAdapter adap = new OracleDataAdapter(comm);
          System.Data.DataTable dt = new System.Data.DataTable();
          adap.Fill(dt);
     }
     finally
     {
          conn.Close();
     }
}

技巧是一致的(尝试从 8 到 64 的值以找到最适合您的情况的值):

comm.FetchSize = comm.FetchSize * 16;

更新:

这是一个改进的代码:

OracleConnection myConnection = new OracleConnection(myConnectionString);
OracleCommand myCommand = new OracleCommand(mySelectQuery, myConnection);
myConnection.Open();
using (OracleDataReader reader = myCommand.ExecuteReader(CommandBehavior.CloseConnection))
{
    // here goes the trick
    // lets get 1000 rows on each round trip
    reader.FetchSize = reader.RowSize * 1000;

    while (reader.Read())
    {
        // reads the records normally
    }
}// close and dispose stuff here

这里

于 2015-10-13T12:31:41.427 回答
2

使用 Microsoft Data Provider for Oracle 和本机 Oracle Data Provider 之间存在已知的性能差异。

你都试过了吗?

你想用这个查询来实现什么?忘记技术的东西,只是这一切的目标。也许您的查询可能有曲调。

您是否尝试过使用探查器查看卡住的位置?

于 2010-03-12T17:47:11.810 回答
1

我认为您的 Oracle 查询返回的文化和日期是不同的,这就是应用程序需要大量时间来解析的地方。

于 2010-03-12T17:50:01.113 回答