get n records at a time from a temporary table
Posted
by Claudiu
on Stack Overflow
See other posts from Stack Overflow
or by Claudiu
Published on 2010-06-17T21:34:54Z
Indexed on
2010/06/17
21:43 UTC
Read the original article
Hit count: 195
I have a temporary table with about 1 million entries. The temporary table stores the result of a larger query. I want to process these records 1000 at a time, for example. What's the best way to set up queries such that I get the first 1000 rows, then the next 1000, etc.? They are not inherently ordered, but the temporary table just has one column with an ID, so I can order it if necessary. I was thinking of creating an extra column with the temporary table to number all the rows, something like:
CREATE TEMP TABLE tmptmp AS
SELECT ##autonumber somehow##, id
FROM .... --complicated query
then I can do:
SELECT * FROM tmptmp WHERE autonumber>=0 AND autonumber < 1000
etc... how would I actually accomplish this? Or is there a better way? I'm using Python and PostgreSQL.
© Stack Overflow or respective owner