Does this mean to fill the Page of 1000 records, this query is called 20 times (20 x fetch size 50)?
No.
The query will get executed only once per page.
The fetch size determines how many rows the database should send at once before waiting for the client to signal that it has processed all the rows send so far which happens automatically by accessing all the rows in a ResultSet
which in turn happens when the result is converted to a List
or similar by the JPA implementation.
This is especially useful when not all rows fit into memory.
So this argument is not that relevant.
Another thing your database might do is optimise the query plan for the fetch size specified.
This means it tries tor bring the first n rows to you as fast as possible even though this might make it take longer to get all rows.
Since you have to fill a Page
object before your program can continue you want your fetch size so large that all the data for a single page gets fetched in one go.
For a simple entity this might be exactly the number of rows in the Page
.
But if the entity has an eagerly fetched one-to-many relationship it might actually do a join and fetch way more then 1000 rows.
So far the theory.
In practice you just shouldn't specify a fetch size and just leave it to the database driver.
And only when you see problems (or have a good reason to expect some) should you actually experiment with different fetch sizes to see where the best value for your scenario is.