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

对着背影说爱祢 提交于 2019-12-20 09:59:14

问题


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?


回答1:


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.




回答2:


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




回答3:


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



回答4:


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



来源:https://stackoverflow.com/questions/30730383/how-to-figure-out-the-optimal-fetch-size-for-the-select-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!