pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: anthony.shipman@symstream.com
To: pgsql-performance@postgresql.org
Cc: Claudio Freire <klaussfreire@gmail.com>
Subject: Re: strange query plan with LIMIT
Date: Wed, 8 Jun 2011 18:34:07 +1000
Message-ID: <201106081834.07263.anthony.shipman@symstream.com> (raw)
In-Reply-To: <BANLkTim7DL+n0yuZp+Mpeq0Vozp1qJ4M0A@mail.gmail.com>
References: <201106071802.08272.anthony.shipman@symstream.com>
	<BANLkTi=+6EYeiFwKv5Eq8AAMt_nCZ+8W6g@mail.gmail.com>
	<BANLkTim7DL+n0yuZp+Mpeq0Vozp1qJ4M0A@mail.gmail.com>

On Wednesday 08 June 2011 17:39, Claudio Freire wrote:
> Of course optimally executing a plan with limit is a lot different
> than one without.

I imagined that limit just cuts out a slice of the query results. 
If it can find 80000 rows in 0.5 seconds then I would have thought that 
returning just the first 100 of them should be just as easy.

>
> Just... why are you sorting by diag_id?
>
> I believe you would be better off sorting by timestamp than diag_id,
> but I don't know what the query is supposed to do.

The timestamp is only almost monotonic. I need to scan the table in slices and 
I use limit and offset to select the slice.

I've forced the query order with some pgsql like:

declare
    query   character varying;
    rec     record;
begin
    -- PG 8.3 doesn't have the 'using' syntax nor 'return query execute'

    execute 'create temporary table tt on commit drop as ' ||
        'select diag_id from tdiag ' || v_where;

    query = 'select * from tdiag where diag_id in (select * from tt) ' ||
            'order by diag_id ' || v_limit || ' ' || v_offset;

    for rec in execute query loop
        return next rec;
    end loop;
end;

-- 
Anthony Shipman                 | Life is the interval
Anthony.Shipman@symstream.com   | between pay days.



view thread (17+ messages)  latest in thread

Message-ID: <201106081834.07263.anthony.shipman@symstream.com>
Permalink:  ../201106081834.07263.anthony.shipman@symstream.com/
Also on:    postgresql.org/message-id/201106081834.07263.anthony.shipman@symstream.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: anthony.shipman@symstream.com, klaussfreire@gmail.com
  Subject: Re: strange query plan with LIMIT
  In-Reply-To: <201106081834.07263.anthony.shipman@symstream.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox