Ad

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>();
    valuesToSet.add(m_ObjectGroupId);

    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++) {
        m_InternalIds.add((String)result.getRows()[i].get(internalIdString));
    }

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

Ad

Answer

Depending on the implementation of javax.servlet.jsp.jstl.sql.Result#getRows() (for example the Tomcat taglibs at https://github.com/apache/tomcat-taglibs-standard/blob/main/impl/src/main/java/org/apache/taglibs/standard/tag/common/sql/ResultImpl.java#L134) 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.

Ad
source: stackoverflow.com
Ad