gpt4 book ai didi

.net - 为什么 OracleDataAdapter.Fill() 很慢?

转载 作者:行者123 更新时间:2023-12-04 16:21:17 25 4
gpt4 key购买 nike

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

我遇到了一个问题,当使用 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

最佳答案

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

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

来自 here

关于.net - 为什么 OracleDataAdapter.Fill() 很慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2434654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com