我正在从ResultSet中读取数据并且它似乎很慢(可能,遗憾的是,这只是你可以获得的速度)。
我正在阅读包含5列的568,365行。我已经分解了代码并添加了计时器,以查看花费最多的时间。
ResultSet.next() - about 65ms
ResultSet.wasNull() - about 150ms
ResultSet.getString/Double() - about 1000ms
所以我希望减少获得琴弦/双打的时间。
我的连接创建:
DriverManager.getConnection("jdbc:sqlserver://server:1433;databaseName=dbname", "user", "pass")
我的陈述创建:
conn.createStatement()
我尝试使用“setFetchSize”(1000,10000,500000)和“selectMethod = cursor / direct”,但没有运气。
我只能循环访问并检索数据,所以如果有任何其他命中或设置,我可以尝试给JDBC ......?
我的代码是(带有计时器):
private List<Object[]> getResultRows(ResultSet queryResult) throws SQLException {
List<Object[]> rows = new ArrayList<>();
ResultSetMetaData rsmd = queryResult.getMetaData();
int colNumber = rsmd.getColumnCount();
int[] colTypes = new int[colNumber];
for (int i = 0; i < colNumber; i++) {
colTypes[i] = rsmd.getColumnType(i + 1);
}
StopWatch next = new StopWatch();
StopWatch read = new StopWatch();
read.start();
read.suspend();
StopWatch isnull = new StopWatch();
isnull.start();
isnull.suspend();
next.start();
// Run on rows
while (queryResult.next()) {
next.suspend();
Object[] rowValues = new Object[colNumber];
// Run on columns
for (int i = 0; i < colNumber; i++) {
read.resume();
rowValues[i] = this.getValueByType(i + 1, colTypes[i], queryResult);
read.suspend();
isnull.resume();
// if last value was null - set null
if (queryResult.wasNull())
rowValues[i] = null;
isnull.suspend();
}
rows.add(rowValues);
next.resume();
}
next.suspend();
return rows;
}
private Object getValueByType(int columnIndex, int columnType, ResultSet queryResult) throws SQLException {
switch (columnType) {
case Types.CHAR:
case Types.NCHAR:
case Types.VARCHAR:
case Types.NVARCHAR:
return queryResult.getString(columnIndex);
case Types.INTEGER:
return queryResult.getInt(columnIndex);
case Types.TIMESTAMP:
return queryResult.getTimestamp(columnIndex);
case Types.DATE:
return queryResult.getDate(columnIndex);
default:
return queryResult.getDouble(columnIndex);
}
}