Skip to content
Advertisement

How to select rows using only number previous loaded rows?

I got a table with some rows

For example:

DROP TABLE IF EXISTS test_t;

CREATE TABLE test_t(
    test_id serial PRIMARY KEY,
    test_name VARCHAR (50) UNIQUE NOT NULL
);

INSERT INTO test_t (test_id, test_name) VALUES (1, 'a');
INSERT INTO test_t (test_id, test_name) VALUES (2, 'b');
INSERT INTO test_t (test_id, test_name) VALUES (3, 'c');
INSERT INTO test_t (test_id, test_name) VALUES (4, 'd');

SELECT * FROM test_t;

Them i load a half of rows

SELECT * FROM test_t LIMIT 2;

After that i need to load all other rows, but all what i know about previous operation is that i load only 2 rows

So i got only the number of already loaded rows = 2

So know i got a number or rows from previous loading, but not the values of loaded rows

and i want to load all the last rows

Can i do that with such a query?

SELECT * FROM test_t OFFSET 2;

As i see know, in PGAdmin, when i run this query all the time i get the same result. Wont it return some day an another result? Some random rows, for example?

This is a little example, in fact i got a big table with 800_000_000 values, and i load half of them, it spend many days, and now i want to stop loading, stop project, update code and continue loading from this table, but i dont want to load it again from start, i want to load only another half of table, and all i know is the number of already loaded rows

Is the query:

SELECT * FROM test_t OFFSET 2;

Suit for such a case?

Advertisement

Answer

You can try:

select * 
from table_name 
offset 100000000

But better use ORDER BY not to get random values in result

select * 
from table_name 
ORDER BY some_column
offset 100000000
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement