pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Dave Crooke <dcrooke@gmail.com>
To: Corin <wakathane@gmail.com>
Cc: pgsql-performance@postgresql.org
Subject: Re: too complex query plan for not exists query and multicolumn indexes
Date: Fri, 19 Mar 2010 14:12:49 -0500
Message-ID: <ca24673e1003191212j77cb9583i4aae6f83f9c9c16c@mail.gmail.com> (raw)
In-Reply-To: <4BA36D7B.6010506@gmail.com>
References: <4BA36D7B.6010506@gmail.com>

K.I.S.S. here ..... the best way to do one of these in most DB's is
typically an outer join and test for null:

select f1.* from friends f1
   left outer join friends f2 on (f1.user_id=f2.ref_id and
f1.ref_id=f2.user_id)
   where f2.id is null;

On Fri, Mar 19, 2010 at 7:26 AM, Corin <wakathane@gmail.com> wrote:

> Hi all!
>
> While evaluting the pgsql query planer I found some weird behavior of the
> query planer. I think it's plan is way too complex and could much faster?
>
> CREATE TABLE friends (
>   id integer NOT NULL,
>   user_id integer NOT NULL,
>   ref_id integer NOT NULL,
> );
>
> ALTER TABLE ONLY friends ADD CONSTRAINT friends_pkey PRIMARY KEY (id);
> CREATE INDEX user_ref ON friends USING btree (user_id, ref_id);
>
> I fill this table with around 2.800.000 random rows (values between 1 and
> 500.000 for user_id, ref_id).
>
> The intention of the query is to find rows with no "partner" row. The
> offset and limit are just to ignore the time needed to send the result to
> the client.
>
> SELECT * FROM friends AS f1 WHERE NOT EXISTS (SELECT 1 FROM friends AS f2
> WHERE f1.user_id=f2.ref_id AND f1.ref_id=f2.user_id) OFFSET 1000000 LIMIT 1
>
> <snip>

view thread (7+ messages)  latest in thread

Message-ID: <ca24673e1003191212j77cb9583i4aae6f83f9c9c16c@mail.gmail.com>
Permalink:  ../ca24673e1003191212j77cb9583i4aae6f83f9c9c16c@mail.gmail.com/
Also on:    postgresql.org/message-id/ca24673e1003191212j77cb9583i4aae6f83f9c9c16c@mail.gmail.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-performance@postgresql.org
  Cc: dcrooke@gmail.com, wakathane@gmail.com
  Subject: Re: too complex query plan for not exists query and multicolumn indexes
  In-Reply-To: <ca24673e1003191212j77cb9583i4aae6f83f9c9c16c@mail.gmail.com>
  References: <4BA36D7B.6010506@gmail.com> <ca24673e1003191212j77cb9583i4aae6f83f9c9c16c@mail.gmail.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