Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6878201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:41:39+00:00 2026-05-27T04:41:39+00:00

I am using the groovy.sql.Sql class to query a database and process the results.

  • 0

I am using the groovy.sql.Sql class to query a database and process the results. My problem is that the ResultSet can be very large; so large that I risk running out of memory if I try to process the whole ResultSet at once. I know the Sql.rows() method supports paging using offset and max results parameters but I haven’t been able to find a good example of how to use it (and I’m not certain that paging is what I’m looking for).

Basically, here’s what I’m trying to do:

def endOfResultSet = false
for(int x = 1; !endOfResultSet; x+=1000){
    def result = sql.rows("Select * from table", x, 1000)
    processResult(result)
    endOfResultSet = result.size()!=1000
}

My question is if Groovy is smart enough to reuse the same result set for the sql.rows("Select * from table", x, 1000) call or if it will be repeatedly be running the same statement on the database and then paging to where the offset starts.

Your help is appreciated, Thanks!

Edit: What I’m trying to avoid is running the same query on the database multiple times. I’d like to run the query once, get the first 1,000 rows, process them, get the next 1,000 rows, etc… until all the rows are processed.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:41:40+00:00Added an answer on May 27, 2026 at 4:41 am

    I assume you’ve seen this blog post about paging?

    To answer your question, if we look at the code for the Sql class in Groovy, we can see that the code for rows(String,int,int) calls rows(String,int,int,null)

    And the code for that is:

        AbstractQueryCommand command = createQueryCommand(sql);
        ResultSet rs = null;
        try {
            rs = command.execute();
            List<GroovyRowResult> result = asList(sql, rs, offset, maxRows, metaClosure);
            rs = null;
            return result;
        } finally {
            command.closeResources(rs);
        }
    

    So as you can see, it gets the full ResultSet, then steps through this inside the asList method, filling a List<GroovyRowResult> object with just the results you requested.

    Edit (after the question was edited)

    As I said in my comment below, I think you’re going to need to write your own paging query for the specific database you are using… For example, with MySQL, your above query can be changed to:

    def result = sql.rows( "SELECT * FROM table LIMIT ${Sql.expand x}, 1000" )
    

    Other databases will have different methods for this sort of thing…I don’t believe there is a standard implementation

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Groovy SQL in a Grails application to query against a database
I am using Groovy Sql to fetch results. This is the output from my
We are using grails-groovy with Hibernate, I have a SQL query like this in
I'm using groovy gsql to query to Mysql database. Everything goes well on localhost
Hi I want to design a class using Groovy Sql which I will be
How can I use the Groovy Console to kick off junit tests? (Currently using
With SQLite, user defined SQL functions can easily be added using the C api
DB: Sql Server 2008. I have a really (fake) groovy query like this:- SELECT
By using g:datePicker on java.sql.Time object that refers to a TIME legacy DB column
I am using groovy's SQL querying functions and MarkupBuilder to write xml files. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.