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 6161999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:34:39+00:00 2026-05-23T21:34:39+00:00

I have a lot of rows in a database and it must be processed,

  • 0

I have a lot of rows in a database and it must be processed, but I can’t retrieve all the data to the memory due to memory limitations.

At the moment, I using LIMIT and OFFSET to retrieve the data to get the data in some especified interval.

I want to know if the is the faster way or have another method to getting all the data from a table in database. None filter will be aplied, all the rows will be 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-23T21:34:40+00:00Added an answer on May 23, 2026 at 9:34 pm
    SELECT * FROM table ORDER BY column
    

    There’s no reason to be sucking the entire table in to RAM. Simply open a cursor and start reading. You can play games with fetch sizes and what not, but the DB will happily keep its place while you process your rows.

    Addenda:

    Ok, if you’re using Java then I have a good idea what your problem is.

    First, just by using Java, you’re using a cursor. That’s basically what a ResultSet is in Java. Some ResultSets are more flexible than others, but 99% of them are simple, forward only ResultSets that you call ‘next’ upon to get each row.

    Now as to your problem.

    The problem is specifically with the Postgres JDBC driver. I don’t know why they do this, perhaps it’s spec, perhaps it’s something else, but regardless, Postgres has the curious characteristic that if your Connection has autoCommit set to true, then Postgres decides to suck in the entire result set on either the execute method or the first next method. Not really important as to where, only that if you have a gazillion rows, you get a nice OOM exception. Not helpful.

    This can easily be exactly what you’re seeing, and I appreciate how it can be quite frustrating and confusing.

    Most Connection default to autoCommit = true. Instead, simply set autoCommit to false.

    Connection con = ...get Connection...
    con.setAutoCommit(false);
    PreparedStatement ps = con.prepareStatement("SELECT * FROM table ORDER BY columm");
    ResultSet rs = ps.executeQuery();
    while(rs.next()) {
        String col1 = rs.getString(1);
        ...and away you go here...
    }
    rs.close();
    ps.close();
    con.close();
    

    Note the distinct lack of exception handling, left as an exercise for the reader.

    If you want more control over how many rows are fetched at a time into memory, you can use:

    ps.setFetchSize(numberOfRowsToFetch);
    

    Playing around with that might improve your performance.

    Make sure you have an appropriate index on the column you use in the ORDER BY if you care about sequencing at all.

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

Sidebar

Related Questions

I have a process that imports a lot of data (950k rows) using inserts
At the moment, I have lot's of Java which does all kind of nifty
So i have a database where there is a lot of data being inserted
I have an Android application that installs a database (~2000 rows across all tables)
I have a database containing Page ojects with html content. A lot of the
I have a database with a lot of words to be used in a
I imported a database with PHPMyAdmin. A lot of rows were useless and I
I have a table in my database with a lot of fields. Most of
I have a problem with displaying data from mysql database. My client requires to
I have a table in a database with rows as follows: Milk Eggs Butter

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.