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

49.8k views Asked by At

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?

5

There are 5 answers

0
Justin Cave On BEST ANSWER

As with (almost) anything, the way to find the optimal size for a particular parameter is to benchmark the workload you're trying to optimize with different values of the parameter. In this case, you'd need to run your code with different fetch size settings, evaluate the results, and pick the optimal setting.

In the vast majority of cases, people pick a fetch size of 100 or 1000 and that turns out to be a reasonably optimal setting. The performance difference among values at that point are generally pretty minimal-- you would expect that most of the performance difference between runs was the result of normal random variation rather than being caused by changes in the fetch size. If you're trying to get the last iota of performance for a particular workload in a particular configuration, you can certainly do that analysis. For most folks, though, 100 or 1000 is good enough.

0
Nirmala On

JDBC does have default prefetch size of 10. Check out OracleConnection.getDefaultRowPrefetch in JDBC Javadoc

0
Jean de Lavarene On

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).

0
przemek hertel On

The default value of JDBC fetch size property is driver specific and for Oracle driver it is 10 indeed.

For some queries fetch size should be larger, for some smaller.

I think a good idea is to set some global fetch size for whole project and overwrite it for some individual queries where it should be bigger.

Look at this article:

http://makejavafaster.blogspot.com/2015/06/jdbc-fetch-size-performance.html

there is description on how to set up fetch size globally and overwrite it for carefully selected queries using different approaches: Hibernate, JPA, Spring jdbc templates or core jdbc API. And some simple benchmark for oracle database.

As a rule of thumb you can:

  • set fetchsize to 50 - 100 as global setting
  • set fetchsize to 100 - 500 (or even more) for individual queries
0
Stephan On

tl;dr

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

  1. Evaluate some maximal amount of memory (bytesInMemory)

    4Mb, 8Mb or 16Mb are good starts.

  2. Evaluate the maximal size of each column in the query and sum up those sizes (bytesPerRow)

    ...

  3. Use this formula: fetch_size = bytesInMemory / bytesPerRow

    You may adjust the formula result to have predictable values.

Last words, test with different bytesInMemory values and/or different queries to appreciate the results in your application.


The above response was inspired by the (as of this writing attic) Apache MetaModel project. They found an answer for this exact question. To do so, they built a class for calculating a fetch size given a maximal memory amount. This class is based on an Oracle whitepaper explaining how Oracle JDBC drivers manage memory.

Basically, the class is constructed with a maximal memory amount (bytesInMemory). Later, it is asked a fetch size for a Query (an Apache Metamodel class). The Query class helps find the number of bytes (bytesPerRow) a typical query results row would have. The fetch size is then calculated with the below formula:

fetch_size = bytesInMemory / bytesPerRow

The fetch size is also adjusted to stay in this range : [1,25000]. Other adjustments are made along during the calculation of bytesPerRow but that's too much details for here.

This class is named FetchSizeCalculator. The link leads to the full source code.