Re: Need advice to avoid ORDER BY

From: Scott Marlowe <scott(dot)marlowe(at)gmail(dot)com>
To: Condor <condor(at)stz-bg(dot)com>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Need advice to avoid ORDER BY
Date: 2013-04-04 23:29:39
Message-ID: CAOR=d=1FwYehYpoZJ4ER82PqVYKh9N9rOCHkmWno1iO+Y5RjuQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Try an index like:

create index yada on mytable (id) where valids=0;

then

select max(jobid) from mytable where valids=0;

On Thu, Apr 4, 2013 at 3:32 PM, Condor <condor(at)stz-bg(dot)com> wrote:

> Hello,
>
> I have one query in my postgresql 9.2.3 that took 137 ms to me executed
> and looking a way
> what I can do to optimize it. I have one table generated numbers from 1 to
> 1 000 000 and
> I need to get first free id, meanwhile id's when is taken can be free
> (deleted data and id
> is free for next job). Table is simple:
>
>
> id serial,
> jobid text,
> valids int default 0
>
> (Yes, I have index).
>
>
> my query is: SELECT jobid FROM mytable WHERE valids = 0 ORDER BY id ASC
> LIMIT 1
>
> I need the first id only.
>
> My question is: Is there a way how I can avoid using ORDER BY to receive
> the first
> free id from mytable ?
>
>
>
> Cheers,
> Condor
>
>
>
> --
> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/**mailpref/pgsql-general<http://www.postgresql.org/mailpref/pgsql-general>
>

--
To understand recursion, one must first understand recursion.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Adrian Klaver 2013-04-05 00:03:07 Re: Triggers on Foreign Tables
Previous Message Robert Lefkowitz 2013-04-04 23:28:50 Triggers on Foreign Tables