Is jOOQ fetchLazy () really lazy?
I am using jOOQ 3.4.2 to query PostgreSQL database using Scala. I want to get small chunks of the result set at a time without loading the entire result set into memory, since the query can return many rows. I have the following code:
val query = context.selectFrom(table)
.where(conditions)
.orderBy(orderField)
.fetchSize(1)
val cursor = query.fetchLazy()
// Iterate through cursor using cursor.fetchOne()
It looks like jOOQ will load the entire result set (or at least a huge chunk of the result set) into memory when I call fetchLazy()
, before I even fetch the first row from the cursor (judging by the large number of recvfrom()
syscalls I see while fetchLazy()
). Am I doing something wrong?
source to share
Am I doing something wrong?
Not from jOOQ's side. This is how you ensure that both jOOQ (via ResultQuery.fetchLazy()
and Cursor.fetchOne()
) and the JDBC driver (via ResultQuery.fetchSize()
, which translates as PreparedStatement.setFetchSize()
) will fetch exactly one line at a time.
However, you may encounter this PostgreSQL JDBC warning stating that fetchSize
you need to execute the query in an explicit transaction to work.
source to share