Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 9D4356327A9 for ; Fri, 19 Mar 2010 16:13:02 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 15444-04 for ; Fri, 19 Mar 2010 19:12:48 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-bw0-f216.google.com (mail-bw0-f216.google.com [209.85.218.216]) by mail.postgresql.org (Postfix) with ESMTP id 8E7C662DBCE for ; Fri, 19 Mar 2010 16:12:51 -0300 (ADT) Received: by bwz8 with SMTP id 8so3343360bwz.3 for ; Fri, 19 Mar 2010 12:12:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type; bh=BhEHQzp+UjMr/QsOEgcl6D5c0dDOqw3rlOQmNvQFMIE=; b=Pj6gQyG1sR+X4i0haXPfxVfW+Fl17DMNTMfl+PXo0njBucSkDUB8qtGsrRhi9oao9o FfvBW+B/+h7Owlnqllxf/nlkYZRrzH3BknCcx4+b4aO7vTeGWfl1/22/UHjXtUQiKSdS 9VyxiIkfFRvZMTecDvapWCbzI1XLKe9yF+ajM= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; b=IkCz7cw6CyQnRpn+N0QCh7Lq/qAwyTm1qWBtUsU/kgGSHREfyErYXRBVIW8lPHvg1D x3DXgb7vRAmUIbVxpp+PYSdKv5tz6YU9S+2Ug058Zvenhv3b2kXfNOjwSrkO4ors5uel i3YrdP2jXeIixDsMJl/oJxcx0XOgKkIU2gfIg= MIME-Version: 1.0 Received: by 10.204.174.205 with SMTP id u13mr2713268bkz.169.1269025969533; Fri, 19 Mar 2010 12:12:49 -0700 (PDT) In-Reply-To: <4BA36D7B.6010506@gmail.com> References: <4BA36D7B.6010506@gmail.com> Date: Fri, 19 Mar 2010 14:12:49 -0500 Message-ID: Subject: Re: too complex query plan for not exists query and multicolumn indexes From: Dave Crooke To: Corin Cc: pgsql-performance@postgresql.org Content-Type: multipart/alternative; boundary=00032555717e1ffac004822c22cc X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.418 tagged_above=-10 required=5 tests=AWL=0.180, BAYES_00=-2.599, HTML_MESSAGE=0.001 X-Spam-Level: X-Archive-Number: 201003/245 X-Sequence-Number: 38070 --00032555717e1ffac004822c22cc Content-Type: text/plain; charset=ISO-8859-1 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 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 > > --00032555717e1ffac004822c22cc Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable K.I.S.S. here ..... the best way to do one of these in most DB's is typ= ically an outer join and test for null:

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

On Fri, Mar 19, 2010 at 7:26 AM, Corin &= lt;wakathane@gmail.com> wrote:
Hi all!

While evaluting the pgsql query planer I found some weird behavior of the q= uery planer. I think it's plan is way too complex and could much faster= ?

CREATE TABLE friends (
=A0 id integer NOT NULL,
=A0 user_id integer NOT NULL,
=A0 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 5= 00.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 W= HERE f1.user_id=3Df2.ref_id AND f1.ref_id=3Df2.user_id) OFFSET 1000000 LIMI= T 1

<snip>
=A0

--00032555717e1ffac004822c22cc--