我正在使用 .Net 和 Java 从 Oracle DB 中读取 100 万条记录。在 .Net 中,我在 Java ojdbc6 瘦客户端中使用 ODP.Net。在 .Net 中读取数据大约需要 10 秒,而在 Java 中大约需要 2 分钟。为什么会有如此巨大的差异?
这是一个代码:
。网:
try
{
DateTime dt1 = DateTime.Now;
OracleConnection con = new OracleConnection();
con.ConnectionString = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=myHost)(PORT=myPort)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=myService)));User Id=myId;Password=myPass;";
con.Open();
string cmdQuery = "SELECT * FROM DROPME";
OracleCommand cmd = new OracleCommand(cmdQuery);
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
int i = 0;
OracleDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Object o1 = reader.GetValue(0);
Object o2 = reader.GetValue(1);
Object o3 = reader.GetValue(2);
Object o4 = reader.GetValue(3);
Object o5 = reader.GetValue(4);
Object o6 = reader.GetValue(5);
Object o7 = reader.GetValue(6);
i++;
}
DateTime dt2 = DateTime.Now;
double elapsed = (dt2 - dt1).TotalSeconds;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
爪哇:
try
{
long t0 = System.currentTimeMillis();
oracleDataSource = new OracleDataSource();
oracleDataSource.setURL("jdbc:oracle:thin:myId/myPass@myHost:myPort:myService");
Connection connection = oracleDataSource.getConnection();
PreparedStatement statement = connection.prepareStatement("SELECT * FROM DROPME");
ResultSet result = statement.executeQuery();
int i = 0;
while (result.next())
{
result.getObject(1);
result.getObject(2);
result.getObject(3);
result.getObject(4);
result.getObject(5);
result.getObject(6);
result.getObject(7);
i++;
}
long t1 = System.currentTimeMillis();
long elapsed = (t1 - t0)/1000;
int t = 0;
}
catch (Exception ex)
{
ex.printStackTrace();
}
编辑: setFetchSize() 完成了这项工作,谢谢。