Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between setting a fetch size on Statement vs ResultSet

What is the difference between setting a fetch size on Statement vs ResultSet? Oracle documentation says that if fetch size is set on a Statement then the same is used by ResultSet as well, as long as the fetch size is set prior to obtaining the result set. What difference does it make if I set it on Statement or a ResultSet? I am using Oracle database. Following is Oracle documentation about it:

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.

Standard JDBC also enables you to specify the number of rows fetched with each database round-trip for a query, and this number is referred to as the fetch size. In Oracle JDBC, the row-prefetch value is used as the default fetch size in a statement object. Setting the fetch size overrides the row-prefetch setting and affects subsequent queries run through that statement object.

Fetch size is also used in a result set. When the statement object run a query, the fetch size of the statement object is passed to the result set object produced by the query. However, you can also set the fetch size in the result set object to override the statement fetch size that was passed to it.

like image 712
JUG Avatar asked May 23 '16 15:05

JUG


People also ask

What is ResultSet fetch size?

The result set fetch size, either set explicitly, or by default equal to the statement fetch size that was passed to it, determines the number of rows that are retrieved in any subsequent trips to the database for that result set.

What is the difference between ResultSet and RowSet?

A ResultSet always maintains connection with the database. A RowSet can be connected, disconnected from the database. It cannot be serialized. A RowSet object can be serialized.

Which method is used to set the fetch size?

To set the fetch size for a query, call setFetchSize on the statement object prior to running the query. If you set the fetch size to N, then N rows are fetched with each trip to the database.

What is ResultSet and statement?

A ResultSet object is a table of data representing a database result set, which is usually generated by executing a statement that queries the database. For example, the CoffeeTables. viewTable method creates a ResultSet , rs , when it executes the query through the Statement object, stmt .


1 Answers

The only place the fetch size really matters is on the ResultSet. When a ResultSet is created, it gets its fetch size from the Statement that was used to create it, but it can be changed later.

Think of the Statement's fetch size as a default that will be passed to all the ResultSets it creates. If you don't want to use this default, you can override it.

like image 93
Mureinik Avatar answered Sep 23 '22 22:09

Mureinik