Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CachedRowSet slower than ResultSet?

In my java code, I access an oracle database table with an select statement. I receive a lot of rows (about 50.000 rows), so the rs.next() needs some time to process all of the rows.

using ResultSet, the processing of all rows (rs.next) takes about 30 secs

My goal is to speed up this process, so I changed the code and now using a CachedRowSet:

using CachedRowSet, the processing of all rows takes about 35 secs

I don't understand why the CachedRowSet is slower than the normal ResultSet, because the CachedRowSet retrieves all data at once, while the ResultSet retrieves the data every time the rs.next is called.

Here is a part of the code:

try {
    stmt = masterCon.prepareStatement(sql);
    rs = stmt.executeQuery();

    CachedRowSet crset = new CachedRowSetImpl();
    crset.populate(rs);

    while (rs.next()) {
        int countStar = iterRs.getInt("COUNT");
        ...
    }
} finally {
    //cleanup
}
like image 373
ben_muc Avatar asked Feb 03 '26 03:02

ben_muc


2 Answers

CachedRowSet caches the results in memory i.e. that you don't need the connection anymore. Therefore it it "slower" in the first place.

A CachedRowSet object is a container for rows of data that caches its rows in memory, which makes it possible to operate without always being connected to its data source.

-> http://download.oracle.com/javase/1,5.0/docs/api/javax/sql/rowset/CachedRowSet.html

like image 72
MasterCassim Avatar answered Feb 04 '26 17:02

MasterCassim


There is an issue with CachedRowSet coupled together with a postgres jdbc driver.

CachedRowSet needs to know the types of the columns so it knows which java objects to create (god knows what else it fetches from DB behind the covers!).

It therefor makes more roundtrips to the DB to fetch column metadata. In very high volumes this becomes a real problem. If the DB is on a remote server, this is a real problem as well because of network latency.

We've been using CachedRowSet for years and just discovered this. We now implement our own CachedRowSet, as we never used any of it's fancy stuff anyway. We do getString for all types and convert ourselves as this seems the quickest way.

This clearly wasn't an issue with fetch size as postgres driver fetches everything by default.

like image 34
Dan Avatar answered Feb 04 '26 15:02

Dan