Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle JDBC performance of ResultSet

I was using so far something like this for querying my database that was working perfectly fine :

PreparedStatement prepStmt = dbCon.prepareStatement(mySql);
ResultSet rs = prepStmt.executeQuery();

But then I needed to use the rs.first(); in order to be able to iterate over my rs multiple times. So I use now

PreparedStatement prepStmt = dbCon.prepareStatement(mySql,ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);    

My question is related to the performance of the two. What do I lose if I use the second option? Will using the second option have any negative effect in the code that I have written so far?

PS: Note that my application is a multi-user, database-intensive web application (on a Weblogic 10.3.4) that uses a back end Oracle 11g database.

Thanks all for your attention.

UPDATE

My maximum reslutset size will be less than 1000 rows and 15-20 columns

like image 602
MaVRoSCy Avatar asked Oct 15 '12 06:10

MaVRoSCy


People also ask

How does JDBC calculate ResultSet size?

Simply iterate on ResultSet object and increment rowCount to obtain size of ResultSet object in java. rowCount = rs. getRow();

Why do we use ResultSet in JDBC?

JDBC ResultSet interface is used to store the data from the database and use it in our Java Program. We can also use ResultSet to update the data using updateXXX() methods. ResultSet object points the cursor at before the first row of the result data. Using the next() method, we can iterate through the ResultSet.

How does JDBC fetch size work?

Fetch Size. By default, when Oracle JDBC runs a query, it retrieves a result set of 10 rows at a time from the database cursor. This is the default Oracle row fetch size value. You can change the number of rows retrieved with each trip to the database cursor by changing the row fetch size value.

Which are the two types of ResultSet in JDBC?

There are two types of result sets namely, forward only and, bidirectional.


2 Answers

If you're using scrollability (your second option), pay attention to this:

Important: Because all rows of any scrollable result set are stored in the client-side cache, a situation where the result set contains many rows, many columns, or very large columns might cause the client-side Java Virtual Machine (JVM) to fail. Do not specify scrollability for a large result set.

Source: Oracle Database JDBC Developer's Guide and Reference

like image 106
Miljen Mikic Avatar answered Sep 19 '22 02:09

Miljen Mikic


Since an Oracle cursor is a forward-only structure, in order to simulate a scrollable cursor, the JDBC driver would generally need to cache the results in memory if it wants to be able to ensure that the same results are returned when you iterate through the results a second time. Depending on the number and size of the results returned from the query, that can involve a substantial amount of additional memory being consumed on the application server. On the other hand, that should mean that iterating through the ResultSet a second time should be much more efficient than the first time.

Whether the extra memory required is meaningful depends on your application. You say that the largest ResultSet will have 1000 rows. If you figure that each row is 500 bytes (this will obviously depend on data types-- if your ResultSet just has a bunch of numbers, it would be much smaller, if it contains a bunch of long description strings, it may be much larger), 1000 rows is 500 kb per user. If you've got 1000 simultaneous users, that's only 500 MB of storage which probably isn't prohibitive. If you've got 1 million simultaneous users, on the other hand, that's 500 GB which is probably means that you're buying a few new servers. If your rows are 5000 bytes rather than 500, then you're talking about 5 GB of RAM which could be a large fraction of the memory required on the application server to run your application.

like image 43
Justin Cave Avatar answered Sep 19 '22 02:09

Justin Cave