Re: Speed Up Offset and Limit Clause

From: "Craig A(dot) James" <cjames(at)modgraph-usa(dot)com>
To: Christian Paul Cosinas <cpc(at)cybees(dot)com>
Cc: "'PFC'" <lists(at)peufeu(dot)com>, pgsql-performance(at)postgresql(dot)org
Subject: Re: Speed Up Offset and Limit Clause
Date: 2006-05-17 02:20:12
Message-ID: 446A885C.2050308@modgraph-usa.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Christian Paul Cosinas wrote:
> I am creating an application that gets the value of a large table and write
> it to a file.
>
> Why I want to use offset and limit is for me to create a threaded
> application so that they will not get the same results.
>
> For example:
>
> Thread 1 : gets offset 0 limit 5000
> Thread 2 : gets offset 5000 limit 5000
> Thread 3 : gets offset 10000 limit 5000
>
> And so on...
>
> Would there be any other faster way than what It thought?

In order to return rows 10000 to 15000, it must select all rows from zero to 15000 and then discard the first 10000 -- probably not what you were hoping for.

You might add a "thread" column. Say you want to run ten threads:

create sequence thread_seq
increment by 1
minvalue 1 maxvalue 10
cycle
start with 1;

create table mytable(
column1 integer,
... other columns...,
thread integer default nextval('thread_seq')
);

create bitmap index i_mytable_thread on mytable(thread);

Now whenever you insert into mytable, you get a value in mytable.thread between 1 and 10, and it's indexed with a highly efficient bitmap index. So your query becomes:

Thread 1: select ... from mytable where ... and thread = 1;
Thread 2: select ... from mytable where ... and thread = 2;
... and so forth.

Craig

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Michael Fuhr 2006-05-17 02:31:30 Re: Adding and filling new column on big table
Previous Message David Wheeler 2006-05-17 02:08:51 Re: IMMUTABLE?