0

Working with a Postgres table containing large records, nearly 7m. I understand SELECT * FROM table is large enough to fit in memory, so db connection is lost after a long delay waiting for query result (can only execute SELECT * FROM table LIMIT n).

I need to process each record going through sequentially until the last. What is the way to do this?

7
  • You should see if you can do this work in the database. Why do you need to process each record individually in the application? Commented Apr 22, 2019 at 11:12
  • @GordonLinoff I want to fetch each record and send (to an application) as JSON. Commented Apr 22, 2019 at 11:16
  • 1
    What is the exact error message you get when you run that statement? Which Programming language are you using? How are you planning to handle 7 million JSON values in your application? Commented Apr 22, 2019 at 11:21
  • @a_horse_with_no_name This is a one-time upload of previously collected environmental datasets, that I want to do to a different platform (FIWARE precisely). Because the dataset isn't in the FIWARE standards, each record should be processed to conform to FIWARE model before forwarding to a publish/subscribe application on the FIWARE platform. Commented Apr 22, 2019 at 11:27
  • I don't think the delay is caused by waiting for the query result, Instead, it is probaly caused by the application , processing the first few records, and not reading the rest. You can test this by dumping to .csv or .tsv and work from there. Commented Apr 22, 2019 at 11:43

1 Answer 1

2

You can get in chunks of n records.

select * from table where tableId>currentTableId and tableId<currentTableId*chunk_size

Explation:

Lets say you have 100 records and your memory contains 10 records at a time.

you query for execute 10 time from your application(any ORM)

 int totalRecords = executeQuery('select count(*) from table');
    int chunkSize = 10;
    int chunks = totalRecords/chunkSize ;
    while(chuks>0){
    executeQuery('select * from table where tableId>currentTableId and tableId<currentTableId*chunkSize ');
chunk-=chunkSize;
    }

This code is in a very abstract way. It's just a hint for you.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you for your answer. Can you please add a little explanation to this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.