Pinch
Pinch

Reputation: 2888

How does fetchLazy work in jooq?

How does fetchLazy work in jooq?
Is it equivalent to doing paginated select with limit and offset?

Upvotes: 3

Views: 791

Answers (1)

Lukas Eder
Lukas Eder

Reputation: 220842

They're different.

fetchLazy()

... returns a Cursor type, which is jOOQ's equivalent of the JDBC ResultSet type. The query will fully materialise in the database, but jOOQ (JDBC) will fetch rows one-by-one. This is useful

  • when large result sets need to be fetched without waiting for the data transfer between server and client to finish - as opposed to a simple fetch(), which loads all rows from the server in one go.
  • when the client doesn't know in advance how many rows they really want to fetch from the server.

LIMIT .. OFFSET

... will reduce the number of returned rows already in the database, without them ever surfacing in the client. This can heavily improve execution speed in the server, as the server

  • May choose a different execution plan - e.g. using nested loops instead of hash joins for low values of LIMIT
  • Doesn't need to keep an open cursor for a long data transfer time, as only few rows are transferred over the wire.

Upvotes: 2

Related Questions