Casting A Long Javax.servlet.jsp.jstl.sql.Result To List Of Strings Is Very Slow

- 1 answer

The for loop at the end of this is very slow when there are 50k rows. Is there a quicker way to get a list of Strings from the rows of a javax.servlet.jsp.jstl.sql.Result? Use a different collection? Convert the Strings differently?

The query is absolutely fine. I am not including here the custom objects that are used to run it. I can't change them. I think you only need to know that it returns a Result.

(I didn't write any of this code.)

    private int m_ObjectGroupId;
    private int m_FetchSize;
    private ArrayList<String> m_InternalIds;

...method following member initialisation...

    String internalIdString = "INTERNALID";
    String selectSql = "SELECT " + internalIdString + " FROM MODELOBJECT WHERE OBJECTGROUPID = ?";

    ArrayList<Object> valuesToSet = new ArrayList<Object>();

    BaseForPreparedStatement selectStatement = new BaseForPreparedStatement(selectSql.toString(), valuesToSet);

    SqlQueryResult queryResult = DBUtils.executeQueryRS(p_Context, selectStatement, getConnection(), m_FetchSize);
    Result result = queryResult.getResult();
    m_InternalIds = new ArrayList<>(result.getRowCount());

    for (int i = 0; i < result.getRowCount(); i++) {

UPDATE: The query only takes 1s whereas the loop takes 30s. result.getRows().getClass() is a java.util.SortedMap[].



Depending on the implementation of javax.servlet.jsp.jstl.sql.Result#getRows() (for example the Tomcat taglibs at it can be that getRows() does unnecessary work each time you call it.

You could rewrite your extraction loop as

for (SortedMap m: result.getRows()) {
    m_InternalIds.add((String) m.get(internalIdString));

which calls getRows() only once.