I’m looping through a ResultSet in Java; which for testing purposes is returning about 30 rows with 17 Columns (all String data) per row. I’m manually building an XML String out of the results using StringBuilder and its literally taking about 36 seconds for the loop to finish these iterations.
Note: I realize this isn’t the best way to go about getting XML out of a Database, or even the best way to get XML out of a ResultSet – but this has me curious of the slow performance regardless.
Update: As per responses so far I have to address the following:
The time to run the Query is less than a second, and I did a System.currentTimeMillis() before and after every section of my code to narrow this down. The 36 seconds is entirely within the code below.
ResultSetMetaData rsmeta = rset.getMetaData();
StringBuilder resultBuilder = new StringBuilder();
resultBuilder.append("<?xml version=\"1.0\" ?><ROWSET>");
if(numColumns != 0){
while (rset.next()) {
resultBuilder.append("<ROW>");
for (int i = 0; i <= numColumns -1;i++) {
columnName = rsmeta.getColumnName(i+1);
resultBuilder.append("<");
resultBuilder.append(columnName);
resultBuilder.append(">");
resultBuilder.append(rset.getString(i+1));
resultBuilder.append("</");
resultBuilder.append(columnName);
resultBuilder.append(">");
}
resultBuilder.append("</ROW>");
numRows += 1;
}
}
else {
stmt.close();
wsConn.close();
return "No Results";
}
Update: Given the suggestions I received – this code takes roughly the same amount of time give or take half a second.
StringBuilder resultBuilder = new StringBuilder();
resultBuilder.append("<?xml version=\"1.0\" ?><ROWSET>");
if(numColumns != 0){
while (rset.next()) {
resultBuilder.append("<ROW>");
for (int i = 0; i <= numColumns -1;i++) {
//columnName = rsmeta.getColumnName(i+1);
resultBuilder.append("<");
resultBuilder.append("TestColumnName");
resultBuilder.append(">");
//resultBuilder.append(rset.getString(i+1));
resultBuilder.append("TestData");
resultBuilder.append("</");
resultBuilder.append("TestColumnName");
resultBuilder.append(">");
}
resultBuilder.append("</ROW>");
numRows += 1;
}
}
else {
stmt.close();
wsConn.close();
return "No Results";
}
The last test I did having eliminated everything else was to replace the while test with a realistic number of iterations (160, the max rows returned from the small tests I’ve done previously). The question now is, what could it be about this result set that causes such a slow down.
while (numRows <= 160) {
// same as above
}
Update: As suggested I’ll be closing this question as the title doesn’t reflect the direction the problem took.
I very much doubt that
StringBuilderis the culprit here. Java uses it extensively, I have used it extensively, I have rewritten it for my own sort-of-JVM, and basically it is always able to eat characters by the hundreds of millions per second.I think your trouble comes from the database access itself. When you run a query and get a
ResultSet, it does not necessarily mean that all the data has been obtained and internally transformed into an easy-to-manage memory representation. Depending on the database implementation (and its JDBC driver), theResultSetmay be a promise of a number of results, which are dynamically fetched when theResultSet.next()andResultSet.getString()methods are called.Try simply exploring the results, calling all your
next()andgetString(), but without storing the obtained data in yourStringBuilder. If it still takes 36 seconds thenStringBuilderis innocent (which I strongly believe it is).