Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are likely causes of StringBuilder and ResultSet performance issues

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.

like image 939
Jeff Dalley Avatar asked Dec 31 '25 07:12

Jeff Dalley


1 Answers

I think your note2 speak by itself.

The time is not lost in the StringBuilder, but somewhere else...


columnName = rsmeta.getColumnName(i+1);

Reading the meta-data could be very slow, depending on the implementation. You could read them only once for all resultSets, and reuse them in the loop.


UPDATE

From your last update, the StringBuilder is out of concern, and the problem is with the ResultSet. I feel that the question title, and all answers given, are not in synch with your current preoccupation.
I suggest closing this question, and opening a new one for the new preoccupation :-)

like image 142
KLE Avatar answered Jan 01 '26 19:01

KLE