JDBC Choose batch / sample size using MySQL

I have a Java application using JDBC that runs once a day on my server and interacts with the MySQL database (v5.5) also running on the same server. The application queries and iterates through the all rows in the table.

The table is currently quite small (~ 5000 rows), but will continue to grow indefinitely. The memory of my servers is limited, and I do not like that the memory consumption of the application is uncertain.

If I use statement.setFetchSize(n)before running the request, I do not understand what is happening here. For example, if I use something like:

PreparedStatement query = db.prepareStatement("SELECT x, y FROM z");
query.setFetchSize(n);
ResultSet result = query.executeQuery();
while ( result.next() ){
    ...
}

How to control potentially large queries of choice? What's going on here? If nequal to 1000, then MySQL will only pull 1000 rows into memory at a time (knowing where it left off), and then capture the next 1000 (or as many) rows as it needs to be?

Edit:
Now it’s clear to me that adjusting the sample size is not useful to me. Remember that my application and the MySQL server are running on the same computer. If MySQL pulls the entire query result into memory, then this affects the application, as they both use the same physical memory.

+4
source share
1 answer

MySQL Connector/J all, Integer.MIN_VALUE ( AFAIK). . " MySQL/J JDBC API ResultSet.

, ( ), , LIMIT ( setFetchSize(Integer.MIN_VALUE)).

+6

Source: https://habr.com/ru/post/1543672/


All Articles