fetchsize in resultset set to 0 by default

后端 未结 3 656
無奈伤痛
無奈伤痛 2021-01-14 17:37

I have to query a database and result set is very big. I am using MySQL as data base. To avoid the \"OutOfMemoryError\" after a lot of search I got two options:

相关标签:
3条回答
  • 2021-01-14 18:05

    In MySQL connector-j 5.1 implementation notes, they said there are 2 ways to handle this situation.

    enter image description here

    The 1st is to make the statement retrieve data row by row, the 2nd can support batch retrieve.

    0 讨论(0)
  • 2021-01-14 18:14

    Besides all you should change your query like

    SELECT * FROM RandomStones LIMIT 1000;
    

    Or

    PreparedStatement stmt = connection.prepareStatement(qry);
    stmt.setFetchSize(1000);
    stmt.executeQuery();
    

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

    0 讨论(0)
  • 2021-01-14 18:15

    The MySQL JDBC driver always fetches all rows, unless the fetch size is set to Integer.MIN_VALUE.

    See the MySQL Connector/J JDBC API Implementation Notes:

    By default, ResultSets are completely retrieved and stored in memory. In most cases this is the most efficient way to operate, and due to the design of the MySQL network protocol is easier to implement. If you are working with ResultSets that have a large number of rows or large values, and cannot allocate heap space in your JVM for the memory required, you can tell the driver to stream the results back one row at a time.

    To enable this functionality, create a Statement instance in the following manner:

    stmt = conn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY,
                  java.sql.ResultSet.CONCUR_READ_ONLY);
    stmt.setFetchSize(Integer.MIN_VALUE);
    

    The combination of a forward-only, read-only result set, with a fetch size of Integer.MIN_VALUE serves as a signal to the driver to stream result sets row-by-row. After this, any result sets created with the statement will be retrieved row-by-row.

    0 讨论(0)
提交回复
热议问题