Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to figure out the optimal fetch size for the select query

In JDBC the default fetch size is 10, but I guess that's not the best fetch size when I have a million rows. I understand that a fetch size too low reduces performance, but also if the fetch size is too high.

How can I find the optimal size? And does this have an impact on the DB side, does it chew up a lot of memory?

like image 715
KKO Avatar asked Jun 09 '15 11:06

KKO


People also ask

What is fetch size in SQL?

The fetch size is the default value for statement and prepared statement to a returns database record of the 10 sizes. The fetch size is a property that uses a statement to fetch more than one database row using a driver. The fetch size is method gives hints to the driver to maintain or manage database rows.

What is a good fetch size for JDBC?

By default, most JDBC drivers use a fetch size of 10. , so if you are reading 1000 objects, increasing the fetch size to 256 can significantly reduce the time required to fetch the query's results.

How to check fetch size in Oracle?

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.

What is hibernate fetch size?

fetch_size - Used to specify number of rows to be fetched in a select query. hibernate. jdbc. batch_size - Used to specify number of inserts or updates to be carried out in a single database hit.


1 Answers

If your rows are large then keep in mind that all the rows you fetch at once will have to be stored in the Java heap in the driver's internal buffers. In 12c, Oracle has VARCHAR(32k) columns, if you have 50 of those and they're full, that's 1,600,000 characters per row. Each character is 2 bytes in Java. So each row can take up to 3.2MB. If you're fetching rows 100 by 100 then you'll need 320MB of heap to store the data and that's just for one Statement. So you should only increase the row prefetch size for queries that fetch reasonably small rows (small in data size).

like image 59
Jean de Lavarene Avatar answered Sep 19 '22 13:09

Jean de Lavarene