pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jens Hartwig <jhartwig@debis.com>
To: PostgreSQL SQL <pgsql-sql@postgresql.org>
Subject: Strange Execution-Plan for NOT EXISTS
Date: Thu, 28 Dec 2000 11:55:02 +0100
Message-ID: <3A4B1C06.2A5C6C94@debis.com> (raw)

Hello all,

I tried (just for academical fun) to resolve ID´s which are not used
anymore, e.g.:

ID   NAME
-- + ------------------
1  | NOBODY
2  | ANYBODY
4  | EVERYBODY

Now I want to get "3" as the next usable ID. I really know that this is
kind of bad style but a friend of mine asked me if I had a solution for
this problem.

My solution would be the following statement:

--------------------------------
select (min(id) + 1)
from t_dummy d1
where not exists (
    select id
    from t_dummy d2
    where d2.id = (d1.id + 1)
);
--------------------------------

The explain plan for this statement, tested against a real table with
about 8,000 records in a freshly vacuumed database, looks like the
following:

Aggregate  (cost=2924207.88..2924207.88 rows=1 width=12)
  ->  Seq Scan on t_dummy d1  (cost=0.00..2924207.88 rows=1 width=12)
        SubPlan
          ->  Seq Scan on t_dummy d2  (cost=0.00..331.36 rows=1 width=12
)

Why that? Wouldn´t it be possible to simple use the primary key index in
the sub-query and exit from the outer query at the first occurence of an
ID which has no following entry? Any ideas from anyone? Am I wrong?

Best regards, Jens

=============================================
Jens Hartwig
---------------------------------------------
debis Systemhaus GEI mbH
10875 Berlin
Tel.     : +49 (0)30 2554-3282
Fax      : +49 (0)30 2554-3187
Mobil    : +49 (0)170 167-2648
E-Mail   : jhartwig@debis.com
=============================================



view thread (2+ messages)  latest in thread

Message-ID: <3A4B1C06.2A5C6C94@debis.com>
Permalink:  ../3A4B1C06.2A5C6C94@debis.com/
Also on:    postgresql.org/message-id/3A4B1C06.2A5C6C94@debis.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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: jhartwig@debis.com
  Subject: Re: Strange Execution-Plan for NOT EXISTS
  In-Reply-To: <3A4B1C06.2A5C6C94@debis.com>
  References: <3A4B1C06.2A5C6C94@debis.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